जैसा कि Google Play Services सेट-अप मार्गदर्शिका पर स्पष्ट रूप से बताया गया है।
https://developer.android.com/google/gcm/client.html
आवेदन OnCreate:
// Check device for Play Services APK. If check succeeds, proceed with
// GCM registration.
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (regid.isEmpty()) {
registerInBackground();
}
} else {
Log.i(TAG, "No valid Google Play Services APK found.");
}
पृष्ठभूमि कोड में रजिस्टर:
/**
* Registers the application with GCM servers asynchronously.
* <p>
* Stores the registration ID and app versionCode in the application's
* shared preferences.
*/
private void registerInBackground() {
new AsyncTask() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// The request to your server should be authenticated if your app
// is using accounts.
sendRegistrationIdToBackend();
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the
// message using the 'from' address in the message.
// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
...
}
स्रोत
2014-07-27 10:29:05
इस मदद करता है? http://stackoverflow.com/questions/11713363/i-can-not-get-registration-id-from-android-gcm – trojanfoe
मुझे एक ही समस्या है –
@ ट्रोजनफ़ो की टिप्पणी में शीर्ष उत्तर में आपका समाधान शामिल है। 'जीसीएम रेगेस्ट्रार.रेगस्टर' असीमित है और इसलिए अविश्वसनीय होने के तुरंत बाद 'getRegistrationId' को कॉल करना। प्रसारित करें कि Google सेवाएं आपको आईडी के साथ भेजती हैं। – tophyr