Android 通知 setFullScreenIntent 不会自动启动 activity

Android notification setFullScreenIntent doesn't automatically launch the activity

我想通过通知自动启动全屏 activity,但我在使用 setFullScreenIntent 时遇到了问题。我遵循了这个:https://developer.android.com/training/notify-user/time-sensitive 和互联网上的线程,但它对我不起作用。 我的通知出现了,但我必须按下它才能启动 activity。

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = this.getSystemService(NotificationManager.class);

            if (notificationManager != null && notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null) {
                NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);
                channel.setDescription("channel_description");
                notificationManager.createNotificationChannel(channel);
            }            
        }

        Intent fullScreenIntent = new Intent(this, ResultActivity.class);
        fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
                fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle("Full Screen Notification")
                        .setContentText("Notification test text")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setDefaults(NotificationCompat.DEFAULT_ALL)
                        .setFullScreenIntent(fullScreenPendingIntent, true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(12345, notificationBuilder.build()); 

这是我的清单:

  <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
  <activity
        android:name=".ResultActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" /> -->
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
  </activity>

谢谢!

我猜当您锁定 phone 时,全屏 Intent 会正确显示,对吗?

您看到的是heads-up notifications。如文档中所述,这是高重要性通知的正常行为,它们会在设备解锁时出现。

你该怎么办?

保持锁定屏幕的全屏意图,但也为您的通知提供自定义布局,这将以更好的上下文 UI 而不是默认通知来展示您的功能。


示例:

闹钟应用(在 Pixel phones 上)触发闹钟的情况如下:

  • 屏幕解锁时启动提示通知
  • 屏幕锁定时的全屏意图。