B4A Library NotificationListener library (NotificationListenerService)

Not open for further replies.
The NotificationListener library allows you to access the device notifications. This is a new feature which is only available on Android 4.3+ (api 18+).

With this library you can listen to new notifications and removed notifications.

You can also clear existing notifications.

There are some steps that you need to follow in order to use this feature:
- Make sure that the IDE references android.jar from API level 18+.
- Download the attached library and copy it to the libraries folder.
- Add a Service module named NotificationService (must be this exact name).
- Add the following code to the manifest editor:
<service android:name="anywheresoftware.b4a.objects.NotificationListenerWrapper"
   android:label="Notification Listener"
  <action android:name="android.service.notification.NotificationListenerService" />
You can change the value of android:label.

- The user must enable your app before it can listen to notifications. This is done by sending the following intent:
Sub btnEnableNotifications_Click
   Dim In As Intent
   In.Initialize("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS", "")
End Sub

The user will see the following screen:


In the service you should create a NotificationListener object and let it handle the StartingIntent:
Sub Process_Globals
   Private listener As NotificationListener
End Sub
Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
   If listener.HandleIntent(StartingIntent) Then Return
End Sub
The following events will be raised: NotificationPosted and NotificationRemoved.

See the attached example. It extracts data out of the notifications and print it to the logs:

Note that only in Android 4.4 you can extract the title and other values (extracted with JavaObject).


- The notifications will also be handled when your app is not running. The service will first be created.
- There is no way to check whether the app is enabled or not.
- If your service is configured incorrectly when you enable the app then nothing will work and there will be no error message. Even after you reinstall the app. In that case you should either change the app package name or restart the device.
- You can also reach the settings screen by going to Settings > Security > Notification Access.


  • NotificationListener.zip
    6.4 KB · Views: 2,441
  • NotificationListenerExample.zip
    8.7 KB · Views: 1,209
Last edited:
Not open for further replies.