2

I recently made an online radio app. I implemented a status bar notification as music player notification. I clear the notification with onDestroy() but the problem is that sometimes onDestroy() is not being called.

How to clear the status bar notification from the status bar programmatically?

I made the code to clear the notification so please don't post that code. I want to know how to clear status bar notification when onDestroy() is not being called. I tried all related StackOverflow links of this problem. Any help will be appreciated.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Ajaz Sakhi
  • 39
  • 5

2 Answers2

2

You got it wrong. You most likely do not want to care onDestroy() at all (as it is NOT called when you think it is). You would be fine knowing when your application is frontmost one (so no notification needed as there is your activity visible) or is running in background (so you need the notification). And for that I'd use extend Application and use ActivityLifecycleCallbacks

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    I +1 this answer because it is probably the root of his problem. OnDestroy might be called a jolly good time later. Maybe you want onStop instead or onPause ? –  Mar 21 '16 at 09:44
  • when i kill application from recent apps by swiping left or right or clear all app from button, notification still in status bar after app closed so how to remove notification from status bar. – Ajaz Sakhi Mar 21 '16 at 10:30
  • cant use on-stop or on-pause because notification will be there if radio is running in background...only notification will be clear when app will closed any way.... – Ajaz Sakhi Mar 21 '16 at 10:38
-1

if you create Notification like this:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(TestActivity.this);
int NOTIFY_ID = 0;

        builder
                .setSmallIcon(R.drawable.some_icon)
                .setTicker(songTitle)
                .setOngoing(true)
                .setContentTitle("Playing")
                .setContentText(songTitle);

        notification = builder.build();

        notificationManager.notify(NOTIFY_ID, notification);

Then you can cancel (delete) this Notification like this:

  notificationManager.cancel(NOTIFY_ID);
Roman Svyatnenko
  • 699
  • 12
  • 27