Search code examples
androidandroid-c2dmgoogle-cloud-messaging

Which service is started in method "internalRegister" of GCM?


Below is the code from internalRegister method of GCMRegistrar class

static void internalRegister(Context context, String... senderIds) {
        if (senderIds == null || senderIds.length == 0 ) {
            throw new IllegalArgumentException("No senderIds");
        }
        StringBuilder builder = new StringBuilder(senderIds[0]);
        for (int i = 1; i < senderIds.length; i++) {
            builder.append(',').append(senderIds[i]);
        }
        String senders = builder.toString();
        Log.v(TAG, "Registering app "  + context.getPackageName() +
                " of senders " + senders);
        Intent intent = new Intent(GCMConstants.INTENT_TO_GCM_REGISTRATION);
        intent.setPackage(GSF_PACKAGE);
        intent.putExtra(GCMConstants.EXTRA_APPLICATION_PENDING_INTENT,
                PendingIntent.getBroadcast(context, 0, new Intent(), 0));
        intent.putExtra(GCMConstants.EXTRA_SENDER, senders);
        context.startService(intent);
    }

In the last line of code it starts a service which is supposed to contact the GCM server. the GSM server in turn send a Broadcast com.google.android.c2dm.intent.REGISTRATION back. Which service is this?


Solution

  • The intent com.google.android.c2dm.intent.REGISTRATION is part of the Android OS since 2.2. In the old days, we call startService to this intent in order to register our devices with Google C2DM server. You can also do the same thing with GCM, without using the GCM client library.

    See this migration guide and you'll know what I meant: http://developer.android.com/guide/google/gcm/c2dm.html

    For me, I skipped the GCM library altogether, and use the com.google.android.c2dm.intent.REGISTRATION intent directly to register.