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:
@Override
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;
if(arg1.getExtras().getString("url").isEmpty())
{
notificationIntent = new Intent(this, arg1.getExtras().getString("packageName").getClass());
}
else
{
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);
}
Applications can be launched using
Intents. You need to construct correctPendingIntentobject, like this:If you didn’t know target package or target
Activity, you can use following code: