Android Question Firebase Notification need instant receive the messages.

tzfpg

Active Member
Licensed User
Longtime User
My app need instant receive the notification when server sent out the messages, how can I do that?

My app already add the gps location method to not let system kill my app, but sometimes devices sleep too long, devices can't receive the message.

server side send notification code:

B4X:
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}"$)
    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

I refer the link from firebase website https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages and https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidmessagepriority

How to add the ttl, notification_priority and AndroidMessagePriority to server send notification code?

Thank you.
 

tzfpg

Active Member
Licensed User
Longtime User
B4X:
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}"$, "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

if I change like this, is it IOS notification also set to high priority?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…