How to Check Which Notifications Are Active in Status Bar in Android Dev

How to check which notifications are active in status bar in Android Dev?

I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see
the notification in the status bar?).

How can I do this?

You can't, sorry. Update: Now possible with Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()

However, you can always simply cancel() it -- canceling a Notification that is not on-screen is perfectly fine. Conversely, you can always safely call notify() again for the same Notification, and it too will not cause a problem if the Notification is already on-screen.

EDIT:

NotificationManager.getActiveNotifications() was added in API 23 if you don't want to use the NotificationListenerService

How to detect when the notification/system bar is opened

Before I big with the implementation, I will give a brief explanation of my (very hacky) logic. When an Activity is no longer visible to the user for any reason, onWindowFocusChanged(..) gets invoked. However, onStop() only gets invoked when the Activity is no longer visible to the user by going to the background. I noticed that when switching Activities, onStop() is always invoked after onWindowFocusChanged(..), so I added a check in onWindowFocusChanged(..) to see if onStop() had already been invoked (with a 1 second delay), and I did this using the static member. Now for the how-to...

You will need a parent Activity that all the Activities in your app extend. In this parent Activity, add this static member:

private static boolean wasOnStopCalledAfterOnWindowFocusChanged;

Then in your onStop() method, add this line, make sure you invoke it BEFORE super.onStop()

@Override
protected void onStop() {
wasOnStopCalledAfterOnWindowFocusChanged = true;
super.onStop();
}

Finally, you need to override onWindowFocusChanged(..) in this parent Activity, and add in the below logic.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus) {
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if (!wasOnStopCalledAfterOnWindowFocusChanged) {

// NOTIFICATION BAR IS DOWN...DO STUFF

}
wasOnStopCalledAfterOnWindowFocusChanged = false;
}
}, 1000);
}
}

Android / Reading with all of active status bar notifications

You can use getActiveNotifications() to get all active notifications which is part of the NotificationListenerService class.
The documentation is very clear about this method:

Request the list of outstanding notifications (that is, those that are visible to the current user). Useful when you don't know what's already been posted.

Check if my application's notification is running

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.



Related Topics



Leave a reply



Submit