B4J Question [Solved] Send Firebase Push Notifications to 2023+ to tokens

schimanski

Well-Known Member
Licensed User
Longtime User
I have to send push notifications to each token, not to topics. The old code works, bur under the new one, i make something wrong.
Does anyone have experience with this? Thanks for help.

B4X:
Public Sub SendMessageTo(Devices As List, msg As Map)
    ...
        Dim m As Map = CreateMap("registration_ids": Devices)
        
   
        m.Put("data", msg)

        m.Put("android", CreateMap("priority": "high"))

        Dim jg As JSONGenerator
        jg.Initialize(CreateMap("message": m))
        
        Job.PostString($"https://fcm.googleapis.com/v1/projects/${Main.ProjectId}/messages:send"$, jg.ToString)
        Job.GetRequest.SetContentType("application/json;charset=UTF-8")
        Job.GetRequest.SetHeader("Authorization", "Bearer " & Token)
  
   .....

B4X:
2024-03-28 18:42:56.336 [PushServer] INFO - 1 Android-Geraete werden gepusht!
ResponseError. Reason: , Response: {
  "error": {
    "code": 400,
    "message": "Invalid value at 'message.data[3].value' (TYPE_STRING), 62\nInvalid value at 'message.data[7].value' (TYPE_STRING), 1711647776324\nInvalid JSON payload received. Unknown name \"registration_ids\" at 'message': Cannot find field.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "message.data[3].value",
            "description": "Invalid value at 'message.data[3].value' (TYPE_STRING), 62"
          },
          {
            "field": "message.data[7].value",
            "description": "Invalid value at 'message.data[7].value' (TYPE_STRING), 1711647776324"
          },
          {
            "field": "message",
            "description": "Invalid JSON payload received. Unknown name \"registration_ids\" at 'message': Cannot find field."
          }
        ]
      }
    ]
  }
}
2024-03-28 18:42:57.485 [AndroidPushFirebase] INFO - Android-Geraet(e) konnten nicht gepusht werden:{
  "error": {
 

DonManfred

Expert
Licensed User
Longtime User
You need to have a list of firebaseregistrationtoken, not numberic ids.
Don´t know how the json must bea build to send to multiple token though.

Check the Documentation.


firefox_zxFlPZBZqy.png


The solution is to send one notification to each registrationtoken.
A batch-send to multiple devices is going to be shutdown soon.
 
Upvote 1

schimanski

Well-Known Member
Licensed User
Longtime User
Sorry for the late reply, had little time. But here again the completeness: To send push notifications to tokens instead of topics under 2023+, simply change the line

B4X:
Dim message As Map = CreateMap("topic": Topic, "data": data)

to

B4X:
Dim message As Map = CreateMap("token": Devices.Get(x), "data": data)

in Erel's push-example. After that, as DonMafred said, send one notification to each token.
 
Upvote 0
Top