Android Question Firebase Messaging - FCM console works, not the B4J-SendMessage tool

mmieher

Active Member
Licensed User
Longtime User
Sending notifications from the FCM console is now working after a friend straightened me out earlier.

Having trouble creating a notification all by myself. Everything looks groovy in the B4J log, but the device never gets the message.
B4X:
Sub AppStart (Args() As String)
    SendMessage("general", "mis title", "body slam")
    StartMessageLoop
End Sub

Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Log("Topic:  " & Topic)
    Log("Title:  " & Title)
    Log("Body:   " & Body)
    
    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
    
    '    neither seems to matter
    m.Put("android", CreateMap("priority": "high"))        '    Erel
    ''m.Put("android", CreateMap("priority": 10))        '    Marc - what am i thinking?
    
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    
    Log("POST:  " & jg.ToString)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    
    ''Log("using API_KEY" & CRLF & API_KEY)
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
    
End Sub


Sub JobDone(job As HttpJob)
   Log("JobDone:  " & job.Success)
   If job.Success Then
     Log(job.GetString)
   End If
   job.Release
   ExitApplication '!
End Sub

Log:
B4X:
Waiting for debugger to connect...
Program started.
Topic:  general
Title:  mis title
Body:   body slam
POST:  {"data":{"title":"mis title","body":"body slam"},"android":{"priority":"high"},"to":"\/topics\/general"}
JobDone:  true
{"message_id":4806193666824373742}
 

KMatle

Expert
Licensed User
Longtime User
This is my sub to send to a topic.

B4X:
 Private Sub SendMessageToTopic(Top As String, MyData As String)
 
   Dim Job As HttpJob
   Job.Initialize("SendMessage", Me)
  
    Dim m As Map = CreateMap("to": $"/topics/${Top}"$)
      
    Dim data As Map = CreateMap("md": MyData)
    Dim pri As Map=CreateMap("priority":"high")
    m.Put("data", data)
    m.Put("priority", "high")
    m.Put("android", pri)
    
 
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.Tag=jg
    'Log(jg.ToString)
    Log(jg.ToPrettyString(2))
    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)
  
    Wait For JobDone(Job As HttpJob)
    If Job.Success Then
        Dim RES As String = Job.GetString
        Log(RES)
        Dim parser As JSONParser
        parser.Initialize(RES)
    End If
  
  
  
  End Sub
 
Upvote 0

mmieher

Active Member
Licensed User
Longtime User
Thanks, Erel. I get a big fm.token thing that I do not know what to do with.

I am confused by all of this. I get notifications when using Firebase console even when my App is not "running" and I temporarily removed the n.notify(1) from fm_MessageArrived in my B4A app?
 
Upvote 0

mmieher

Active Member
Licensed User
Longtime User
Firebase Console uses a different type of notification messages.

Make sure that targetSdkVersion is set to 30 and not 31.
It is set to 30. I probably need to download updates for the SDK, but I see conflicting messages about whether or not I should ever use the B4A IDE to do so. My personal experience is that using the IDE to update the SDK messes other things up to the point I have to restore c:\Android from a backup.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…