B4J Question firebase messages are not queued

Isac

Active Member
Licensed User
Longtime User
If I send more than one message with b4j to b4a the messages are not queued,
but they are overwritten.

I tried to use collapse_key but I can not solve it.


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)
    m.Put("collapse_key", "body" & Rnd(1, 1000000))
    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")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
    Log("Map"& m)
    ' Use Wait For
    Wait For (Job) JobDone(Job As HttpJob)
    Log(Job)
    If Job.Success Then
        Log(Job.GetString)
    Else
        Log($"Something went wrong: ${Job.ErrorMessage}"$)
    End If
    Job.Release
End Sub
 

Isac

Active Member
Licensed User
Longtime User
Is it possible that you are sending more than 4 messages with different collapse_key?

https://firebase.google.com/docs/cloud-messaging/http-server-ref#send-downstream

NO,

example: if I send the first message with hello and then postpone good morning
hello is overwritten by good morning.
the message should be added.




How are you showing the notification on the B4A side ?

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
Service_Create
    Log("Message arrived")
    Log($"Message data: ID ${Message.MessageId}, Data ${Message.GetData}"$)
    Log("Message="& Message)
    Dim map As Map :map.Initialize
    map = CreateMap("body2":Message.GetData.Get("body"),"title1":Message.GetData.Get("title"))
    For Each key As String In map.Keys   
    Dim value As String = map.Get(key)
    Next
    Log($"${key}:${value}"$)
    Dim jgen As JSONGenerator
    jgen.Initialize(map)
    Dim n As NB6
    mano = LoadBitmapResize(File.DirAssets, "mano.png", 24dip, 24dip, False)
    n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(mano)
    n.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), jgen.ToString, Main).Notify(4)

End Sub
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
Now I understand. @aaronk was right to request the B4A code.

You are using the same notification id (4) so you are replacing the previously shown notification.

I entered Notify(1) but it is the same

B4X:
    n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(mano)
    n.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), jgen.ToString, Main).Notify(1)
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
You must create a counter that will increase with the next message sent and put this counter in 'Notify (number)' where 'Dim number as int'. After reading the notifications, set the meter back to 1.
 
Upvote 0
Top