Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 881
Genadinik Avatar asked Mar 11 '13 21:03

Genadinik


People also ask

How to integrate GCM with Android push notifications?

Implement an Android Client app to register with GCM, send the registration id to your push notification server and manage the notifications sent from your server via GCM. Implement a server side API to get and store registration ids from the client app and optionally provide an Admin panel to send push notification from.

How do I set up push notifications on Android devices?

Get the SENDER_ID of the project or create a configuration file. Implement an Android Client app to register with GCM, send the registration id to your push notification server and manage the notifications sent from your server via GCM.

What is a GCM Service in Android?

A Service which will receive the GCM message and show it to the user as a notification, regardless of if our app is on the foreground. A Broadcast Receiver. This class will need to be registered in the manifest. Its role is to listen for system events for example.

What are Google Cloud messaging notifications?

In Android, the mechanism for native applications is Google Cloud Messaging (GCM) notifications. To clarify the use cases a bit: when an app is in the foreground, PubNub’s channels will do a great job for providing real-time data streams.


2 Answers

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.

like image 38
Eran Avatar answered Oct 06 '22 00:10

Eran


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);
}
like image 81
Ryan Avatar answered Oct 06 '22 00:10

Ryan