For Guru
For Guru

Reputation: 1207

Android StartActivity on GCM receive

I have scenario when App receives GCM on any event occurred, an Activity is shown to get detail of the event from server.

When GCM is received there are chances that App might be running or same activity might be running so how to handle these scenarios.

Upvotes: 0

Views: 1425

Answers (2)

LordRaydenMK
LordRaydenMK

Reputation: 13321

You can achieve this with using an Ordered Broadcast.

  1. Define an action string you will use when the event occurs that you want to go to the activity or notification (e.g., com.commonsware.java.packages.are.fun.EVENT).

  2. Dynamically register a BroadcastReceiever in your activity, with an IntentFilter set up for the aforementioned action string and with a positive priority (the default priority for a filter is 0). This receiver should then have the activity do whatever it needs to do to update the UI based on this event. The receiver should also call abortBroadcast() to prevent others from getting it. Be sure to register the receiver in onStart() or onResume() and unregister the receiver in the corresponding onStop or onPause() method.

  3. Register in your manifest a BroadcastReceiver, with an set up for the aforementioned action string. This receiver should raise the Notification.

  4. In your service (e.g., an IntentService), when the event occurs, call sendOrderedBroadcast().

More info on the CommonsBlog

Upvotes: 0

bjiang
bjiang

Reputation: 6078

You need to create GcmReceiver and GcmMessageHandler class as following:

GcmReceiver

public class GcmReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(), GcmMessageHandler.class.getName());

        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

GcmMessageHandler handle the IntentService from receiver

public class GcmMessageHandler extends IntentService {

    String mes;
    private Handler mHandler;

    public GcmMessageHandler() {
        super("GcmMessageHandler");
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);
        mes = extras.getString("YOUR CONTENT"); // The content you wanna get
        Intent i = new Intent(*CONTEXT*, ACTIVITY_YOU_WANT_TO_GO.class);// change the context and activity name.
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        CONTEXT.startActivity(i); // change the context name.

        GcmReceiver.completeWakefulIntent(intent);
    }
}

In the AndroidManifest:

<receiver
        android:name=".GcmReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <category android:name="YOUR_PACKAGE_NAME"/>
        </intent-filter>
    </receiver>

    <service android:name=".GcmMessageHandler"/>

For more details, please refer to this.

Upvotes: 1

Related Questions