Calling notice of dismissal

Thought that this is not a problem, but still. My service notification doesn't want to delete-reject, no matter what I do.

I have a progress notification that is triggered on a service using startForeground (id, Notification). This is how I built it:

public Notification createErrorNotification(String message) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

    Intent showAppIntent = new Intent(getApplicationContext(), MainActivity.class);
    showAppIntent.setAction(Intent.ACTION_MAIN);
    showAppIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent pendingShowAppIntent = PendingIntent.getActivity(getApplicationContext(), 10,
            showAppIntent, PendingIntent.FLAG_CANCEL_CURRENT);


    builder.setContentTitle(getString(R.string.label_download_error))
            .setWhen(0)
            .setContentText(message)
            .setContentIntent(pendingShowAppIntent)
            .setAutoCancel(true)
            .setOngoing(false)
            .setSmallIcon(R.drawable.ic_launcher);

    return builder.build();
}

      

At some point, if an error occurs, I replace the progress notification with the error notification described above, disabling "current" and other content. I am also completely discontinuing service:

private void stopService() {
    stopSelf();
    taskThread.stopLooper();
    downloadHandler.stopExecution();
    downloadHandler = null;
    taskThread = null;
}

      

And call

stopForeground(false);

      

false - because I want the notification to be kept on screen and dismissed by the user. But firing napkins just don't work.

If I call stopForeground (true) - the notification is correctly removed.

Anyone have any ideas what I am doing wrong here?

+3


source to share


3 answers


As @orium suggested, in order to solve this problem, you need to stop the services in foreground to remove the notification, and create a new notification using the same ID, but with settings that might be rejected. It would be: stopForegound (true) and create a notification with setAutoCancel (true)



+3


source


You can use notification id 0 for notification, in which case a pending deletion notification is called where you can stop the service.



0


source


There is no need to delete the notification. You can detach it from the service using:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
    stopForeground(STOP_FOREGROUND_DETACH);
else
    stopForeground(false);

      

Then you can use the notification manager to re-notify with the same ID if you need to update any information. The STOP_FOREGROUND_DETACH flag will prevent the notification from being fired when the service is destroyed on later versions of Android. You won't see the notification disappear and reappear like you did after dismissing the first.

0


source







All Articles