Android 安卓抬头通知在几秒钟后消失
我希望通知不会在几秒钟后消失。 因此,我创建了如下通知:Android 安卓抬头通知在几秒钟后消失,android,notifications,heads-up-notifications,Android,Notifications,Heads Up Notifications,我希望通知不会在几秒钟后消失。 因此,我创建了如下通知: NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.cast_ic_notification_small_icon) .setDefaults(Notification.FLAG_ONGOING_EVENT)
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.cast_ic_notification_small_icon)
.setDefaults(Notification.FLAG_ONGOING_EVENT)
.setPriority(Notification.PRIORITY_HIGH)
.setContentTitle(notificationDetails.getSubject())
.setContentText(notificationDetails.getMessage())
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setOngoing(true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
notificationBuilder.setFullScreenIntent(pendingIntent, true);
以及设置标志“正在进行”事件和方法“正在进行”(true)
但几秒钟后,通知继续消失。
我希望只有当用户单击时通知才会消失。
谢谢。提醒通知的持续时间无法更改。它设置在操作系统级别取决于操作系统为其提供的时间。持续时间无法更改。否则,它会干扰要显示的队列中的其他提醒通知。实际上可以使提醒通知持久化。诀窍是使用
setFullScreenIntent
。如果您不希望通知具有全屏版本,则可以使用不会实际启动任何活动的虚拟意图,如下所示:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.cast_ic_notification_small_icon)
.setDefaults(Notification.FLAG_ONGOING_EVENT)
.setPriority(Notification.PRIORITY_HIGH)
.setContentTitle(notificationDetails.getSubject())
.setContentText(notificationDetails.getMessage())
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setOngoing(true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
notificationBuilder.setFullScreenIntent(pendingIntent, true);
这是一种黑客行为,但这种行为还是有道理的。如果某个应用程序试图显示全屏通知,那么它必须是一个重要事件,如警报或电话。如果手机决定不显示全屏通知,在用户采取行动之前,它可能仍然会显示一些持久性的信息
这在我测试过的手机上是有效的,但是这种行为没有文档记录,因此没有任何保证。您可以通过以下方式实现:
notificationBuilder.setFullScreenIntent(pendingIntent, true)
您还必须使用这些:
val tempChannel = NotificationChannel(tempChannelId, "temp channel",
NotificationManager.IMPORTANCE_HIGH) // Setting to high is important
tempChannel.enableVibration(true)
...
notificationBuilder.setAutoCancel(false)
notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX) // PRIORITY_HIGH works too
notificationBuilder.setVibrate(LongArray(0)) // For older devices we need to add vibration or sound for the Heads-Up notification. This line will not make it vibrate, you can use another pattern, or default, if you want it to vibrate
notificationBuilder.setFullScreenIntent(pendingIntent, true)
重要:
在大多数设备上,这将在屏幕顶部显示通知,重叠所有内容,并保持不变
但在某些设备上,这将立即启动通知的未决意图。(如华为)
为了解决这个问题,我们可以为fullScreenIntent使用一个虚拟PendingEvent,它以相同的方式显示通知,只是不使用notificationBuilder.setFullScreenIntent(PendingEvent,true)代码>这将作为一个后备,因此会出现通知,但会在大约5秒后收缩到状态栏
val servicePendingIntent = PendingIntent.getService(context, 0, Intent(context, FullScreenIntentService::class.java), 0)
val mainActivityPendingIntent = PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), PendingIntent.FLAG_ONE_SHOT)
...
notificationBuilder.setContentIntent(mainActivityPendingIntent)
notificationBuilder.setFullScreenIntent(servicePendingIntent, true)
其中:
class FullScreenIntentService : Service() {
override fun onCreate() {
super.onCreate()
showNotificationHereTheSameWayJustWithoutSetFullScreenIntent()
stopSelf()
}
override fun onBind(intent: Intent?): IBinder? = null
}
别忘了在AndroidManifest中注册服务
.setFullScreenIntent(fullScreenPendingIntent, true)
若要保留提示通知,您需要添加全屏意图此问题在Honor和华为设备上观察到。
您可以尝试通过使用setFullScreenIntent
并向AndroidManifest添加权限来修复它
代码:
PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setFullScreenIntent(pIntent, true);
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
AndroidManifest.xml:
PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setFullScreenIntent(pIntent, true);
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
AFAIK,这是不可能的。