मूल रूप से, आप अपने मुख्य गतिविधि में निम्न करना चाहिए:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, GCMIntentService.GCM_SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
}
बाद में आप जब भी एप्लिकेशन एक com.google.android.c2dm.intent.REGISTRATION
एक registration_id
अतिरिक्त के साथ आशय प्राप्त करता है अपने अनुप्रयोग सर्वर, लिए पंजीकरण आईडी भेजना चाहिए। यह तब हो सकता है जब Google समय-समय पर ऐप की आईडी अपडेट करता है।
आप उदाहरण के लिए: अपनी खुद की कार्यान्वयन के साथ com.google.android.gcm.GCMBaseIntentService
बढ़ा कर इस लक्ष्य को हासिल कर सकते हैं, .:
public class GCMIntentService extends GCMBaseIntentService {
// Also known as the "project id".
public static final String GCM_SENDER_ID = "XXXXXXXXXXXXX";
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(GCM_SENDER_ID);
}
@Override
protected void onRegistered(Context context, String regId) {
// Send the regId to your server.
}
@Override
protected void onUnregistered(Context context, String regId) {
// Unregister the regId at your server.
}
@Override
protected void onMessage(Context context, Intent msg) {
// Handle the message.
}
@Override
protected void onError(Context context, String errorId) {
// Handle the error.
}
}
अधिक जानकारी के लिए, मैं (री) हैं writing the client side code और the Advanced Section of the GCM documentation के लिए दस्तावेज़ पढ़ें।
आशा है कि मदद करता है!
[हैंडलिंग Android पर Google क्लाउड संदेश सेवा में पंजीकरण आईडी परिवर्तन] के संभावित डुप्लिकेट (http://stackoverflow.com/questions/16838654/handling-registration-id-changes-in-google-cloud-messaging-on -एंड्रॉयड) – Eran