Firebase cloud messaging authorizion error - android

i am trying to send FCM push notification to my android app
on from postman i have on my developer console two api keys when i try to use the first one i get this response from FCM :
Unauthorized
Error 401
and when i try the second one i get this response from FCM :
Error=MissingRegistration
and i have this data in the body :
{ "data":{
"score":"5x1",
"time":"15:10"
}
,
"to":"*******"
}
the token is working fine and i have test it in the firebase console
what the problem could be and what is the solution

Error 401 Unauthorized comes beacuse of wrong server key.
To solve this error:
Go to your project settings in firebase console.
Click on Cloud Messaging and copy Server Key mentioned in that page.
Let me know, if more help is required.

Related

FCM: Invalid registration token. Check the token format

I’m implementing Firebase Cloud Messaging (FCM) and am experiencing a problem that I’m unable to solve. I have implemented FirebaseMessagingService and FirebaseInstanceIdService according to the guide(s). When I go to Firebase Console for my app, and use the Notification function, I can successfully send a message to ALL my app instances (using the package name).
Now, in the code I have fetched the Firebase Instance Id (token) by use of the following code:
String token = FirebaseInstanceId.getInstance().getToken();
SendFirebaseTokenToServer(token);
(note that currently I’m using HTTP protocol, as my server does not yet have a cert). Anyway using the token I get from the call above, I go back to the Firebase Console and try to send a message to one (1) installed instance of my app. I grab the token from our server DB where it is stored as "varchar(max)". When I do that I get the following error message:
Invalid registration token. Check the token format.
I have googled that and found only one hit (having to do with Firebase and iOS):
http://stackoverflow.com/questions/41343520/ios-invalid-registration-token-check-the-token-format
That issue indicates that a cert was required (I think I’m reading it correctly). I’m not sure what I’m doing wrong. I need to get this to work using the Firebase Console first, then my server guy can start on his end knowing that it should work.
Turns out i was programatically encoding all POST or PUT parameters prior to sending to our server. the FCM token had a semicolon in it, which got encoded to a "%3A", seemingly causing the problem.
do NOT encode the FCM token.

FCM push notifications with Parse Server

This is complicated but I'll try to be as concise as possible. I am using FCM to send push notifications to Android devices. My backend is parse server. I can successfully send pushes from the FCM console to the device. And when I send pushes from parse to https://mybackend/parse/push, it says {"result": "true" } as the response. I have checked the server logs, and it (predictably) says all http post requests to /parse/push were 200 success.
However when sending from parse (using curl or the parse push console), the pushes do NOT show up in FCM console. There is some issue between parse server and FCM then.
In my index.js, I have:
push: {
android: {
senderId: "XXXXX",
apiKey: "YYYYYY"
}
}
I have the keys, and done everything according to here: http://parseplatform.org/docs/parse-server/guide/#push-notifications
what else am I missing? Why can't I get this to actually appear on the device? Any help is greatly appreciated, thanks.
Setting up a Parse Server for an Android app, I remember having to double check this to get it right. The key for me was this: when I logged into Firebase console and selected the app I was working on, and clicked on the gear [or cog or whatever] and then Project Settings, I had to make sure to click on the Cloud Messaging tab and grab the Legacy server key [under project credentials] NOT the web api key that appears on the General tab. Also the sender ID that you want to use appears on that page.
I'm not sure when they will deprecate the Legacy server key, you might try the Server key instead, but definitely make sure you get the info off of the Cloud Messaging tab.
I don't know why FCM (and APNs) don't send a different message when the key (or cert) is not set up right [that would be more developer-friendly at least] and instead just send a 200 but my guess is that they evaluate the key [or cert] after receiving the intended notification and sending a response.

I am testing Firebase notification in Postman but not get the response

I am new to Firebase. Now I tried to test the notification in postman
but If I test it in the Firebase console, I can able to get the notification.
I used the correct sender ID and API key from the Firebase console.
Please advice me for this issue. I checked some stack, but not solved those answers.
Edit: Payload:
{ "data": {
"ReferenceKey": "BD",
"ReferenceValue": "10"
},
"to" : "fzUHLONY-gY"
}
The 401 Error is most commonly received if you are receiving an Invalid/Wrong Server Key (see my answer here for more details). Simply use the correct Server Key from the Firebase Console.
With regards to your request in Postman, it looks fine. Do note that you don't have to add the Sender ID in the request. See my answer with details on using Postman.
Let me know if you're still encountering any errors.
In the Firebase console, go to Project settings -> Cloud Messaging and try to use the Server Key.
Remove sender from header and in body add firebase token id which are valid.Hope it will help you............................................

FCM Notification is not working If i send it by using ARC ( Advanced REST API )

I integrated the push notification system with Firebase in my project and it's working well except one thing.
If I try to send a notification from Firebase console, using Device Token & Topic, the notification shows in device.
If I try to send a notification from ARC ( Advance REST CLIENT) using Device Token, the notification shows in device.
If I try to send a notification from ARC ( Advance REST CLIENT) using my topic, NOTHING HAPPENS.
Requested URL : https://fcm.googleapis.com/fcm/send
RAW headers :
Content-Type: application/json
Authorization: key=AIz.....
RAW PAYLOAD :
{
"data":
{
"employeename":"kumaran","empid":"234444"
},
"to":"/topics/bmmembers"
}
Response Header
Status : 200: OK
JSON :
{
"message_id": 6536474446058224000
}
But my device not received any notification and checked Logcat whether any json arrived in client end, but not received.
Any idea and how we need to resolve this issues.
This is my trial and error type of a "Solution found for this Issue":-
Issue found is "The Quota for the particular Server-key or credential or the whole Google API Account itself might have been expired or used the limit. "
Below i have explained how i once again successfully received FCM Notifiaction with Image for the same Android App.
ISSUE:-
I was unable to send FCM push notification (with image) from Advanced REST Client, by using the Android key generated from Google API console in my very old created Project (Eg: My Project; that has 5-apps and its credentials).
I tried sending the Post request from ARC client many time but dint receive any single FCM notification in my Android Mobile.
SOLUTION I FOUND WAS:-
1a. I deleted the credentials (API key, server key,web key, OAuth key etc) in Google API console related to that particular app (Eg: FCM App1).
1b. Also i went to Firebase console and deleted the particular app in its project also.
Finally now the App (Eg.FCM App1) doesn't contain connection with
1.Google API console and with
2.Firebase Console.
1c. I also deleted the "google-services.json" file from the Android studio->Project folder.(If you are unable to delete this, try executing File->'Invalidate cache and restart' option and then retry deleting it. It will get deleted.)
Next, freshly
I created a new gmail account, and opened a new Google API Account, further opened a fresh new Project in that Google API Account (Eg: My Project1).
Next, I went to Firebase console signing in with this newly created Gmail acccount, opened a fresh new Project, selected "Add App" and selected "Android".
-> entered my package name, SHA1 key and Added the App.
-> Also downloadedthe new google-services.json file and added again in the Project folder in Android Studio.
-> And followed everything as per previous procedure.
==> Finally I got the fresh ""Server-Key"" in the new Google API console. Copied this server-key and pasted in Advanced REST client at place Authorization:key=AIzaSyAJzs-DDD......etc.
=> added the correct TokenID freshly created for this App from Log.
===> Pressed SEND button in the ARC, and WOOOOWWWW....
within few seconds I GOT THE FCM NOTIFICATION WITH IMAGE IN MY ANDROID MOBILE EVENTHOUGH MY APP WAS IN BACKGROUND OR COMPLETELY CLOSED at that time....!!!

What is the `Authorization` part of the http post request of Google's Firebase Downstream message?

I want to try to send a message using google's FCM messaging service and as the document says, the http request should be something like this:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
My problem is that I have not idea what Authorization's value should be and when I delete this header and make the request, I get error 401:Unauthorized.I think this must be some kind of api key or something but I cannot find it in my project. Can anyone help me?
PS: for testing purposes I currently am using this site to send a messsage to my device
According to About Firebase Cloud Messaging Server documentation:
Authentication
To send a message, the app server issues a POST request. For example:
https://fcm.googleapis.com/fcm/send
A message request consists of two parts: the HTTP header and the HTTP
body.
The HTTP header must contain the following headers:
Authorization: key=YOUR_SERVER_KEY
Make sure this is the server key, whose value is available in your Firebase project console under Project Settings > Cloud Messaging. Client keys or any other keys are rejected by FCM.
Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text.
If Content-Type is omitted, the format is assumed to be plain text.
UPDATE (2016/11/16)
Perhaps you should consider using the FCM token instead of the server keys as the note inside the following screenshot
UPDATE (2016/12/08)
Firebase has upgraded the server keys to a new version and recommended that you upgrade to the newest version. Please see the screenshot below:
i have also faced this error. The issue is with the server key. Don't use the api key from google-services.json file.
You should use the server api key from your firebase console project, at the cloud messaging tab project keys: https://console.firebase.google.com/project/firebase-probiz/settings/cloudmessaging
This is your Server key which you can observe into console.developers.google.com or Firebase project console under Project Settings > Cloud Messaging. Note the key it's auto created by Google Services. You ought add IP Address inco G.Console to restrict usage.
Plus do remember about Content-Type: application/json, otherwise you will get Error=MissingRegistration.

Categories

Resources