几秒钟后,Android Heads-up 通知消失了。

5

我希望通知不会在几秒后消失。因此,我创建了以下的通知:

  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);

需要设置FLAG_ONGOING_EVENT并调用setOngoing(true)方法。

但是几秒钟后通知仍然会消失。 我希望只有用户点击时才能使通知消失。 谢谢。


3
据我所知,那是不可能的。 - CommonsWare
7个回答

11

实际上,可以使悬浮通知保持持久性。诀窍是使用setFullScreenIntent。如果您不想让通知具有全屏版本,可以使用一个虚拟意图,它不会启动任何活动,像这样:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
notificationBuilder.setFullScreenIntent(pendingIntent, true);

这虽然是一种hack方法,但行为还是有些道理的。如果一个应用程序试图显示全屏通知,那么它必须是一个重要的事件,比如闹钟或电话呼叫。如果手机决定不显示全屏通知,那么它应该仍然显示一些持久的东西,直到用户采取行动。

在我测试过的手机上,这个方法是有效的,但这种行为并没有被记录在文档中,所以不能保证所有情况都适用。


7
这个问题在荣耀和华为设备上被观察到。 您可以尝试使用setFullScreenIntent来修复它,并在AndroidManifest中添加权限。 代码:
PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setFullScreenIntent(pIntent, true);

AndroidManifest.xml:

<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

3

头部通知的持续时间无法更改,它是在操作系统级别设置的,取决于操作系统提供了多少时间。


内置的Android闹钟是如何工作的? 在我的旧Android 8.0上,当闹钟响起时,弹出一个悬浮通知,带有两个操作按钮(贪睡,解除),并且它会一直停留在那里,直到我采取行动。 - mkarczewski

1
持续时间无法更改。否则,它将干扰其他在队列中等待显示的悬浮通知。

0
除了前台 Heads-up 通知在几秒钟后消失的相同问题外,我遇到的另一个问题是操作按钮被折叠了。
还有其他一些帮助我的事情:
builder.setCategory(NotificationCompat.CATEGORY_CALL);

在添加了这行代码之后,通知没有在几秒钟后消失,操作按钮也没有被折叠。

0

你可以用以下方式实现:

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使用一个虚拟的pendingIntent,以相同的方式显示通知,只是没有notificationBuilder.setFullScreenIntent(pendingIntent, 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 中注册服务。


0
.setFullScreenIntent(fullScreenPendingIntent, true)

如果要让悬浮通知保持显示,您需要添加一个全屏意图


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接