如何区分 Notifications
并将它们传递给不同的 Activity ?
在下面的方法中,第三个 parameter(String tag
) 值是从包含一个值的 onMessage()
中检索的。
根据该值,我想区分打开哪个 URL,例如 Google 或 Facebook。
在我将 Intent 代码包含在下面的“if”条件中后,该应用程序终止。
请帮忙
private static void generateNotification(Context context, String message,
String tag) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
message, when);
String title = context.getString(R.string.app_name);
Log.d("GCM", "Tag Value " + tag);
if (tag.equals("one")) {
// pass to a different activity
notificationIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.google.com"));
}
if (tag.equals("two")) {
// pass to a different activity
notificationIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.facebook.com"));
}
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
最佳答案
在 list 上为 Activity 创建 Intent 过滤器,您需要像这样启动
<activity
android:name=".activities.TabsActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="com.dittoadvertising.push" >
</action>
<category android:name="android.intent.category.DEFAULT" >
</category>
</intent-filter>
当 Uri.parse("https://www.facebook.com") = 过滤器中的操作名称
并且在你的activity中会调用方法onNewIntent(Intent newIntent),然后intent传入
关于安卓 : Click notification to launch activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12855395/