【问题标题】:Is there any way to keep notification always visible until user interact with?有没有办法让通知始终可见,直到用户与之交互?
【发布时间】:2019-10-07 13:40:27
【问题描述】:

Android 提醒通知会出现几秒钟,然后从主屏幕消失并停留在状态栏和通知抽屉中。有没有办法在用户交互之前在主屏幕上显示弹出通知?

【问题讨论】:

    标签: java android notifications


    【解决方案1】:

    您需要为收到通知时将打开的活动创建新活动。 收到通知后,您需要将数据中的有效负载设置为密钥对值,否则它将由 Android 操作系统处理。

    这会有所帮助-Android Activity as a dialog

    How to handle notification when app in background in Firebase

    【讨论】:

      【解决方案2】:

      如果您不介意将通知变成快餐栏,您可以展示一个在用户与之交互之前不会消失的通知:

      public void showSnakbarIndef(final View rootView, String message, String actionMessage) {
              Snackbar.make(rootView, message, Snackbar.LENGTH_INDEFINITE)
                      .setAction(actionMessage, new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                              //exit or other action according to notification
                          }
                      })
                      .show();
          }
      

      【讨论】:

        【解决方案3】:

        Notification.Builder 中使用setOngoing(true)setAutoCancel(true),如下所示:

        notification = new Notification.Builder(getApplicationContext())
                  .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                  .setSmallIcon(R.drawable.ic_launcher)
                  .setOngoing(true)
                  .setAutoCancel(true)
                  .setContentText(contentText)
                  .setContentIntent(contentIntent)
        

        【讨论】:

        • 这样用户根本无法关闭通知,除非他们完全静音频道。
        • 已编辑。谢谢@NikosHidalgo
        【解决方案4】:

        NotificationCompat.Builder 中的 setFullScreenIntent

        【讨论】:

          猜你喜欢
          • 2021-08-24
          • 1970-01-01
          • 1970-01-01
          • 2022-12-08
          • 1970-01-01
          • 1970-01-01
          • 2014-07-13
          • 2011-02-06
          • 1970-01-01
          相关资源
          最近更新 更多