B4J Question FirebaseNotifications - missing an Authentication Key

woniol

Active Member
Licensed User
Longtime User
Hi,
I'm using Firebase Notifications to send notifications from B4J server to my Android(B4A) app.
It all done according to this example: https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

It works fine most of the time but sometimes after sending the request using code from the example I get this in Job done:
B4X:
Reason: The request was missing an Authentication Key (FCM Token). Please, refer to section "Authentication" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server., Response: <HTML>
<HEAD>
<TITLE>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

The same Server Key is passed every time.
I use jOkHttpUtil2_NONUI 2.70 in my B4J app.

Any ideas what can cause this strange error?
Some problems in: Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
or some Firebase issue?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

woniol

Active Member
Licensed User
Longtime User
based on the error it is not.

Post your code

Most of the time it works fine, but sometimes the notification is not send because of that error.
The code I use comes from the example and is:
B4X:
Sub Process_Globals
    Dim API_KEY As String = "AAAAM0xxxxxxxxxxxxx"
End Sub

Private Sub SendNotification(Topic As String, data As Map)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    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)
    LogError("Noti sending: "&jg.ToString&" KEY: "&API_KEY)
End Sub

Sub JobDone(job As HttpJob)
    If job.Success Then
        Main.Logger.logsINFO("[Notify] Message sent")
    Else
        Main.Logger.logsERROR("[Notify] Message not sent: "&job.ErrorMessage)
    End If
    job.Release
End Sub
 
Upvote 0

woniol

Active Member
Licensed User
Longtime User
The code looks correct (though there is no need to have a JobDone sub anymore).

Sleep for a few seconds when the request fails and send a new one. You should expect errors to happen.
Ok, thank you. I'll change it to:
B4X:
Wait For (j) JobDone(j As HttpJob)
and then when it fails , I'll resend it after some sleep.

I'll let know about the results.

As I mentioned: I use jOkHttpUtil2_NONUI 2.70 in my app and it runs on raspberry pi.
 
Last edited:
Upvote 0

woniol

Active Member
Licensed User
Longtime User
Hi,
the 'missing api key' problem returned.
This time I download data from different web service every 10 minutes (with timer) with the code below:
B4X:
Dim Job As HttpJob
    Job.Initialize("", Me)
    Job.Download("https://airapi.airly.eu/v2/measurements/installation?installationId=xxx")
    Job.GetRequest.SetHeader("Accept", "application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Accept-Language", "pl")
    Job.GetRequest.SetHeader("apikey", API_KEY)
    Wait For (Job) JobDone(Job As HttpJob)
    If Job.Success Then
        ParseAnswer(Job.GetString)
    Else
        Main.Logger.logsERROR("[Airly] data received error: "&Job.ErrorMessage)
    End If
    Job.Release

and sometimes I get logged:
B4X:
[Airly] data received error: {"message":"No API key found in request"}

so I looks very similar to the problem I have with Firebase Notification requests.

Is it possible that
B4X:
Job.GetRequest.SetHeader("apikey", API_KEY)
is not called before sending reguest?
 
Upvote 0
Top