B4A Library NotificationListener library (NotificationListenerService)

Status
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:
B4X:
AddApplicationText(
<service android:name="anywheresoftware.b4a.objects.NotificationListenerWrapper"
   android:label="Notification Listener"
  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


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

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.
 

Attachments

  • NotificationListener.zip
    6.4 KB · Views: 2,471
  • NotificationListenerExample.zip
    8.7 KB · Views: 1,236
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
Here's the case:
I use CallLog.GetSince 8:00. There's no missed call. At 8:05 I have a missed call. CallLog.GetSince will return 1 missed call. At 8:10, I clear the notification and the missed call will be delete but CallLog.GetSince 8:00 still return 1 missed call. I need to run CallLog.GetSince 8:11 or so to get 0 missed call. That's why I need to use NotificationListener to know when the missed call was cleared. I don't know a better way to do it.

I forgot to mention, I use a service to run Callog.GetSince every 30 seconds.
 

pesquera

Active Member
Licensed User
Longtime User
Hi, I've using this wonderful library from a long time and working very good for catching whatsapp messages

Now, I'm having an issue that is intermittent.. sometimes the notification message is processed again, and sometimes is for several instances
After processing a notification I'm clearing it with ClearNotification(SBN)

Seems that the event Listener_NotificationPosted is fired again, without any income notification.. the same notification is processed again
How can I find the problem?

I've tested it with Android 6 and Android 5

the code that I'm using is that posted on #32
NotificationListener library (NotificationListenerService)


EDIT: I was thinking about some last code changes.. I conditionally do start the NotificationService, if the user profile has rights for whatsapp message processing
I have this on the starter service: (Service_Create)
B4X:
If loc_Perfil <> "Cliente/Paciente" And _
    ph.SDKversion >= 19 And _  ' Android 4.4+
    Not(loc_AHPManager.GetBoolean("miParam_App_Closed"))
        StartService(NotificationService)
End If
Could this be related?


and, I'm not sure.. is this right?
NotificationService
B4X:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Starter
B4X:
#Region  Service Attributes
   #StartAtBoot: True
   #ExcludeFromLibrary: True
#End Region
 
Last edited:

pesquera

Active Member
Licensed User
Longtime User
Thanks Erel, just another question:
I'm proccesing the messages with this line:
B4X:
Dim lines() As Object = extras.RunMethod("getCharSequenceArray", Array("android.textLines"))
   If lines<>Null Then
      For Each line As Object In lines
'         catch the message from the line
'         < ---------------  I want to delete the message catched here
I noticed that the message is queued and procceced again when another message is coming
It's possible to delete the line from that array, after catched?
 

deantangNYP

Active Member
Licensed User
Longtime User
May i know if its possible to wake up the screen and vibrate when Notification comes in (btnCreateNotifications_Click)?
 

Neojoy

Member
Licensed User
Longtime User
Hi, I've using this library, but NotificationListener stops after some times and show this error "java.lang.RuntimeException: Object should first be initialized (StatusBarNotification)".

When it happens my app crashes and go away

Any idea?
 

Neojoy

Member
Licensed User
Longtime User
Please check the logs and post the full error message.

This error occurs only on device, this is the only error message that I get, using Try and catch.

Is there another way to get full error message on device?
 

Neojoy

Member
Licensed User
Longtime User
Is there an way to close NotificationListener interface after turn on or bring to front my activity?
 

Neojoy

Member
Licensed User
Longtime User
Which interface are you asking about?

interface that turn on NotificationListener when I call "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"
I would like to force it disappear and back to my previous screen without press back button
 
Last edited:

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hello to all,
I should intercept the sending of an email and be able to read the email (recipient, object, and content) data to save the data to a table in the App.

Can anybody help me?

thank you
 

beelze69

Active Member
Licensed User
Longtime User
H
Thanks Erel, just another question:
I'm proccesing the messages with this line:
B4X:
Dim lines() As Object = extras.RunMethod("getCharSequenceArray", Array("android.textLines"))
   If lines<>Null Then
      For Each line As Object In lines
'         catch the message from the line
'         < ---------------  I want to delete the message catched here
I noticed that the message is queued and procceced again when another message is coming
It's possible to delete the line from that array, after catched?
Hi!,

Suppose I need to check if the WhatsApp message has come from a particular Number and if that message has an attachment and then forward that attachment and the message automatically to specific email-id, how can that be done ?

Thanks
 
Status
Not open for further replies.
Top