Android Question Firebase Messaging send to specific user

amer bashar

Member
Licensed User
Hi all,
According to this instructions when I`m using B4J code to send notification to specific user, I put the FCM_Token of the user instead of topic :
B4X:
Send("fHt-0W4FSUu0ggRCfZac3A:APA91bFkFiLWho .. to the end of token", "title", "body")
but it returned with this error :
B4X:
ResponseError. Reason: , Response: {
  "error": {
      "code": 400,
          "message": "Request contains an invalid argument.",
              "status": "INVALID_ARGUMENT",
                  "details": [
                        {
                            "@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
                            "errorCode": "INVALID_ARGUMENT"
                        },
                        {
                        "@type": "type.googleapis.com/google.rpc.BadRequest",
                        "fieldViolations": [
                        {
                            "field": "message.token",
                            "description": "Invalid registration token"
Please can you help me either sending through b4j to FCM_Token or using the php right way?
 

amer bashar

Member
Licensed User
I found the solution (must replace "topic" with "token" :
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String) As ResumableSub
    Dim Job As HttpJob
    Job.Initialize("", Me)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    Dim message As Map = CreateMap("token": Topic, "data": data)
    If Topic.StartsWith("ios_") Then
        'B4i
        Dim Badge As Int = 0
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body)
        message.Put("notification", iosalert)
        message.Put("apns", CreateMap("headers": _
            CreateMap("apns-priority": "10"), _
            "payload": CreateMap("aps": CreateMap("sound":"default", "badge": Badge))))
    Else
        'B4A
        message.Put("android", CreateMap("priority": "high"))
    End If
    Dim jg As JSONGenerator
    jg.Initialize(CreateMap("message": message))
    Log(jg.ToPrettyString(4))
    Job.PostString($"https://fcm.googleapis.com/v1/projects/${ProjectId}/messages:send"$, jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "Bearer " & Token)
    Wait For (Job) JobDone(Job As HttpJob)
    If Job.Success Then
        Log(Job.GetString)
    End If
    Job.Release
    Return True
End Sub
But now the problem is the notification not shown when the app is background, although it does when using php to send
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 1
Top