1

如何区分Notifications并将它们传递给不同的活动?在下面的方法中,第三个parameter(String tag) 值是从中检索的onMessage(),其中包含一个值。根据该值,我想区分打开哪个 URL,例如 Google 或 Facebook。在我在下面的“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);

}
4

2 回答 2

1

在清单上为活动创建意图过滤器,您需要像这样启动

<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") = 过滤器中的操作名称时

并且在您的活动中将调用方法 onNewIntent(Intent newIntent),然后意图传入

于 2012-10-12T09:31:02.483 回答
0

将您的代码更改为:

  if (tag.equals("one")) {
     notificationIntent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("https://www.google.com"));
  } else if (tag.equals("two")) {
     notificationIntent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("https://www.facebook.com"));
  }
于 2012-10-12T08:52:37.610 回答