Android Question Firebase notification after reboot

marcick

Well-Known Member
Licensed User
Longtime User
  1. Is it normal that after a reboot of the phone I don't receive firebase notifications ? (I feel not).
  2. Is it normal that if I kill the app (or the OS do it I suppose) I don't receive firebase notifications ? (I feel not).
  3. Maybe the Starter service [where I have CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")] need to have #StartAtBoot: true ?
 

KMatle

Expert
Licensed User
Longtime User
1. No. It will take some secs until the Android system services start but the notofication is received

2. No. It (=the Service) will be automatically started when a message arrives in ALL cases. No extra code needed!

3. No. You only need to subscribe ONCE. The rest is handled by Android (= the service is called when a message arrives)

#StartAtBoot: true

NO way!

Q:

How do you send the messages (see code below)
What is the return code of the FCM api?
Did you set your app to be able to rund in background (Phone energy settings -> allow to run when screen is turned off)?

Example:

B4X:
Response from server: {"multicast_id":xxxxxxxxxxx,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1523558915376964%3df52d23f9fd7ecd"}]}



Just tested that (again)

Code (receive)


B4X:
Public Sub Subscribe
    'you can subscribe to more topics
    fm.SubscribeToTopic("nothing")
    Log(fm.Token)
End Sub


Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub

Code (send)

B4X:
Private Sub SendMessageToSingleDevice(Devtoken As String, MyData As String)
 
   Dim Job As HttpJob
   Job.Initialize("SendMessage", Me)
 
   Dim m As Map = CreateMap("to": $"${Devtoken}"$)
   Dim data As Map = CreateMap("data": MyData)
   m.Put("data", data)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.Tag=jg
   Log(jg.ToString)
   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
 
  Private Sub SendMessageToTopic(Top As String, datastring As String)
 
   Dim Job As HttpJob
   Job.Initialize("SendMessage", Me)
 
   DateTime.DateFormat="dd.MM.yyyy"
   Dim DatumZeit As String
   DatumZeit=DateTime.Date(DateTime.Now)
   DatumZeit=DatumZeit & " " & DateTime.Time(DateTime.Now)
 
 
    Dim m As Map = CreateMap("to": $"/topics/${Top}"$)
   Dim noti As Map = CreateMap("body":"Notification " ,"title":"New message")
   Dim data As Map = CreateMap("data": datastring)
   m.Put("notification", noti)
   m.Put("data", data)
   m.Put("content_available": True)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.Tag=jg
   Log(jg.ToString)
   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

Nothing more is needed!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
3) NO! NEVER set StartAtBoot in the Starter Service! Use another Service if you need start at boot-functionality. Set start at boot in this service.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is it normal that if I kill the app
There are devices which don't allow your app to be started from a push notification after the user kills it with a swipe.

I don't know which version of B4A you are using however there were several improvements in B4A v8 in the way apps start in the background.
 
Upvote 0
Top