Уведомление выскакивает только несколько раз, затем останавливается. Периодически выполняется фоновая служба - PullRequest
0 голосов
/ 21 октября 2019

Я занимаюсь разработкой приложения, в котором мне нужно каждые 3 минуты запускать фоновый сервис для извлечения некоторых данных в формате JSON с сервера.

Сейчас, в целях тестирования, я пытаюсь подключиться к сервису. показывать уведомление каждые 30 секунд. Но, как ни странно, он вызывает службу и показывает уведомление только 3-4 раза и перестает показывать дальнейшие уведомления после этого.

Я использую AlarmManager для выполнения этой задачи.

MainActivity

    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;

    import com.google.android.material.tabs.TabLayout;

    import androidx.viewpager.widget.ViewPager;
    import androidx.appcompat.app.AppCompatActivity;

    import android.util.Log;

    import com.matrix.msnimble.service.CaseReceiver;
    import com.matrix.msnimble.ui.main.SectionsPagerAdapter;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
            ViewPager viewPager = findViewById(R.id.view_pager);
            viewPager.setAdapter(sectionsPagerAdapter);
            TabLayout tabs = findViewById(R.id.tabs);
            tabs.setupWithViewPager(viewPager);

            Intent caseReceiver = new Intent(getApplicationContext(), CaseReceiver.class);
            PendingIntent recurringCaseReceiver = PendingIntent.getBroadcast(getApplicationContext(), 0, caseReceiver, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarms.setInexactRepeating (AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30000, recurringCaseReceiver);
        }
    }

BroadcastReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class CaseReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent pushPullService = new Intent(context, PushPullService.class);
        context.startService(pushPullService);
    }
}

Сервис

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.matrix.msnimble.MainActivity;
import com.matrix.msnimble.R;

public class PushPullService extends IntentService {

    public PushPullService() {
        super(PushPullService.class.getName());
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        createNotificationChannel();
        showNotification();
    }

    private void showNotification(){

        Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, 0);
        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon );

        Notification builder = new NotificationCompat.Builder(this.getApplicationContext(), "casenotification")
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("New Case(s)")
                .setContentText("You have received 5 fresh cases")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setDefaults(Notification.DEFAULT_ALL)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setLargeIcon(largeIcon)
                .setAutoCancel(true).build();
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this.getApplicationContext());

        try {
            int max = 10000;
            int min = 1;
            int range = max - min + 1;

            notificationManager.notify((int)(Math.random() * range) + min, builder);
        }catch
        (Exception e){Log.d("--->",e.getMessage());}
    }

    private void createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "casenotification";
            String description = "com.matrix.msnimble";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel("casenotification", name, importance);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC );
            channel.setDescription(description);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

AndroidManifest

<service
    android:name=".service.PushPullService"
    android:enabled="true"
    android:exported="true">
</service>
<receiver android:name=".service.CaseReceiver" ></receiver>

Снимок экрана уведомления на устройстве

enter image description here

Ответы [ 2 ]

1 голос
/ 21 октября 2019

Попробуйте этот класс обслуживания намерения для вашего непрерывного уведомления и вызова API с некоторой продолжительностью. И одна вещь, которую вы установили startForeground для непрерывного оповещения и оживления вашего сервиса. Я надеюсь, что это поможет вам.

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.matrix.msnimble.MainActivity;
import com.matrix.msnimble.R;

public class PushPullService extends IntentService {
    private Handler mhandler;
    private long mDelay = 500; //Default 500ms delay
    private Runnable mrunnable;
    private int NOTIFICATION_ID = 0;

     private Notification makeNotification(Context context) {
       Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, 0);
    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon );

        String channelId = getString(R.string.channel_name);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setContentTitle("New Case(s)")
                        .setContentText("You have received 5 fresh cases")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setLargeIcon(largeIcon)
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(false);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

       // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    getString(R.string.app_name),
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        Notification n;
        n = notificationBuilder.build();
        return n;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    mhandler = new Handler();
        int delay = 10000; //milliseconds : you can set as per your requirement
        mrunnable = new Runnable() {
            @Override
            public void run() {
                //TODO: Your API call or whatever you want call that method
                mhandler.postDelayed(this, delay);
            }
        };
        mhandler.postDelayed(mrunnable, delay);

        startForeground(1, makeNotification(this));

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null)
            notificationManager.cancel(NOTIFICATION_ID);
        stopForeground(true);
        if (mhandler != null)
            mhandler.removeCallbacks(mrunnable);
    }
}
1 голос
/ 21 октября 2019

Пожалуйста, перейдите по этой ссылке , чтобы сгенерировать уведомление, дать уникальный идентификатор уведомления для каждого отдельного уведомления

...