Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo ways for registration and starting service in Google Cloud Messaging (GCM)
    text
    copied!<p>I'm trying to develop an application with GCM. I read the official document about GCM, but feeling like there are two ways for doing these.</p> <h2>1. As for registering an android device for GCM</h2> <p>It seems like there are two ways for registering a device to GCM. </p> <p>"http://developer.android.com/guide/google/gcm/gs.html#android-app" says,</p> <pre><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"); } </code></pre> <p>on the other hand, "http://developer.android.com/guide/google/gcm/gcm.html#registering" says,</p> <pre><code>Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); // sets the app name in the intent registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); registrationIntent.putExtra("sender", senderID); startService(registrationIntent); </code></pre> <h2>2. As for getting responses from GCM server and starting a service for handling them</h2> <p>Also, I'm feeling like there are two ways to start intent for handling the response.</p> <p>"http://developer.android.com/guide/google/gcm/gs.html#android-app" instructs like,</p> <p>"make a sub class of com.google.android.gcm.GCMBaseIntentService"</p> <p>and implement </p> <p>onRegistered(Context context, String regId)</p> <p>onUnRegistered(Context context, String regId)</p> <p>onMessage(Context context, Intent intent)</p> <p>...etc.</p> <p>While "http://developer.android.com/guide/google/gcm/gcm.html#registering" is described with,</p> <pre><code>public class MyBroadcastReceiver extends BroadcastReceiver { @Override public final void onReceive(Context context, Intent intent) { MyIntentService.runIntentInService(context, intent); setResult(Activity.RESULT_OK, null, null); } } public class MyIntentService extends IntentService { private static PowerManager.WakeLock sWakeLock; private static final Object LOCK = MyIntentService.class; static void runIntentInService(Context context, Intent intent) { synchronized(LOCK) { if (sWakeLock == null) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock"); } } sWakeLock.acquire(); intent.setClassName(context, MyIntentService.class.getName()); context.startService(intent); } @Override public final void onHandleIntent(Intent intent) { try { String action = intent.getAction(); if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) { handleRegistration(intent); } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) { handleMessage(intent); } } finally { synchronized(LOCK) { sWakeLock.release(); } } } } </code></pre> <p>Is it right that there are two ways for doing both of the processes above, and I can use whichever way I want to?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload