5

Is there a way I can check programatically whether my app's notification is currently running(shown)?

That is to say that NotificationManager.notify() was invoked.

tshepang
  • 12,111
  • 21
  • 91
  • 136
kkudi
  • 1,625
  • 4
  • 25
  • 47
  • Seems no. You have to use other method to flag the state of your notification. – xandy Mar 13 '11 at 01:50
  • Possible duplicate of [How to check which notifications are active in status bar in Android Dev?](http://stackoverflow.com/questions/3630733/how-to-check-which-notifications-are-active-in-status-bar-in-android-dev) – Sam Dec 03 '16 at 04:57

3 Answers3

3

Is there a way I can check programatically whether my app's notification is currently running(shown)?

No.

That is to say that NotificationManager.notify() was invoked.

You called notify(). Hence, you already know if notify() was called. You also know if your code calls cancel() or cancelAll(). You will also know, via the various PendingIntents and flags, if the Notification goes away based upon user action. Hence, you have all of the information yourself to determine if the Notification is on-screen or not.

However, savvy developers will write their apps such that they do not care if their Notification is on-screen or not.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    even though this is irrelevant to my question, i just wanted to say that you DO care if your notification is on or screen or not. if there's a pending action, which should not be cleared and want the notification to re-appear when then phone reboots... – kkudi Mar 13 '11 at 02:19
  • 2
    @kkudi: "if there's a pending action, which should not be cleared and want the notification to re-appear when then phone reboots" -- no. You do not want to re-display the `Notification` after a reboot just because the `Notification` is on the screen. You may want to re-display the `Notification` after a reboot because *the work represented by the `Notification` is not yet done*. You are the one doing the work, so you can know if, on a reboot, the work is not yet done and therefore it makes sense to re-display the `Notification`. – CommonsWare Mar 13 '11 at 02:25
  • exactly you re-display the notification because the user has to perform an action when you click on it. =) – kkudi Mar 13 '11 at 02:43
  • it seems that you want, so i will accept it as an answer and set a flag when the notify is called and unset the flag when cancel is called. – kkudi Mar 13 '11 at 02:43
  • mark, there's often a set of complex logic to determine if a notification is shown. checking if it's shown is the ultimate verification that things worked correctly. the obvious need for this would be in a unit test. – Jeffrey Blattman Mar 05 '12 at 20:19
  • @farble1670: It does not change the fact that it's not possible. You can find out if the user clears it (see separate answer for `deleteIntent`), but beyond that, you need to track its state yourself, like it or not. – CommonsWare Mar 05 '12 at 20:39
  • @CommonsWare Have you looked at NotificationListenerService? It can read all system and app notifications. – IgorGanapolsky Mar 19 '15 at 21:19
  • 1
    @IgorGanapolsky: That did not exist in March 2011, when my answer was written. That being said, implementing a `NotificationListenerService` just to know whether your `Notification` is showing is silly, IMHO. – CommonsWare Mar 19 '15 at 21:21
  • @CommonsWare There is no other way, this is the only way I can think of. – IgorGanapolsky Mar 19 '15 at 21:29
  • 1
    @IgorGanapolsky: You could follow the instructions in the answer, and keep track of its state yourself. You know when you create it. You know when you get rid of it. You know when the user gets rid of it. That's enough for you to track the state of the `Notification`. – CommonsWare Mar 19 '15 at 21:46
  • @CommonsWare If the app's notifications are blocked in the system setting, then there is no way to detect it in the first place! – IgorGanapolsky Mar 19 '15 at 22:02
  • @IgorGanapolsky: Correct. And if the notifications are blocked, whether for your app specifically or for all non-priority apps, that's the user's decision to make. IMHO, the only practical `Notification` strategies for Android are those that can be oblivious to what is happening with the `Notifications` themselves (blocked for you, blocked for all apps, blocked for low-priority apps, etc.). If you have additional concerns in this area, please ask a new Stack Overflow question. – CommonsWare Mar 19 '15 at 22:06
3

Is there a way I can check programatically whether my app's notification is currently running(shown)?

Yes. Have a look on this: How to know when my notification is cleared via Clear button?

@octavian-damiean said:

It seems like you are looking for the deleteIntent field of the Notification class.

Community
  • 1
  • 1
Felipe
  • 16,649
  • 11
  • 68
  • 92
2

You can in API 23 and above. Just call NotificationManager.getActiveNotifications().

Sam
  • 40,644
  • 36
  • 176
  • 219