6

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!

Eran
  • 387,369
  • 54
  • 702
  • 768
Genadinik
  • 18,153
  • 63
  • 185
  • 284

2 Answers2

10

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);
}
Ryan
  • 1,135
  • 1
  • 13
  • 28
  • Actually one question: once I send and parse the extra parameter, how do I save it into something like SharedPreferences? – Genadinik Mar 11 '13 at 22:10
  • Actually I may have told you wrong. I am on my phoneI will update when I am back at a computer. Sorry update as soon as I can. – Ryan Mar 11 '13 at 22:17
  • Thanks...I think sending the id from the server along with the notification makes sense. I just implemented that. The trick is how to specify what Activity to go to in my call to generateNotification(ctxt, extras.getString("message"), "New Message"); – Genadinik Mar 11 '13 at 22:18
  • From `generateNotification` could you parse out the id and then create an intent `intent = new Intent(ctxt, someClass.class)` where `someClass` changes depending on the id? – Ryan Mar 11 '13 at 22:32
  • yes I could do that, but the method definition is ( Context, String , String)...unless I am not seeing another option for it. How would this call look like if we passed the intent in it? – Genadinik Mar 11 '13 at 22:39
  • I am trying to figure it out now. I have to step away for a few hours, but I will be back at it later today. I will try it out then, and I will post some results here. Thank you :) – Genadinik Mar 11 '13 at 23:23
3

Of course you can add a parameter like item_id. You can add any parameter you want to the notification. Unlike Apple Push Notifications, there are no predefined payload parameters, so just like you have a message parameter, you can have any other parameter with a String value (as long as the total lengths of the parameter names and values don't pass 4096 bytes).

And as for loading an Activity from the notification, you can find everything you'll need here.

Eran
  • 387,369
  • 54
  • 702
  • 768