Android:点击通知以启动活动

时间:2012-10-12 08:49:27

标签: android notifications google-cloud-messaging android-pendingintent

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

}

2 个答案:

答案 0 :(得分:1)

On Manifest为活动创建意图过滤器,您需要像这样启动

<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),然后调用intent

答案 1 :(得分: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"));
  }