4

我希望通知不会在几秒钟后消失。所以我创建了这样的通知:

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

但几秒钟后,通知继续消失。我希望通知仅在用户单击时消失。谢谢你。

4

6 回答 6

8

实际上可以使提醒通知持久化。诀窍是使用setFullScreenIntent. 如果您不希望通知具有全屏版本,则可以使用不会实际启动任何活动的虚拟意图,如下所示:

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

这是一个黑客,但这种行为是有道理的。如果应用程序试图显示全屏通知,那么它一定是重要事件,例如闹钟或电话。如果手机决定不显示全屏通知,它可能仍会显示一些持久的内容,直到用户采取行动。

这适用于我测试过的手机,但没有记录该行为,因此无法保证。

于 2017-08-04T22:00:53.220 回答
3

提示通知的持续时间无法更改它在操作系统级别设置取决于操作系统为其提供多少时间。

于 2017-01-13T17:09:21.213 回答
2

在荣耀和华为设备上观察到此问题。您可以尝试通过使用AndroidManifestsetFullScreenIntent并向其添加权限来修复它。

代码:

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" />
于 2021-04-14T00:27:28.440 回答
1

持续时间无法更改。否则,它将干扰队列中要显示的其他提醒通知。

于 2017-01-13T17:10:55.170 回答
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 中注册服务。

于 2020-05-04T13:49:03.317 回答
0
.setFullScreenIntent(fullScreenPendingIntent, true)

要保持提示通知,您需要添加全屏意图

于 2020-11-12T08:31:11.867 回答