【发布时间】:2012-10-19 09:47:44
【问题描述】:
好的,这是我的问题。我有一个接收推送通知的Service。现在,根据推送通知的内容,如果单击它,它应该启动相应的应用程序。
我在接收推送通知或使用该服务时没有问题,我唯一无法开始工作的是它打开了正确的应用程序的部分。
我需要做什么才能从Service 打开应用程序?这必须是动态的,因为将有多个应用程序使用此服务。
如果有人能指出我正确的方向,那就太好了。
附言。我正在使用 GCM 服务,该服务已放入库中,因此我可以在我的多个应用程序中使用它。这是 GCMIntentService.onMessage() 函数,我需要在其中检查 url 的内容,然后为通知设置正确的意图:
@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);
}
【问题讨论】:
标签: java android service android-intent