Android Question Firebase doesn't work on all devices

birnesoft

Active Member
Licensed User
Longtime User
month before it worked, but now the messages do not arrive when application is closed.
on my Huawai it works but on ONEPlus 3t and Gigaset it doesn't.

Starter

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

Sub Process_Globals
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    StartService(FirebaseMessaging)
End Sub

Firebase

B4X:
#Region  Service Attributes
    #StartAtBoot: true
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
    fm.SubscribeToTopic("2")
End Sub

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

Sub fm_MessageArrived (Message As RemoteMessage)
    ToastMessageShow("ok",False)
    Log ("ok")
End Sub



Manifest

CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
 

birnesoft

Active Member
Licensed User
Longtime User
B4X:
SendMessage("2","test","test",Me)

Sub SendMessage(Topic As String, Title As String, Body As String,sendy As Object)
    Dim Job As HttpJob
    Job.Initialize("fcm",sendy)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    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
 
Upvote 0

birnesoft

Active Member
Licensed User
Longtime User
Thanks for your answer.
I set #StartAtBoot: false
but it seems this was not the problem.

In the original program normally it shows a notification. I cut the program to find the defect.
I tested on 8 different devices, but it works only on 50% of them.
 
Upvote 0

birnesoft

Active Member
Licensed User
Longtime User
We solved the problem on the onePlus 3T:
in batterie settings we disabled advanced optimization.

But on the other devices it still doesn't work.

here is the test project: ...

thanx
 

Attachments

  • FireBaseTest.zip
    11.1 KB · Views: 126
Upvote 0

birnesoft

Active Member
Licensed User
Longtime User
Thank you Erel,
I made it like in the example. But it doesn't work on two of my devices. (Gigaset /Huawai 20pro)

main
B4X:
Sub Activity_Click
    SendMessage("general","tity","body",Me)
End Sub

Sub SendMessage(Topic As String, Title As String, Body As String,sendy As Object)
    Dim Job As HttpJob
'    Log("tttttttt:"&Topic)
    Job.Initialize("fcm",sendy)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    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

Starter
B4X:
#Region  Service Attributes
    #StartAtBoot: true
    #ExcludeFromLibrary: True
#End Region
Sub Process_Globals

End Sub

Sub Service_Create
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
End Sub


FirebaseMessaging
B4X:
#Region  Service Attributes
    #StartAtBoot: false
#End Region
Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
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
 

Attachments

  • FireBaseTest.zip
    9.6 KB · Views: 130
Upvote 0
Top