setFullScreenIntent 第二次不起作用

Anh*_*nun 8 notifications android android-notifications android-activity

我在 android 中有一个应用程序,在某些情况下我想将一个 Activity 带到前台。我为此使用NotificationManager。这是我的代码。问题是,活动第一次成功地被带到前面,但后来却没有。此外,此代码是从服务运行的。

Intent notificationIntent = new Intent(context, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

            NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentIntent(contentIntent)
                        .setContentTitle("Bring me front!")
                        .setContentText("Bring me!!! BRING!!!")
                        .setFullScreenIntent(contentIntent, true);

            NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(0, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

小智 6

NotificationManager 有一个(未记录的)行为,即如果您发布与已处于活动状态的通知的 ID 相匹配的新通知,它将忽略fullScreenIntent 字段。

  • 这正是我们一直遇到的问题 - 尽管仅在**某些设备**上,而不是在所有设备上。谢谢! (2认同)

dsa*_*ler 3

有几点需要检查:

  1. 您的意图可能需要FLAG_ACTIVITY_NEW_TASK(并且可能FLAG_ACTIVITY_CLEAR_TOP)确保该活动被带到前面。
  2. 确保您没有尝试多次重复使用同一个 PendingIntent;每次发布此通知时,您都需要构造一个新的 PendingIntent 以便与fullScreenIntent.