Android Question B4J auth.Any and the FCM Registration Token

mmieher

Active Member
Licensed User
Longtime User
I am trying to write a simple B4J desktop program to manage sending Firebase notifications to the devices using my App.

Everything seems to be working on the B4A receive-end thanks to your help.

However, I am having much difficulty getting the correct FCM Registration Token. The token returned by my Google Account sign on just ain't good enough.
B4X:
' edited
ResponseError. Reason: , Response: {
  "error": {
    "code": 400,
    "message": "The registration token is not a valid FCM registration token",
    "status": "INVALID_ARGUMENT",
        "@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
        "errorCode": "INVALID_ARGUMENT"

This seems to work just fine:
B4X:
Private Sub GoogleSignOn As ResumableSub
    LogSub("GoogleSignOn")
    
    Dim dirToken As String = File.DirData("GoogleAuth")
    Dim Scope As String = "https://www.googleapis.com/auth/firebase.messaging"
    
    oauth2.Initialize(Me, "oauth2", ClientID, Scope, ClientSecret, dirToken)
    ''oauth2.ResetToken        '    for debugging
    oauth2.GetAccessToken
    
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, rToken As String)
    Log("oAuth2 Success = " & Success)
    If Success Then
        Token = rToken
        LogColor("GoogleSignon SUCCESS!", clrApp)
        LogColor("new token = " & Token, clrApp)
    Else
        LogError("GoogleSignOn Success FALSE")
        oauth2.ResetToken
        Token = ""
    End If
    
    Return Success
    
End Sub

...
GoogleSignon SUCCESS!
new token = ya29.A0ARrdaM8iKn2rhB-3VrAM7k0pxM0iPWcf1cLxDyeQwLA8MVmwhhM32cU9OlJlvu1Cpp_smGUWzW3qQR0JnzHa2nXeltQFEoZw5bE75Xp23qERrPv-T6QkzCPzFJcUjghMIppJwxwP0E2E9_0FULajvunMuKa5AA

I would think that https://www.googleapis.com/auth/firebase.messaging would get me to the right place.

I have tried to mess around with this. I use it in the B4A app, but cannot find a FirebaseAuth lib that works in B4J:
https://www.b4x.com/android/forum/threads/b4x-b4xpages-firebasepush-firebaseauth.122477/#content

I have triple-sight-verified that the Server Key is correct and that the FCM API stuff is enabled in the G-Cloud. I also get a nice explanation of the requested permission from Mr. Google in a newly-opened browser.
 

mmieher

Active Member
Licensed User
Longtime User
Oops. This might help:
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String) '  ignore
    LogSub("Topic:  " & Topic & "     Title:  " & Title & "     Body:  " & Body)
    
    If Token.Length = 0 Then
        LogError("BAD TOKEN")
        Wait For cum
    End If
    
    Dim threeMap As Map
    threeMap.Initialize
    threeMap.Put("title",Title)
    threeMap.Put("body",Body)
    ''Log(threeMap) 'ignore
    
    Dim twoMap As Map
    twoMap.Initialize
    twoMap.Put("notification", threeMap)
    twoMap.Put("token", Token)
    ''Log(twoMap) 'ignore
    
    Dim mapTop As Map
    mapTop.Initialize
    mapTop.Put("message",twoMap)
    ''Log(mapTop) 'ignore
    
    ''DisplayMap(mapTop)
    ''    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
    
    Dim jg As JSONGenerator
    jg.Initialize(mapTop)
    Log(jg.ToPrettyString(4))
    
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    
    Dim jUrl As String = "https://fcm.googleapis.com/v1/projects/" & PROJECT_ID & "/messages:send"
    ''Log("jUrl = " & jUrl)
    
    Job.PostString(jUrl, jg.ToString)
    ''Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetContentType("application/json")
    
    Job.GetRequest.SetHeader("Authorization", "Bearer " & Token)
''    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
    
End Sub
 
Upvote 0
Top