androidandroid-intentbroadcastreceivergoogle-cloud-messagingnotification-area

Android receiving a GCM message and put it in notification


I read here on how to receive a GCM message: http://developer.android.com/google/gcm/client.html - I'm talking about the title: Receive a downstream message. There is a note: Using WakefulBroadcastReceiver is not a requirement. If you have a relatively simple app that doesn't require a service, you can intercept the GCM message in a regular BroadcastReceiver and do your processing there. Once you get the intent that GCM passes into your broadcast receiver's onReceive() method, what you do with it is up to you.

When a GCM message is received all I want is to extract the title from it and put it in the notification area so when the user clicks on it, it will open my app with a specific fragment. Ofcourse, the device might be asleep when that message arrives.

The broadcast receiver example is this:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

So here are several questions:

1) Wakeful broadcast receiver is solely to prevents the device from going to sleep or also to wake it up when a message arrives?

2) How can I know if I need a regular broadcast receiver or a wakeful one?

3) Assuming I have several broadcast receivers, how does the app knows which to use when a message arrives?

4) Instead of calling an intent service that uses the intent of the broadcast receiver, if I only want to extract the title and put it in the notification area, I should just handle the intent inside the broadcast receiver itself?


Solution

  • 1) Wakeful broadcast receiver is solely to prevents the device from going to sleep or also to wake it up when a message arrives?

    solely to prevents the device from going to sleep.

    2) How can I know if I need a regular broadcast receiver or a wakeful one?

    if you are going to do too much work, you need to use intentService because the receiver is called on the UI thread and if you want to keep your CPU be alive while doing your work use wakeful. In general it is not guarantee that when you call the service from your broadcast receiver in this situation your CPU dose not fall sleep again so always use wakeful.

    3) Assuming I have several broadcast receivers, how does the app knows which to use when a message arrives?

    From intent filter of your receiver in the manifest:

        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example" />
        </intent-filter>
    

    4) Instead of calling an intent service that uses the intent of the broadcast receiver, if I only want to extract the title and put it in the notification area, I should just handle the intent inside the broadcast receiver itself?

    yes you can do it because it dose not take much time. And also it is guarantee that the CPU dose not go sleep while doing onRecive method of broadCastReciever.