B4A Library NotificationListener library (NotificationListenerService)

Status
Not open for further replies.
The NotificationListener library allows you to access the device notifications.

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:
B4X:
AddApplicationText(
<service android:name="anywheresoftware.b4a.objects.NotificationListenerWrapper"
   android:label="Notification Listener"
   android:exported="false"
  android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
  <intent-filter>
  <action android:name="android.service.notification.NotificationListenerService" />
  </intent-filter>
</service>)
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:
B4X:
Sub btnEnableNotifications_Click
   Dim In As Intent
   In.Initialize("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS", "")
   StartActivity(In)
End Sub

The user will see the following screen:

SS-2013-12-12_13.06.15.png


In the service you should create a NotificationListener object and let it handle the StartingIntent:
B4X:
Sub Process_Globals
   Private listener As NotificationListener
End Sub
Sub Service_Create
   listener.Initialize("listener")
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:
SS-2013-12-12_13.08.18.png



Tips

- 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.

Updates:

- Example updated with targetSdkVersion=34 - android:exported="false" added to the service declaration and the permission to post notifications is requested.
 

Attachments

  • NotificationListener.zip
    6.4 KB · Views: 2,743
  • NotificationListenerExample.zip
    9.1 KB · Views: 184
Last edited:

webkumanda

Member
Licensed User
Longtime User
Hi,

I read whatsapp or gmail first message is ok.
but second message is "NULL"

I could not solve the problem :(

Thanks
 

magarcan

Active Member
Licensed User
Longtime User
Just a few questions. Doew it work with Android 5? Can I also capture notification icon/image?
 

magarcan

Active Member
Licensed User
Longtime User
It should work with Android 5.

You can get the package name and then use PackageManager to get the app icon. I don't see any other way to get the notification icon.
But some apps use custom notification icons (images for example) different to app icon. That's the case I'm asking for ;)
 

pesquera

Active Member
Licensed User
Longtime User
Hi,

I start 2 services plus this service on main App.. I want to allow the user to stop these 3 services and exit App
I noticed an strange behaviour occurs, after exiting the App.. if some notification is opened, then the App is loaded and the App icon displays on the notification área
I can see on the logs that inmediately after that the services are stopped, this notification service is started again.. Where can I take a look? thanks

I start/stop the services on the same order
on Main, Activity_Create
StartService(Service1) ' sms interceptor
StartService(Service2) ' GCM push
StartService(Service2) ' this notification service
on Main, Menu Option to exit
StopService(Service1) ' sms interceptor
StopService(Service2) ' GCM push
StopService(Service2) ' this notification service
 

pesquera

Active Member
Licensed User
Longtime User
Ok. thanks for the answer.. I solved it with a flag (GLO_AppIsOpened ) to exit the App :)
 

IzioSettanta

Member
Licensed User
Longtime User
hi...a question..

i try to read notification from gmail, this is the bundle:

B4X:
(Bundle) Bundle[{android.title=MARK, android.support.actionExtras={0=Bundle[EMPTY_PARCEL], 1=Bundle[EMPTY_PARCEL]},
 android.subText=mark@gmail.com, android.showChronometer=false, android.icon=2130837754, android.text=this is a sample message,
 android.progress=0, android.progressMax=0, android.showWhen=true, android.people=[Ljava.lang.String;@423f4c28,
 android.largeIcon=android.graphics.Bitmap@42207e28, android.infoText=null, android.wearable.EXTENSIONS=Bundle[mParcelledData.dataSize=1516],
 android.progressIndeterminate=false, android.scoreModified=false}]

but (only from GMAIL) when i try to get text .....this is null!!! WHY?!?!?....whatsapp, sms ..and other apps work fine! BUG?

i use this code:

B4X:
Dim jno As JavaObject = SBN.Notification
Dim extras As JavaObject = jno.GetField("extras")
        extras.RunMethod("size", Null)
            Dim title As String = extras.RunMethod("getString", Array As Object("android.title"))
            LogColor("Title = " & title, Colors.Blue)
            Dim text As String = extras.RunMethod("getString", Array As Object("android.text"))
            LogColor("text = " & text, Colors.Blue)

i use android 4.4.3 and 5.1.1
PLS HELPPPP!!!!!
 

FabioG

Active Member
Licensed User
Longtime User
Hello to all

there is a way to check if the app is already enabled on the Notification Access ?
 

wes58

Active Member
Licensed User
Longtime User
Hello to all

there is a way to check if the app is already enabled on the Notification Access ?
I am using the following code to check if the application is enabled:
B4X:
Sub CheckSettings As Boolean
    Dim ph As Phone
    Dim nstr, pstr As String
    Dim r As Reflector
    pstr = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    nstr = ph.GetSettings("enabled_notification_listeners")
    Return nstr.Contains(pstr)
End Sub
And then, open the Settings to enable/disable it:
B4X:
Sub Button1_Click
    Dim In As Intent
    In.Initialize("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS", "")
    StartActivity(In)
End Sub
 

FabioG

Active Member
Licensed User
Longtime User
I am using the following code to check if the application is enabled:
B4X:
Sub CheckSettings As Boolean
    Dim ph As Phone
    Dim nstr, pstr As String
    Dim r As Reflector
    pstr = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    nstr = ph.GetSettings("enabled_notification_listeners")
    Return nstr.Contains(pstr)
End Sub
And then, open the Settings to enable/disable it:
B4X:
Sub Button1_Click
    Dim In As Intent
    In.Initialize("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS", "")
    StartActivity(In)
End Sub

it works! thanks!
but I have to customize this code with the packagename of my app?
 

wes58

Active Member
Licensed User
Longtime User
it works! thanks!
but I have to customize this code with the packagename of my app?
pstr is the package name of your application - pstr = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")

nstr is the the string containing all applications that are enabled in Settings: nstr = ph.GetSettings("enabled_notification_listeners")

This will return true if the pstr (your package name) is part of the nstr, i.e. whether your application is enabled in Settings: Return nstr.Contains(pstr)
 

FabioG

Active Member
Licensed User
Longtime User
thanks for your reply

so, if I understand
I can change this

B4X:
pstr = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")

to

B4X:
pstr = r.GetStaticField("MY_PKG_NAME", "packageName")

right ?
 

IzioSettanta

Member
Licensed User
Longtime User
sms(text message) and whatsapp print something...gmail null, but in the "Bundle there is a text!!!

android.text=this is a sample message

bug?

i try on HTC, Samsung and other...and with a different android version 4.4--->5.1.1
 
Status
Not open for further replies.
Top