For most of the history of Appium, it has not been possible to inspect notifications received on an Android device, especially the "status bar" notifications you can only look at by pulling them down from the status bar. To automate verification of notifications received, the brute-force approach meant navigating to this notifications view and hoping to get lucky reading the text elements on that page. More recently, the Appium team figured out a way to read notifications via the Appium Settings helper app. This is a little app which is installed alongside an Appium session, and facilitates device-level functionality that is not possible merely through UiAutomator2 or Espresso.
Basically, the Appium Settings app can be granted the permission to read any incoming notifications. It can then save these notifications for retrieval by the main Appium process using ADB. All of this complexity is wrapped up in one Appium command (first released in Appium 1.16):
For this command to work, the Appium Settings app must of course be granted notification read permissions. This can be done manually in the "Notification Access" area of your Android system settings (the precise way to navigate to this view differs based on device). Or, you could automate it using Appium as part of your device provisioning steps.
At the moment, the return value for this method is a bit complex. In JSON, it would look something like this:
But of course, in the Appium Java client, we will not get a JSON response, so it will be our job to cast the response appropriately:
And we'll need to keep casting as we navigate through the object. Basically, we have a main key statusBarNotifications, which returns an array of notification objects. Each of these notification objects itself has a lot of metadata associated with it (you can use this metadata to filter by the package ID of your app, for example). The notification key of this object has the main content we're looking for, including the title and the text of the notification.
This technique is especially powerful because, having granted the special permission to Appium, you can read notifications for any app on the device.
That's it! Check out a full example where we walk through each notification received since Appium started, and print it out to the console, making sure we appropriately choose the type of notification text and title which has content: