14

I want to remove a notification after my foreground service is being destroyed. I tried to call stopForeground(true) from onDestroy() and from unbind(). Although onUnbind() and onDestroy() were called(I can see it from logs) notification is still there. I stop the service by calling unbindService(playerConnection) and stopService(this) from my activity.

Why it's not working this way? Any ideas on how to remove a notification when service is destroyed?

UPATE: one interesting thing I've noticed when playing around with the notification. I've made a special method inside the service:

fun hideNotification() {
    stopForeground(true)
    Log.d("PlayerService", "hideNotification")
}

Than I call it from my activity when the button is pressed. And it removes the notification. But the same function being called from activity's onStop() or service's onUnbind() or onDestroy() doesn't. I can't understand what the difference between these situations.

AlexKost
  • 2,792
  • 4
  • 22
  • 42

3 Answers3

2

I had the same issue when trying to stop a foreground service and remove all it's notification.

As a workaround i delete the notification channel and recreate it if needed.

What solved it to me was to do as follows:

  1. Stop the service from within it in onStartCommand() (or elsewhere within the service).
  2. StopForeground(true)
  3. Remove notification channel (this what actually cleaned all the notifications).

Example:

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        var stopService : Boolean  # Service stop condition
        var isRunning : Boolean  # stopSelf() raise exception if service is not running.
        if (stopService}){
            stopForeground(true)
            notificationManager.deleteNotificationChannel("channel_id");
            if (isRunning){
                Log.d(TAG, "Service is running, stopping.")
                stopSelf()
            }else
                Log.d(TAG, "Service is not running, no need to stop.")
            return START_NOT_STICKY
        }

Some references:
How to remove old notification channels?
What is the proper way to stop a service running as foreground

  • creation of notification channel is after or equal to api level 26, yet the notification doesn't get removed even prior to that – juztcode Nov 04 '20 at 04:10
1

After hours of debugging between 2 services, one that removed the notification and the other that doesn't, I found that the only difference between them is that one of them has the android attribute exported to false.

 <service
        ...
        ...
        android:exported="false">

I ignore the reason why setting exported to false make me able to remove the notificatión but I wanted to share this solution

Victor
  • 1,818
  • 13
  • 16
  • I am still not able to make it work using android:exported="true" but I found the following: 1- This happens for Samsung devices, other devices (like Google Pixel) work correctly. 2- The notification is correctly removed after calling stopForeground(true), but another notification is created immediately after. – Victor Mar 15 '22 at 20:50
-4

For removing Notification you have to use notificationManager.cancel(ID); in onDestry() or when ever you want to remove notification.

ID: which ever ID used for displaying notification, same ID need to given in cancel()

Ex: ID = 210;

notificationManager.notify(ID, notification); ==> For display notification.

notificationManager.cancel(ID) ===> For removing notification.

Hope this helps you.

Abdul
  • 18
  • 3
  • 1
    Thanks for comment but it's not working. I start the notification with startForeground(NOTIFICATION_ID, notification) call. So it supposed to be dismissed with stopForeground(true) call. Moreover, stopForeground indeed works if I call it from separate method before onDestroy. See update. – AlexKost Apr 28 '17 at 01:13