Android Question Notification Listener stops catching notifications

Gentry

Member
Licensed User
My current project relies on catching notifications, and frequently stops catching notifications. I discovered that to restore the functionality, I need to disable then re-enable Notification Access in the system control panel, which seems to reset the "hook" and then new notifications are captured as expected.

I haven't figured out how to cause the listener to 'break' and haven't been able to catch the failure while connected to logging. But it happens so frequently I needed to add a test feature for the user to check if it is functioning (A button creates a notification and if the app doesn't capture it, then it leads the user to re-set Notification Access).

I suspect that the Notification Internal Service is crashing, failure seems to occur when a notification that is not even being processed by the app is received by the device, and is not restarting on it's own.

Is there a way to detect the running state and force a reset of the Notification Internal Service so I don't need to bother my users to do it manually?

Here is my Listener_NotificationPosted code, but it works fine, until something "un-hooks" the listener.

B4X:
Sub Listener_NotificationPosted (SBN As StatusBarNotification)
    Dim temptitle As String
    If Starter.MyLocation.IsInitialized = False Then
        StartService(loc)
    Else If Starter.MyLocation.Latitude = 0 Then
            StartService(loc)
        End If
    NameGuesses = ""
    GeoFenceUp=False
    FenceFlag = False
    If Starter.MyLocation.Latitude = 0 Then
        StartService(loc)
    End If
    Dim p As Phone
    Dim text As String
    Dim LastAlertLocFile As RandomAccessFile
    Dim LastAlertLoc As String

    If p.SdkVersion >= 19 Then
        DateTime.DateFormat = "yyyyMMdd"
        lasttimestamp = DateTime.Now
        Dim jno As JavaObject = SBN.Notification
        Dim extras As JavaObject = jno.GetField("extras")
        extrastring = extras
        extras.RunMethod("size", Null)
        temptitle  = extras.RunMethod("getString", Array As Object("android.title"))
        If  SBN.PackageName = "com.capture.app"  Then
            title  = temptitle
        End If
        If Main.testflag And SBN.PackageName = "com.b4aexample" Then
            ToastMessageShow("Notification Captured",False)
            If isMessageTest Then
                isMessageTest = False
                Dim nb As NotificationBuilder
                nb.Initialize
                nb.Cancel(300)
                ToastMessageShow("Capture Working, All Good!", True)
                If Main.EventList.IsInitialized Then
                    Main.EventFile.WriteB4XObject(Main.EventList,0)
                    Main.EventFile.flush
                End If
            Else
                title  = extras.RunMethod("getString", Array As Object("android.title"))
                address = extras.RunMethod("getString", Array As Object("android.text"))
                currentLoc.Initialize(address,Me,"New_Event")
            End If
            Main.testflag = False
        End If
        If  SBN.PackageName = "com.capture.app"  Then
            u.tlog2("L_NP "&SBN.PackageName & " " & extrastring,5)
            title  = extras.RunMethod("getString", Array As Object("android.title"))
            address = extras.RunMethod("getString", Array As Object("android.text"))
            currentLoc.Initialize(address,Me,"New_Event")
        End If
 
    End If

End Sub

Edit: reposting in the correct forum, I hope. Guess its not a "bug".

Thanks for looking.
Gentry
 

Gentry

Member
Licensed User
When my app starts up, I see "Notification internal service created" but this log isn't in my code. What seems to be happening is the component that should trigger the "NotificationService" Service and raises "Sub Listener_NotificationPosted" when a new notification is received fails and does not re-start.

I'm not sure what to wrap with the try catch, if listener_notificationPosted stops getting called, putting try-catch in there doesn't seem like it would make a difference. Unless the concept is that code in Listener_NotificationPosted is crashing the NotificationService activity, and then it doesn't restart until the Notification Access is turned off and then on again. My understanding is that when a new notification is fired, the NotificationService should be re-started if it for some reason was not already running.

I'll wrap some stuff in try-catch, see if I can catch any errors im not seeing otherwise.

thx.
Gentry
 
Upvote 0

DonManfred

Expert
Licensed User
of this library that is implemented differently
i guess it has something to do with the way b4a and android are communicating?
May you want to share the java-code as an Example to see the implementation?
 
Upvote 0

Gentry

Member
Licensed User
I tried NotitficationListener 2.0 by simply replacing it, It does not have .HandleIntent(StartingIntent) and my code crashes with that check commented out.
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
May you want to share the java-code as an Example to see the implementation?
The java code is available in that thread.

I tried NotitficationListener 2.0 by simply replacing it, It does not have .HandleIntent(StartingIntent) and my code crashes with that check commented out.
The code should be the same or very similar. See the example project.
 
Upvote 0

DonManfred

Expert
Licensed User
Upvote 0

Gentry

Member
Licensed User
The code should be the same or very similar. See the example project.

In the 1.20 example, there was this line:

Sub Service_Start (StartingIntent As Intent)
If listener.HandleIntent(StartingIntent) Then Return
...
End Sub

Which seems like it is a filter to drop intents that are not for the NotificationListener to handle.

The 2.0 example does not include this filter. and the object does not have this method defined. When I comment it out of my code, the app crashes with this:

B4X:
*** Service (notificationservice) Create ***
Error occurred on line: 37 (NotificationService)
java.lang.ClassCastException: com.gosiruz.ua.notificationservice cannot be cast to anywheresoftware.b4a.objects.NotificationListenerWrapper
    at anywheresoftware.b4a.objects.NotificationListenerWrapper$NotificationListener.Initialize(NotificationListenerWrapper.java:63)
    at com.gosiruz.ua.notificationservice._service_create(notificationservice.java:1118)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at com.gosiruz.ua.notificationservice.onCreate(notificationservice.java:55)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3297)
    at android.app.ActivityThread.-wrap5(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1618)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6316)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
** Service (notificationservice) Start **

Line 37 is the listener.initialize("listener") in service_create

Using B4A 7.80. I also cleaned the project.
 
Upvote 0

Gentry

Member
Licensed User
I missed the #Extends change, which I've not added and my NotificationService starts and doesn't raise error, but now doesn't catch the listener_NotificationPosted.

And of course, the example compiles and tests as working.

Now I compare with the example, see what additional detail I may have overlooked.
 
Upvote 0

Gentry

Member
Licensed User
Of course, the Manifest code strikes again! Hopefully NL 2.0 doesn't suffer from the disconnect issue I was experiencing.

Thanks!
 
Upvote 0

Gentry

Member
Licensed User
I'm afraid the disconnect issue is still present with the new 2.0 NotificationListener library.

Once the listener hook is 'disconnected' killing and restarting the app, even re-deploying the app thru B4A does not fix the condition. Only turning Notification Access for the app off, then on again restores the listener functionality.

Once the hook is disconnected, I send qualified notifications, but the _listenerposted event does not fire, and I do not see anything in the unfiltered logs that indicates the fail.

I am having trouble causing the crash condition, it seems to happen randomly, and when I am mobile testing the app, so not catching it in adb log.

Here is the unfiltered log snip showing when I press a button to create a test notification and the timer event that I use to display the notification access screen to ask the user to turn in off then on again.

B4X:
ViewRoot's Touch Event : ACTION_DOWN
ViewRoot's Touch Event : ACTION_UP
listeners=com.gosiruz.ua/com.gosiruz.ua.notificationservice
test_message = If this message stays up, re-enable your notification monitor.
Set Notification for=Test Message - If this message stays up, re-enable your notification monitor.
Action2 Tag set to accepted
Action2 Tag set to ignore
Notservice CaptureTest_Tick
Timeline: Activity_launch_request intent: act=android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS pkg=null time:28660304
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Class not found: b4a.example.customlistview, trying: com.gosiruz.ua.customlistview
_Create_Menu Start
_Create_Menu Start
Timeline: Activity_idle id: android.os.BinderProxy@82805ae time:28664796
** Activity (main) Pause, UserClosed = false **
 
Upvote 0

Gentry

Member
Licensed User
Yes, I put try-catch around everything risky(all javaobject calls) in the NotificationService service, but nothing has been caught yet.

It seems that the "disconnect" with the new library is more severe. In order to reset it, I have to completely reboot the device. with the 1.2 version just turning the Notification Access off and back on was sufficient to bring it back to life. I also tried un-installing and re-installing the app, but that was not sufficient to reset the hook.

Hopefully this is a clue as to where the disconnect is happening. It seems to be breaking outside of the app entirely.
 
Upvote 0
Top