I am trying to use the GCM service in my android app.
For that, I used the android documentation from http://developer.android.com/guide/google/gcm/gcm.html
I created the client side registration process with the sender id etc and the server side application where I am using the registration id and the sender id to send messages.
When I am installing the app in my phone through Eclipse, the push notifications works fine, so the sender id i have is right.
Then, when i export the apk file with Eclipse and install it in my phone, I am getting the error message that the SenderId is wrong
MissmatchedSenderId
Anyone has an idea whyI am getting this.
I have read those topics:
Why do I get "MismatchSenderId" from GCM server side?
When sending messages using GCM, I keep getting the response 'MismatchSenderId'
But the strange thing in my case is that everything works fine before exporting the app as apk and then I have this problem.
Any idea is mostly wellcome.
I actually had the same problem, and was researching more than 10 hours.
I finally found out the problem!
Nothing related to the Server API key or Browser API Key or SenderID.
The problem was the Google documentation:
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
Google says that you have to call the getRegistrationId function and only if the id is empty call register!
Which did not work for me at all... when I did that I always got back MismatchSenderId when sending to this regId.
My solution was:
Always call
GCMRegistrar.register(this, SENDER_ID);
and when the function
protected void onRegistered( Context c, String regId )
is called save the regId in my server database.
if I do it this way, all works fine!
The combination of SenderID and API key provided by GCM is unique per application.
We faced the senderID mismatch issue, when we updated our senderID on the client side but still used the API key related to old senderID.
We were able to resolve the issue after we updated the server API key.
Also the answer by #schurtertom is very helpful
Related
I am integrating GCM in my application. I did one R&D 2 years back where user can send the detail on the main activity like this
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
Here we can send the SENDER_ID, which was api key(if I recall correctly something like AIxxxxxxxxxxxxxp-xxxxx_xxxx_2xxxxxx2_De).
But how do we send this using the sample app. What is difference between SENDER_Id generated and API_KEY and SERVER_API_KEY. How is this implemented?
I havegone through this link https://developers.google.com/cloud-messaging/registration. But got confuse. Can any body help me for these
SENDER_ID, API_KEY and SERVER_API_KEY. What are those? when and where are they used?
Use of google-service.json file in debug mode? Already gone through link What does google-services.json really do?
Do we need to fill the entries inside json manually, or is it filled by android api's? Entried like
"oauth_client": [],
"api_key": [],
In below line taken from RegistrationIntentService.java. R.string.gcm_sender_id is basically SENDER_ID or API_KEY
String token = instanceID.getToken(getString(R.string.gcm_sender_id),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Also is there any tutorial which is implemented based on the latest GCM integration(apart from sample provided by developer.android)
Help will be appreciated.
SENDER_ID is the 13-digit(currently) numeric String, which is the project number of your Google project created in Google Developer console, it's used on client side to register the application and get device token(registration_id) which is then used as recipient when you send notifications to gcm end server.
API_KEY,SERVER_API_KEY are same thing, they are the 40-character String starting with AIza, they are used as authentication so GCM server knows who is sending the notification and does the person have the right to send notification to those registration_id(initially to the SENDER_ID, as that's where the registration_id come from).
2&4. In debug mode, the use of the file is to pass in the SENDER_ID, which is where R.string.gcm_sender_id in your question 4 comes from.
I don't quite understand the question, API_KEY is used on the server side, Android client shouldn't need to deal with it.
I'm developing a small application using GCM Service.
Before, I tried to send to my self a message, but (server side) the answer has been:
"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}
But at the beggining of my app i check this:
final String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals(""))
{
// Register
GCMRegistrar.register(this, SENDER_ID);
}
else ...
It seems evident that getRegistrationId() works fine only locally, (SharedPreferences ?)..
The nice thing is that i never did Unregister my app,just reinstall it, not changing version number (because it is in test,still) so in this case my account has expired, in those cases google should not send me another id that i could catch here:
#Override
protected void onRegistered
???
However there is a safe way to understand if my app is registered GCM server side?
Thanks!
EDIT:
I'm thinking this:
When i reinstall my application through Eclipse, there is a moment where my application is not installed, if GCM server send me a message in that moment there is no receiver and so google unvalidates my ID.
In your opinion is this idea, a stupid idea?
follow these steps http://developer.android.com/google/gcm/gs.html.
It could happen because some time gcm registartion id gets expired so everytime you should check that is it registered or not if not register it
You should only register the device one time. Save the registrationId in some way (shared preferences is a good one) and use it.
From the documentation:
Register the application for GCM and return the registration ID. You must call this once, when your application is installed, and send the returned registration ID to the server.
In some tests i've made, the registration id returned by gcm.register(id) isn't always the same for the device,app pair. However, Google says:
Repeated calls to this method will return the original registration ID.
If the method gcm.register(id) returns an id, it will be valid ever since.
Best.
The application prepared by me is using GCM to get push notifications from the server.
The emulator is getting the push notification but my device is not getting the push notification when I install the same app on the device.
I have provided the SENDER_ID and API Key to the server. The registration Id and device id is sent through the code.
I am fetching the device id using this code :
TelephonyManager telephonyManager1 =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
device_id = telephonyManager1.getDeviceId();
I am stuck with the problem since last 2 days, but not able to solve it till now. Any help is highly appreciated.
Did you register the device with GCM? for getting the Registration ID you have to do the Following code:
inside onCreate() method, add the following code:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
here you will get regId which you must have to sent it to the Server so that server can send push notification to Device which has the above regId.
for More Help you can refer here
EDITED
you can also refer this Link HERE
The issue was solved as the problem was from the server side.
In GCM we do not need to provide the device id. But just the sender id, registration id and api key are needed.
I am trying to migrate to GCM in Android, C2DM now being deprecated. The registration process described here is different from registration described here. Are both registration same? Can we see code for GCMRegistrar to know for sure?
I've successfully migrated my C2DM project to GCM. Tested, it works fine. The only changes were:
in the Android app - change the value of sender upon registration
on the server side - change the auth header and the URL
That was it, as far as the interaction with Google goes. There were more some changes dictated by the app's logic:
in the Android app, the registration ID was cached in its preferences. Upon upgrade, I remove reg ID from the preferences to force re-registration, this time with GCM.
the logic of passing the reg ID to the server got an extra boolean parameter - if this is a C2DM or GCM reg ID
the logic of sending messages became conditional upon the said parameter.
Throwing out the C2DM logic completely out of the server would be unwise - not everyone upgrades their Android apps. The old, C2DM-enabled versions will be out in the wild for some time. And Google pledged to keep C2DM running in the short term. So message sending is conditional - depending on reg ID type, it sends either to GCM or to C2DM.
EDIT re: conditional logic:
if($RegID_Is_GCM)
{
$Auth = GCM_Auth();
$URL = $GCM_URL;
}
else
{
$Auth = C2DM_AUTH();
$URL = $C2DM_URL;
}
They are actually the same thing. The second one encapsulates the first one in a static method and registers a broadcast receiver. You can attach the source to the gcm.jar and see for yourself. You can find source code in ~/android-sdks/extras/google/gcm/gcm-client/gcm-src.jar
The Thing I like most in GCM is the RegID we will get from GCM server,it is not only an ID its an Address of this application on this Device. So this time you don't need to send a device Id to server along with your Registration Id as per was in C2DM.
In C2DM every time you request a registration id you will get a new ID.
But in GCM RegId generated by using your application package along with some device id so if you will request for Registration Id again and again you will receive the same RegId.
And if you uninstall an application and will install it again still GCM server will give you the same Registration Id.
So one Registraion Id will do no need of any Device Id to send to server.
I have been successful at migrating from C2DM to GCM. I have also documented how to implement GCM at
http://android.amolgupta.in/2012/07/google-cloud-messaging-gcm-tutorial.html
GCMRegistrar is just a helper that does the leg work described in the first page.
You can see the class here. android-sdk\extras\google\gcm\gcmclient\src\com\google\android\gcm.
I am working with a messaging application using c2dm.. I get registered my app with c2dm..But the time am sending message to any particular id who is using my app, getting errors saying that "Sender Id mismatch".. What it will be ? can anyone help me ?
you need to set the sender_id in the client code with the email
address you registered to the c2dm servers (the one you use in the
http post to send messages).
usedintent.putExtra("sender","yourMail#gmail.com");