Why do I get "MismatchSenderId" from GCM server side? - android

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!!

Related

Example GET request to manage topic subscriptions from the server side using Firebase Cloud Messaging

I found at https://developers.google.com/instance-id/reference/server#get_information_about_app_instances the following example GET request:
https://iid.googleapis.com/iid/info/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA?details=trueAuthorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
The key in that example is: AIzaSyZ-1u...0GBYzPu7Udno5aA. Where can I find my key, the one that I should use in my GET request? I am trying to find it at https://console.developers.google.com/apis/credentials, and this is what I see:
I tried using all of the four keys that you see in the image above, and I always receive this message from the browser:
{"error":"MissingAuthorization"}
Does this error refer to the key being wrong, or to something else? Am I looking in the right place for the key? Thank you.
EDIT 1: I was looking at the question at How to check how many topics has been subscribed?, and I also tried using the keys that I found in my Firebase Console, under the "Cloud Messaing" tab. This is what I see:
I tried the GET request using those keys in the image above as well and I still see the same error: {"error":"MissingAuthorization"}.
EDIT 2: I am using this from the command line:
C:\curl>curl -k https://iid.googleapis.com/iid/info/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA?details=trueAuthorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{"error":"MissingAuthorization"}
C:\curl>
Notice how I simply copied/pasted what I found at https://developers.google.com/instance-id/reference/server#get_information_about_app_instances under the "Example GET request" heading. I am not even attempting to use my own keys and I see the same error: {"error":"MissingAuthorization"}. Does it mean the problem is with the key I am using? Is not it the one I see in the Firebase Console?
Based on the instructions at https://developers.google.com/instance-id/reference/server, under the heading "Get information about app instances", it shows that it is necessary to provide the Authorization: key=YOUR_API_KEY, which is set in the header. See at https://android.jlelse.eu/firebase-push-notification-using-curl-command-devoid-backend-e63795d282c4 an example of how to provide a parameter in the header.
Concerning the Authorization: key=YOUR_API_KEY that has to be used, it is the key that appears in the "EDIT 1" section of this question. I used the Server key, not the Legacy server key, and it worked for me.
https://developers.google.com/instance-id/reference/server shows this end point: https://iid.googleapis.com/iid/info/IID_TOKEN. But IID_TOKEN must be replaced with the instance ID token which corresponds to the instance of the app that was installed on a device. In my case, I found it by printing the instance ID or token of the device, using the following code in the first activity of my Android app:
Log.d("The token of device: ", FirebaseInstanceId.getInstance().getToken());
In the log of the Android monitor, I found the IID_TOKEN that I needed:
D/The token of device:: f35EFw4bsef:QWE34bFm56ZuQLWYSDDgPlkf4a88Lu6Gp4DoXVDJ5dRIlsdDncq0UdNnlDi7wxbbut6YX7Z1kwgyS3bzk_Zrl-1doHCf9XFdOXTThNzo4sDFEWqQjHKfNa3uH2Js4Flbf_CnRkD2Mftr
Finally, I used cURL from the command prompt, and you can see how I obtained results correctly:
C:\curl>curl -X GET -k --header "Authorization: key=AAAAJ5XteYp:DKE32aIdFalyFXku6A-eR_wHL6ZBUFxfyPtcrm3wwF2l-nPEv_vAeYqo3NJzaIKKcSSow6gqoAwf3cBEm8QWYbF6w-asW0SX0RmHZftQglwmo_ziwrk8wFcGp1_DOQ1PLFFgP4BFWXAD" "https://iid.googleapis.com/iid/info/f35EFw4bsef:QWE34bFm56ZuQLWYSDDgPlkf4a88Lu6Gp4DoXVDJ5dRIlsdDncq0UdNnlDi7wxbbut6YX7Z1kwgyS3bzk_Zrl-1doHCf9XFdOXTThNzo4sDFEWqQjHKfNa3uH2Js4Flbf_CnRkD2Mftr
{"applicationVersion":"22","attestStatus":"NOT_ROOTED","application":"com.[myapp]","scope":"*","authorizedEntity":"232255245625","appSigner":"3d34g3fs3443292d825f21da4fdd5b34a56f2a3h","platform":"ANDROID"}
C:\curl>
Note: In the code above I have changed the values of things such as tokens and keys for privacy purposes, but you need to use your values and everything should work.

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....!!!

Google Cloud Messaging

I downloaded the GCM project from git and i was able to start the server (app engine for java) and also compile the client apk on the device. On startup the device successfully registers with the server and I can see the registration id when i go to
http://myserverip:8080/_ah/admin/.
The problem is when i hit the 'Send Message' button there is a message 'Single message queued for registration id ' and it doesn't do anything.
Below is a screenshot of the error i noticed on the terminal. Am i missing something. Any help is appreciated. Thanks.
.
By the screenshot - You are getting a 401 (unauthorized) status code - you are missing the API Key.
Look in the code you downloaded for the place (it will probably be some string whose current value is something like "ENTER_YOUR_API_KEY_HERE") in which you should enter your API Key which belongs to the API Project whose ID you are using at the Client app as sender ID when you register to GCM.

Android GCM send and MismatchSenderId

The odd thing is that notifications were working before. So I'm not sure what happened. I tried reinstalling my app to the device, but nothing changed.
{"multicast_id":xxxxxxxxxxxxxxxxxxx,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
Because this was working at one point, I know I am using the correct keys. I even triple checked based on other SOF threads I came across. What could be the potential problem now? I've looked around for a few days now and I'm still at a loss..
** EDIT **
Here's the POST info when a message is sent out from my server.
Headers
Array
(
[0] => Authorization: key=xxxxxxx-key-for-browser-apps-from-google-apis-console-xxxxxxx
[1] => Content-Type: application/json
)
Fields
Array
(
[registration_ids] => Array
(
[0] => xxxxxxxxx-big-old-id-from-the-device-xxxxxxxx
[data] => Array
(
[message] => hello
)
)
Also
I am using the key associated with Key for browser apps (with referers).
Apparently, when you recompile your app it will sometimes throw off the device id generation to where it won't match a previous id.
What I have to do is a COMPLETE uninstall of the app on my phone and recompile the app again (via eclipse). This seems quite pathetic, but it's the closest to a workable solution I have at the moment..
I think it must be something to do with the way you have set up API access for your project in the Google APIs console, in particular the 'allowed referers'. As the dev docs say
Mismatched Sender A registration ID is tied to a certain group of
senders. When an application registers for GCM usage, it must specify
which senders are allowed to send messages. Make sure you're using one
of those when trying to send messages to the device. If you switch to
a different sender, the existing registration IDs won't work. Happens
when error code is MismatchSenderId.
My project is set up to allow any referer like:
Key for browser apps (with referers)
API key: my secret
Referers: Any referer allowed
Perhaps you have restricted it to a certain IP range, and now you are trying to send the message from outside that range
you did not use right device token, that's why you got the error.
So, please get the new device token (Android mobile's token) and use it
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
MismatchSenderId because with in same device you have logged with different keys. to solve this problem uninstall app and run it againg and update the registration key. and then run the CURL script in your teminal which i post above it will give success message and you will get notification to your device

Android Gcm Not working with ROR

I read basic example for implementing google gcm. I developed small client side android application which successfully gives me registration id from gcm server.. now on server side I am using Ruby on rails hence we use this gem for implementing server side of gcm https://github.com/dondeng/gcm_on_rails. I pass my registration id to to my server.
I successfully ran these commands
device = Gcm::Device.create(:registration_id => "XXXXXXXXXXXXXXXXXXXXXX")
notification = Gcm::Notification.new
notification.device = device
notification.collapse_key = "updates_available"
notification.delay_while_idle = true
notification.data = {:registration_ids => ["RegistrationID"], :data => {:message_text => "Get on cloud nine"}}
notification.save
used to deliver notifications:
$ rake gcm:notifications:deliver
my rake command running without any error. But after completion of all these tasks my device not receiving any message.. I don't know whats going on in background. How to confirm that sent message properly receive by device.. Need Help... Thank you....
Debugging GCM messages can be cumbersome and difficult.
One possible reason is a wrong API key for GCM, which must be defined in the configuration files. If you use the wrong API key, then you might get a success response from the server {:code=>200, :message=>nil}, although no message has been sent. In this case go to the Google API Console and look up the right key.
Another possible reason is a wrong REGISTRATION_ID for the device. if the REGISTRATION_ID is not correct you may get an NotRegistered or InvalidRegistration error, which is not always recognized by the gcm_on_rails plugin (at the moment). Use the following curl command to debug the API through the console (insert your API_KEY and the REGISTRATION_ID of the device).
curl --header "Authorization: key=API_KEY" \
--header Content-Type:"application/json" \
https://android.googleapis.com/gcm/send \
-d "{\"registration_ids\":[\"REGISTRATION_ID\"],
\"data\":{\"message_text\":\"Test, Test\"}}"
A successful / unsuccessful attempt to call the Gcm API should look like this:
{"multicast_id":7961..,"success":1,"failure":0,"canonical_ids":1,
"results":[{"registration_id":"..","message_id":"0:.."}]}
{"multicast_id":7961..,"success":0,"failure":1,"canonical_ids":0,
"results":[{"error":"NotRegistered"}]}
Finally another reason is an invalid key in the day: do not use a reserved word like message_type as a key in the data hash. It will return a status code 200, but it will not deliver any notification. message_type is one of the advanced options (took me more than an hour to find that out).
If you have no error on server-side, maybe you are not handling correctly the event in your Android App.
Follow this guidelines: http://developer.android.com/guide/google/gcm/gs.html#android-app, and write logs for debug.

Categories

Resources