无法从推送通知启动活动

时间:2014-11-27 03:13:02

标签: android-activity push-notification

我想在用户点击推送通知时启动活动。它适用于活动A,但不适用于其他活动B,它不能完全发挥作用。活动B发生的情况是,如果应用程序未运行,单击推送通知将正确启动活动B.但是,如果应用程序已在运行,则单击推送通知只需打开当前屏幕。

以下是我设置推送通知的方式:

Intent rewardsIntent = new Intent(this, ActivityRewards.class);
rewardsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationUtils.createNotification(this, R.string.reward_notification, getResources().getString(R.string.reward_notification), "You have been rewarded", rewardsIntent);

NotificationUtils中的静态方法创建推送通知:

public static void createNotification(Context context, int notificationId, String title, String text, Intent resultIntent) {
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ico_launcher)
            .setContentTitle(title)
            .setContentText(text);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
            context,
            0,
            resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );

    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setAutoCancel(true);
    mBuilder.setOngoing(true);

    NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notificationId, mBuilder.build());
}

在AndroidManifest.xml中,我将活动声明如下:

   <activity android:name=".ActivityRewards"
              android:label="@string/my_rewards"
              android:exported="true">
   </activity>

这让我困惑了好几天。任何帮助将非常感谢。感谢。

1 个答案:

答案 0 :(得分:0)

我设法通过为ActivityRewards添加launchMode声明来完成这项工作,如下所示:

<activity android:name=".ActivityRewards"
          android:label="@string/my_rewards"
          android:launchMode="singleTask"
          android:exported="true">

相关问题