Home » Android » java – Can I test status bar notifications using Android's testing framework?

java – Can I test status bar notifications using Android's testing framework?

Posted by: admin April 23, 2020 Leave a comment


I have a class that sends a status bar notification in Android. I can’t find a way to test whether the notification was sent or not, which makes it very hard to write any kind of useful unit test.

Does anyone have a solution for this?

How to&Answers:

Maybe not the answer you are looking for, but as usual the solution is to:

  • Create an interface abstracting the notification functionality.
  • Create a default implementation that delegates to the system notification API.
  • When running tests replace (or decorate) the default implementation with a mock implementation that supports testing.

This can be simplified with the following technology:

  • Dependency injection frameworks (such as RoboGuice) to streamline the choice of implementation.
  • Mocking library (such as EasyMock) to automate creation of mock implementations.


Try to create a Mock object by extending NotificationManager and override the notify() methods. The overridden functions can be asserted. In your test case(s), inject the Mock to the subject activity and run the tests using Android JUnit Test.


Robotium cannot interact with the notification bar. You are restricted to one application. Check FAQ for robotium, there is mention of this:


UI Testing

This is what you want I think. The uiautomator.

Works only on Android 4.1 or above. You need run the sample code first maybe.

You can write a piece of code to perform the pull-down-status-bar action and then do your things. This is only UI testing. You can’t test your data with uiautomator.

Hope this would help.


Solved a similar kind of situation by storing notification detail in data store before sending and marking as acknowledged whenever user enters into application through notification bar.
As robotium is restricted to application boundaries it can’t access system data hence tested the behavior using junit/jmockit based unit test.


You can use an emulator, just for these kind of tests, with Android Marshmallow (api 23).

For example:

 public void testCheckNotification_2() throws IOException, InterruptedException, GcmException
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        GcmResponse gcmResponse = endPointImp.sendGcmMulticastRequestImp(getGcmMultiRequest());
        assertThat(gcmResponse.getSuccess(), is(1));
        assertThat(mManager.getActiveNotifications().length, is(1));
        StatusBarNotification barNotification = mManager.getActiveNotifications()[0];
        assertThat(barNotification.getId(), is(INCIDENCIA.getTitleRsc()));

        // We check the pending intent.
        PendingIntent pendingIntent = barNotification.getNotification().contentIntent;
        assertThat(pendingIntent.getCreatorPackage(), is(GcmRequest.PACKAGE_DIDEKINDROID));

The important part is the call to the NotificationManager (mManager) to check that it has active notifications after sending a multicast message to Google FCM http endpoint.


Check out NotificationListenerService:

“A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.”



Just to save others some time: a thing that looks like it should detect notifications, but doesn’t, is trying to create a PendingIntent with the NO_CREATE flag.

Since attempts to create a PendingIntent with the same values as an existing notification, but with the NO_CREATE flag, should return null, it seemed that would tell whether a notification already had one or not.

Sadly, this doesn’t seem to be reliable.