【问题标题】:Android Heads-up notification disappears after a few secondsAndroid Heads-up 通知会在几秒钟后消失
【发布时间】:2017-01-13 17:04:57
【问题描述】:

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

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

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

【问题讨论】:

  • AFAIK,这是不可能的。

标签: android notifications heads-up-notifications


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      在荣耀和华为设备上观察到此问题。 您可以尝试通过使用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" />
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          你可以这样做:

          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 中注册服务。

          【讨论】:

            【解决方案6】:
            .setFullScreenIntent(fullScreenPendingIntent, true)
            

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-08-26
              • 1970-01-01
              • 2021-05-12
              • 1970-01-01
              相关资源
              最近更新 更多