5

I have GCM notification implementation. I know that the client application receives the notification whether it is in the foreground, background or killed state. What I would like to know is, how can I launch my application on notification received, when the application is in the killed state?

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
jasdmystery
  • 1,742
  • 1
  • 19
  • 31

2 Answers2

5

In the message receiver, I do the following:

final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

Here replace YourActivity.class with the entry activity. This worked for me.

sroes
  • 14,663
  • 1
  • 53
  • 72
jasdmystery
  • 1,742
  • 1
  • 19
  • 31
2

you Can Use NotificationManager to Start your activity.

try to use Below Code Inside your onMessage() method Which is overridden method in Class that extends GCMBaseIntentService class of GCM.

int icon = R.drawable.your_app_icon;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, YOUR_ACTIVITY.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        // notificationIntent.putExtra("PostName", vPostText);
        // Log.i(TAG, "Sent Postid " + postid);

        // Util util = (Util) context.getApplicationContext();
        // util.setPostID(postid);
        // util.setNotify(true);
        // util.setUserNAME(vPortCode);
        // util.setPostNAME(vPostText);
        // util.setmEDIA(vMedia);
        // util.setmEDIATHHUMB(vMediaThumb);
        // util.setmEDIATYPE(vMediaType);
        // util.setAirportName(vAirportName);

        notificationIntent.putExtra("Set_image", true);
        notificationIntent.putExtra("Notify", true);

        // set intent so it does not start a new activity
        // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        // | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context,
                (int) System.nanoTime(), notificationIntent, 0);

        notification.setLatestEventInfo(context, title, message, intent);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify((int) System.nanoTime(), notification);
Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
  • 1
    Thank you.I know how to start an activity from the application. But, What I would like to do is,on notification received,if my application is in the background,i would bring it to the fore ground, if it is in the foreground then i would continue processing, and if my application is in the stop/killed state then i want to "relaunch the application" not activity. So can you tell me if there is any way to launch the application when the application is in the stop/killed state on notification received? – jasdmystery Nov 28 '12 at 10:11
  • @jasdmystery: did you find away out? i'm facing same issue. – user836026 Jan 22 '13 at 09:44
  • 1
    Refer to the answer above. – jasdmystery Jan 22 '13 at 12:44