Android - how can I send a GCM push notification with instructions of which activity to load?

android, google-cloud-messaging, push-notification

Solution

UPDATE: Give Eran credit for the JSON, I just want to elaborate.

You can add other parameters with the data key:

{
   "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
   "data": {
       "stuff": "100",
       "more": "abc"
   },
}

Then access the same way using `intent.getExtras().getString("stuff")`.

It is all here.

Then in your `generateNotifcation()`:

private static void generateNotification(Context context, String message) {
    NotificationManager notificationManager = (NotificationManager)
        context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, message, when);
    String title = "...";


    //get id from json here and decide which activity to go to...
    Intent notificationIntent = new Intent(context, someClass.class);


    notificationIntent.putExtra("message",message);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.defaults|=Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}

Problem

I am able to create push notifications. But currently I am just able to make people land on the home screen. How can I send people to a specific Activity? And is it possible to also put add some parameter like item_id so the activity knows what data to load? Or if there is a good tutorial for this somewhere, that would be great as well. I can't really seem to find much good info on this by googling. In my GCMIntentService I have this method: ``` @Override protected void onMessage(Context ctxt, Intent message) { Bundle extras=message.getExtras(); try { String question_id = extras.getString("question_id"); // SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( this ); // Intent intent = new Intent(ctxt, QuestionActivity.class); generateNotification(ctxt, extras.getString("message"), "New Message" ); } catch ( Exception e ) { } } ``` But I am not sure how to change the generateNotification to also signal what Activity the person should land on. Thanks!

Original source