I have the following code to create an ONGOING_EVENT:
Notification notification = new Notification(R.drawable.icon, "someText" , System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Home.class), 0);
notification.setLatestEventInfo(this, "someTitle", "someText", contentIntent);
notificationManager.notify(SOME_ID, notification);
Now, I want to kill it later on:
notificationManager.cancel(SOME_ID);
However, the notification remains. What could be the cause of this?
The problem is
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;. I misinterpreted the documentation for this flag, and subsequently couldn’t remove the notification. The flag usually is set by a service started withstartForeground(int id, Notification notification)and prevents the removal of the notification until the service is stopped bystopForeground(boolean removeNotification).I now use the
startForeground/stopForegroundmethods and the notification is shown/hidden perfectly fine.