Foreground notification still appearing after Intent service destroyed

1.6k Views Asked by At

I have an IntentService and inside this instant service, in onCreate, I call the method startforeground(). I see then the notification when the intentService is created. However, when the IntentService is destroyed (going to onDestroy), I can see the notification for few seconds after that the service is destroyed. Why is that?

This is the code of the IntentService:

public class USFIntentService extends IntentService {

    private static final String TAG = "USFIntentService";

    private static final int USF_NOTIFICATION_ID = 262276;
    private static final String USF_NOTIFICATION_CHANNEL_ID = "USF_NOTIFICATION_CHANNEL";

    public USFIntentService() {
        super("USFIntentService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG,"in onCreate");
        startUsfForegroundService();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"in onDestroy");
    }

    private void startUsfForegroundService() {
        // Define notification channel
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel =
                new NotificationChannel(USF_NOTIFICATION_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);

        // Build notification to be used for the foreground service.
        Notification notification =
                new Notification.Builder(this, USF_NOTIFICATION_CHANNEL_ID)
                        .setContentTitle(getText(R.string.notification_title))
                        .setContentText(getText(R.string.notification_message))
                        .setSmallIcon(R.drawable.usf_notification_icon)
                        .build();

        // Set the service as a foreground service.
        startForeground(USF_NOTIFICATION_ID, notification);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i(TAG, "onHandleIntent");
        if (intent != null) {
            doStuff();
        }
        Log.i(TAG,"End of onHandleIntent");
    }

}

I call this service like this:

Intent startServiceIntent = new Intent(intent);
startServiceIntent.setComponent(new ComponentName(context, USFIntentService.class));
context.startForegroundService(startServiceIntent); 
3

There are 3 best solutions below

2
On

Try to call Service#stopForeground after your job is done to remove it

5
On

You can call stopForeground(true) when you finish doing the stuff. So that your service gets immediately removed from foreground state and the parameter true ensures that the notification will be removed.

0
On

If STOP_FOREGROUND_REMOVE is supplied, the service's associated notification will be cancelled immediately.

If STOP_FOREGROUND_DETACH is supplied, the service's association with the notification will be severed. If the notification had not yet been shown, due to foreground-service notification deferral policy, it is immediately posted when stopForeground(STOP_FOREGROUND_DETACH) is called. In all cases, the notification remains shown even after this service is stopped fully and destroyed.

stopForeground(STOP_FOREGROUND_REMOVE) // remove with notification 

stopForeground(STOP_FOREGROUND_DETACH) // remove only intent and not notification