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............................................
Related
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.
So yes I have done my research and none of the resources I found had an answer that was correct.
I have enabled GCM in my api console on google. I have put the sender ID in my app and the server key in the server. When I run my server to send notifications through gcm I get an error 'Not Registered'. I get the device key with this plugin: https://github.com/phonegap/phonegap-plugin-push.
How come I keep getting the not registered error?
you have to use FCM it's now essay to use and better than GCM
https://firebase.google.com/docs/cloud-messaging/android/client
for node js coding this link should help https://www.npmjs.com/package/fcm
Closing this post. The answer for anyone using this plugin who keeps getting an error like me. Try using this inside your app folder: cordova plugin rm phonegap-plugin-push and then reinstall it with your sender_id again
Do you get token in your app(client)?
When server send push message, server need server_api_key and token.
Client register in GCM. (It get token)
Server send message (token and server_api_key)
If client do not register, you get message "Not registered"
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.
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.
I'm trying to create a push service for my Android app, and I follow Google GCM's documentation and example for this matter:
I can register/unregister my Android app. From my server side, I can see that I have one subscription registered, but when I try to send a message from server to my Android app I always get following error:
Error sending message to device #0: MismatchSenderId
For my Android app, I use SENDER_ID = 200000000001.
And for my server side as Google says I use API_KEY = AIzxxxxxxxxxxxxxxxxXxxXxxXxxxXXXXXxxxxs
I followed this document:
http://developer.android.com/guide/google/gcm/demo.html
I've enabled GCM in my Google API panel too, yet I'll get that annoying error message.
How can I fix this problem?
Did your server use the new registration ID returned by the GCM server to your app? I had this problem, if trying to send a message to registration IDs that are given out by the old C2DM server.
And also double check the Sender ID and API_KEY, they must match or else you will get that MismatchSenderId error. In the Google API Console, look at the URL of your project:
https://code.google.com/apis/console/#project:xxxxxxxxxxx
The xxxxxxxxx is the project ID, which is the sender ID.
And make sure the API Key belongs to 'Key for server apps (with IP locking)'
Mismatch happens when you don't use the numeric ID. Project ID IS NOT SENDER ID!! It took me 9 hours to figure this out. For all the confusion created by google, check the following link to get numeric id.
https://console.cloud.google.com
instead of
https://console.developers.google.com
Hope it helps!!
Update:-
Things have changed again. Now the sender id is with firebase.
Go to https://console.firebase.google.com and select your project. Under settings -> cloud messaging, you can find the sender id.
And it works!
Please run below script in your terminal
curl -X POST \
-H "Authorization: key= write here api_key" \
-H "Content-Type: application/json" \
-d '{
"registration_ids": [
"write here reg_id generated by gcm"
],
"data": {
"message": "Manual push notification from Rajkumar"
},
"priority": "high"
}' \
https://android.googleapis.com/gcm/send
it will give the message if it is succeeded or failed
I encountered the same issue recently and I tried different values for "gcm_sender_id" based on the project ID. However, the "gcm_sender_id" value must be set to the "Project Number".
You can find this value under: Menu > IAM & Admin > Settings.
See screenshot: GCM Project Number
This happens when the Server key and Sender ID parameters HTTP request do not match each other. Basically both server ID and Server key must belong to the same firebase project. Please refer to the below image. In case of mixing these parameters from deferent Firebase projects will cause error MismatchSenderId
InstanceID.getInstance(getApplicationContext()).getToken(authorizedEntity,scope)
authorizedEntity is the project number of the server
Your android app needs to correct 12-digit number id (aka GCM Project Number). If this 12-digit number is incorrect, then you will also get this error.
This 12-digit number is found in your Google Play Console under your specific app, 'Service & API' -> 'LINKED SENDER IDS'
Check google-services.json file in app folder of your Android project. Generate a new one from Firebase console if you are unsure. I got this error in two cases.
I used a test Firebase project with test application (that contained right google-services.json file). Then I tried to send push notification to another application and got this error ('"error": "MismatchSenderId"'). I understood that the second application was bound to another Firebase project with different google-services.json. Because server keys are different, the request should be rewritten.
I changed google-services.json in the application, because I wanted to replace test Firebase project with an actual. I generated right google-services.json file, changed request, but kept receiving this error. On the next day it fixed itself. I suspect Firebase doesn't update synchronously.
To get a server key for the request, open https://console.firebase.google.com and select an appropriate project.
Then paste it in the request.
With the deprecation of GCM and removal of its APIs, it appears that you could see MismatchSenderId if you try to use GCM after May 29, 2019. See the Google GCM and FCM FAQ for more details.
Use sender ID & API Key generated here: http://developers.google.com instead (browse for Google Cloud Messaging first and follow the instruction).
If use for native Android, check your AndroidMaifest.xml file:
<meta-data
android:name="onesignal_google_project_number"
android:value="str:1234567890" />
<!-- its is correct. -->
instead
<meta-data
android:name="onesignal_google_project_number"
android:value="#string/google_project_number" />
Hope it helps!!