2

I want to send push notifications in my app but after few minutes android kill my background service and notifications not showing. How to make background service which android will not close?

BackroundService.class

public Context context = this;
public android.os.Handler handler  = new Handler();
public static Runnable runnable = null;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Log.e("Service", "Service crated!");

    runnable = new Runnable() {
        public void run() {
            Log.e("Service", "Service is still running!");
            Toast.makeText(context, "Service is still running", Toast.LENGTH_SHORT).show();
            handler.postDelayed(runnable, 50000);
        }
    };

    handler.postDelayed(runnable, 15000);
}

@Override
public void onDestroy() {

}

@Override
public void onStart(Intent intent, int startid) {
    Log.e("Service", "Service started by user!");
}

AlarmReceiver.class

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

    int notificationId = intent.getIntExtra("ID", 0);
    String message = intent.getStringExtra("TEXT");
    String tittle = intent.getStringExtra("TITTLE");

    Intent mainIntent = new Intent(context, BackgroundService.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);

    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context);
    builder.setSmallIcon(R.drawable.finance43)
            .setContentTitle(tittle)
            .setContentText(message)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setPriority(Notification.PRIORITY_HIGH)
            .setCategory(Notification.CATEGORY_MESSAGE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = "REMINDERS";
        NotificationChannel channel = new NotificationChannel(channelId,
                "Reminder",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
        builder.setChannelId(channelId);
    }
    notificationManager.notify(notificationId, builder.build());
}

When I start app i see "Service is still running!" for about 1 hour.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Glembinek
  • 77
  • 1
  • 2
  • 4
  • 1
    The system will kill the service if it needs to free resources. However you can mitigate this behavior by starting it as a foreground service or by indicating you want it to be restarted when possible. More [here](https://developer.android.com/guide/components/services) – devgianlu Apr 16 '19 at 18:36
  • Possibly https://stackoverflow.com/questions/48451761/firebase-message-with-high-priority-not-waking-device-from-doze-android-6 has some useful info (especially the second answer) – user3486184 Apr 16 '19 at 18:38
  • With foreground service it's working. Thanks! – Glembinek Apr 20 '19 at 06:29

0 Answers0