Android 通知集全屏意图不会自动启动活动



我想从通知中自动启动全屏活动,但我在setFullScreenIntent方面遇到了问题。我遵循了以下内容:https://developer.android.com/training/notify-user/time-sensitive和互联网上的线程,但这对我来说不起作用。我的通知出现了,但我必须按下它才能启动活动。

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>

谢谢!

我猜当你锁定手机时,全屏意图会正确显示,对吧?

你看到的是提醒通知。正如文档中所述,这是高重要性通知的正常行为,它们在设备解锁时出现。

你该怎么办

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


示例:

报警应用程序(在Pixel手机上(有以下触发报警的情况:

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

最新更新