Migration from C2DM to GCM - android

I reading this, but I don't understand what I must do. Now my application doing registration in c2dm by this code:
String pushId = C2DMessaging.getRegistrationId(this);
if(pushId == "")
{
C2DMessaging.register(this, "email#gmail.com");
}
What I must change in this code to do migration from c2dm to gcm?

First, go through the Getting Started steps. Once you created an API project, you will receive a 'Project ID', as mentioned #4 item on that document:
Take note of the value after #project: (4815162342 in this example). This is your project ID, and it will be used later on as the GCM sender ID.
So you just need to change your code to:
C2DMessaging.register(this, "4815162342");
The senderID must be a string number.
I am using the example from the 'Getting Started' guide, you should replace the sender ID with your own Project ID.
Finally, go through the GCM Architectural Overview, as you need to make some changes to the server for all of these to work.

What you need to change is basically the email address.
You need to send instead the API Key which you received in Google APIs Console page.

In GCM to get rid from Qutota google removed the signup an activation of an email for using Google Cloud Service.
When you go to Google Developer Console Here and click on create a new project that will give you a new project id that will be present in URL.
The PROJECT ID here will work like a user name and one more thing, this time Google is providing a jar gcm.jar that you need to add in your project classpath using build path to make GCM work .
this jar contains a class named GCMRegistrar having predefined function register() so you just need to add this code and forget
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
In C2DM SENDER_ID : Activated Gmail ID.
In GCM SENDER_ID : PROJECT ID in url .

Related

Android GCM SENDER_ID vs API_KEY usage

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.

StackMob failing to send broadcast push notification because of "no C2DM ClientLogin token"

I am trying to implement push notifications via StackMob on an existing Android app. I have gone through their tutorial and dev center to try to find a solution to my problem, but I cannot. In my app's BaseActivity I have my sender ID (actual id replaced with # for obvious reasons):
public static String SENDER_ID = "############";
from the Google API Console. I also have the init function (again, PUBLIC and PRIVATE keys replaced in this code):
StackMobAndroid.init(this.getApplicationContext(), StackMob.OAuthVersion.One, 0, "<PUBLIC KEY FROM STACKMOB DASHBOARD>", "<PRIVATE KEY FROM STACKMOB DASHBOARD>");
From what I understand, I also need to register any devices to recieve the notifications, which is just below the init:
try {
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
registerForPush();
} else {
Log.e("BaseActivity", "User already registered for push notifications");
}
} catch (UnsupportedOperationException e) {
Log.e("BaseActivity", "This device doesn't support gcm. Push will not work");
}
private void registerForPush() {
GCMRegistrar.register(this, SENDER_ID);
}
When I run my app, I get Registering app com.example.android of senders ############ in LogCat, so AFAIK my device is registered to get push notifications. But when I try sending a push notification from the StackMob dashboard and check the Log from that push, I see
Failed to send message {"alert":"deadbeef"} because there is no C2DM ClientLogin token for version=0
Can somebody explain what that log message is actually telling me?
There are two versions of Android push: the old one C2DM and the new one GCM. You can register tokens for either of these and StackMob will send a message using the appropriate credentials. C2DM has been deprecated, so all the SDKs register tokens as GCM by default and that's what the push instructions have you configure with the sender_id and all that.
Your token has accidentally been registered as C2DM somehow. When we go to push we see you have no credentials there and print that message. The solution is to remove the token via the push console and register it again as GCM.
https://dashboard.stackmob.com/data/console
The real question is how you ended up registering a C2DM token in the first place. If any StackMob tool or demo doesn't have GCM as a default and C2DM clearly marked as legacy, let me know.

Android Google Cloud Messaging (GCM) and missmatched sender id

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

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

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

Registration confusion Android GCM

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.

Categories

Resources