Reputation: 3042
Thought that wouldn't be a problem at all, but still. My Service notification doesn't want to swipe-dismiss no matter what I do.
I have a progress notification, that is started in the service using startForeground(id, Notification). Here is how i build 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 error occurs, I replace my progress notification with the described Error notification, disabling 'ongoing' and other stuff. I also stop service completely:
private void stopService() {
stopSelf();
taskThread.stopLooper();
downloadHandler.stopExecution();
downloadHandler = null;
taskThread = null;
}
And call
stopForeground(false);
false - because I want notification to be kept on screen and dismissed by user. But swipe-dismissing simply doesn't work.
If I call stopForeground(true) - notification is correctly removed.
Anyone has any ideas what am I doing wrong here?
Upvotes: 4
Views: 4908
Reputation: 642
Removing the notification isn't necessary. You can detach it from the service by using:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
stopForeground(STOP_FOREGROUND_DETACH);
else
stopForeground(false);
You can then using the notification manager to notify again with the same ID if you need to update any information. The STOP_FOREGROUND_DETACH flag will prevent the notification from going away when the service is destroyed on later Android versions. You won't see the notification disappearing and re-appearing like you would after dismissing the first one.
Upvotes: 1
Reputation: 1018
You can use a notification id of 0 for the notification, in which case the delete pending intent of the notification will invoked where you can stop the service.
Upvotes: 0
Reputation: 3042
As @orium have suggested, to resolve this issue you need to stop the services in foreground mode removing notification, and create a new notification using the same ID, but with settings set to dismissable. That would be: stopForegound(true) and create notification with setAutoCancel(true)
Upvotes: 4