Take the 2-minute tour ×

Okay, here is my problem. I have a Service that receives Push notifications. Now, depending on the content of the push notification it should start the corresponding application, if clicked on it.

I got no problem receiving the push notifications or using the service, the only thing I can't get to work, is the part where it has the open the right application.

What do I need to do, to open an application from a Service? This has to be dynamically, since there will be multiple apps working with this service.

It would be great if anyone could point me into the right direction.

PS. I am using GCM service, which I put into a library, so I can use it in multiple apps of mine. This is the GCMIntentService.onMessage() function in which I need to check the content of the url and then set the right intent for the notification:

protected void onMessage(Context arg0, Intent arg1)
    Log.i(TAG, "new message = " + arg1.getExtras());

    //Get a reference to the NotificationManager
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

    //Instantiate the Notification      
    int icon = getResourseIdByName(arg0.getApplicationContext().getPackageName(), "drawable", "notification_icon");
    CharSequence tickerText = arg1.getExtras().getString("tickertext");
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    //Define the notification's message and PendingIntent 
    Context context = getApplicationContext();
    CharSequence contentTitle = arg1.getExtras().getString("contentTitle");
    CharSequence contentText = arg1.getExtras().getString("contentText");

    Intent notificationIntent = null;

        notificationIntent = new Intent(this, arg1.getExtras().getString("packageName").getClass());
        notificationIntent = new Intent(this, MyWebView.class);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;

    //Pass the Notification to the NotificationManager
    int notificationId = Integer.parseInt(arg1.getExtras().getString("notificationId"));

    mNotificationManager.notify(notificationId, notification);
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Applications can be launched using Intents. You need to construct correct PendingIntent object, like this:

Intent targetIntent = new Intent(Intent.ACTION_MAIN, null);
ComponentName cn = new ComponentName("target_app_package_name", "target_activity_class_name");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

If you didn't know target package or target Activity, you can use following code:

Intent startingIntent = context.getPackageManager().getLaunchIntentForPackage("target_app_package_name");
share|improve this answer
I tried this, but I can't get it to work. –  DijkeMark Oct 19 '12 at 11:50
It works now. if I use this: notificationIntent = arg0.getPackageManager().getLaunchIntentForPackage(arg1.getExtras().getString("p‌​ackageName").toString()); –  DijkeMark Oct 19 '12 at 12:14

well, from just a quick look at it, you seem to be able to get the full package name of corresponding application.

then you can just start application by package name, something like this

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.your.packageName.in.String");


For that, AFAIK, you can only start an activity from your own package that created that notification. You CAN start an activity from another application package, but then you have to know the activity class name for that package, which you usually can't possibly know.

Intent clickIntent = new Intent(context,Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,clickIntent,0);

this is what you need when creating notification.

notification.setlatestEventInfo(context, "title","content",pendingIntent);

urgh! sorry for crappy comment... I can't seem to put codes in the comments :(

share|improve this answer
But how can I put this into a push notification? –  DijkeMark Oct 19 '12 at 11:33
Check the edit :) –  Dan Lee Oct 29 '12 at 6:49

Your Answer


