B4J Question FCMPush Authentication issue

QtechLab

Active Member
Licensed User
Longtime User
Hi,
I would like to send notification to specific device with the B4J server. I have the Firebase token of the destination device.

I'm in trouble with Firebase authentication.

This is the code, FCMPush with little variation:
B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private const API_KEY As String = "AAAArE0gcBE..."  
    Private myToken As String = "eiTAn3c39UU..."
End Sub

Sub AppStart (Args() As String)
    SendMessage(myToken, "This is the title", "Hello!!!!")
    StartMessageLoop
End Sub

Private Sub SendMessage(Token As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("token": Token)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    m.Put("data": data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/v1/projects/myapplication****-78ecd/messages:send", jg.ToString)
    Job.GetRequest.SetContentType("application/json")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
    'Job.GetRequest.SetHeader("Authorization", "Bearer " & API_KEY)
End Sub

Sub JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    End If
    job.Release
    StopMessageLoop '<-- non ui app only
End Sub

This is the error:
B4X:
Waiting for debugger to connect...
Program started.
{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}
[jobname=fcm, success=false, username=
, password=, errormessage=Unauthorized, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@50134894, tag=java.lang.Object@2957fcb0
, httputils2service=null]
Program terminated (StartMessageLoop was not called).

Can someone help me?

Thanks in advance
 

QtechLab

Active Member
Licensed User
Longtime User
It is not a server. It is a command line program.

Where does this link come from? You don't need to change the link. Just the "to" value in the map.
Yes, it is not a server. i was thinking of it beacouse i have to integrate it in a server.

I changed the link, i thought that "https://fcm.googleapis.com/fcm/send" had to be changed with the application that need that service.

Now it is working. Thanks very
 
Upvote 0
Top