Android Question android.app.ActivityThread$H.handleMessage android 8

joaomoraes83

Member
Licensed User
Hi

I'm having trouble using firebase messaging with android 8.

The following error happens frequently only on android 8:

Fatal Exception: android.app.RemoteServiceException

error message captured in Firebase Crashlytics:

Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1802)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)

code b4a:
B4X:
#Region  Service Attributes
    #StartAtBoot: False   
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
'    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n.Notify(1)
End Sub

Sub Service_Destroy

End Sub

Any help will be welcome
 

joaomoraes83

Member
Licensed User
This code looks correct. Do you have other services? Do you start other services?

In the Starter module:

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals

End Sub

Sub Service_Create
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
    Dim MobileAds As MobileAds
    MobileAds.Initialize
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0

joaomoraes83

Member
Licensed User
Are there any other services?

thanks for listening

I don't use other services, just this code.

b4j "firing" code follows

B4X:
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private const API_KEY As String = "**************************************************"
   
End Sub

Sub AppStart (Args() As String)
    SendMessage("general", "*************************")
    StartMessageLoop
End Sub

Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$,"restricted_package_name": "com.paginss1", "priority": "high")
    Dim data As Map = CreateMap("title": Title, "body": Body)
    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
        m.Put("priority", 10)
    End If
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub


Sub JobDone(job As HttpJob)
   Log(job)
   If job.Success Then
     Log(job.GetString)
   End If
   job.Release
   ExitApplication '!
End Sub

I reiterate that the errors appear only on devices with android 8 that receive the message
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I don't use other services, just this code.
So in fact this is a B4J Question?
In Subject and #1 you are talking about a B4A App!

Notifications work on Android 5 and above. It works for any Androidversion on my side. Up to Android 11.

The B4A-Implementation (AND the used SDK) is important.
Looks like you did not implement it correctly in your B4A App.
 
Upvote 0

joaomoraes83

Member
Licensed User
So in fact this is a B4J Question?
In Subject and #1 you are talking about a B4A App!

Notifications work on Android 5 and above. It works for any Androidversion on my side. Up to Android 11.

The B4A-Implementation (AND the used SDK) is important.
Looks like you did not implement it correctly in your B4A App.

hello Don

follow the tutorial that I followed


what can i have done wrong?
 
Upvote 0
Top