【问题标题】:How do I make an Android local notification that doesn't pop-up, just shows up on the notification list?如何制作不弹出的Android本地通知,只显示在通知列表中?
【发布时间】:2019-01-21 09:00:13
【问题描述】:

我正在制作一种警报应用程序。我有一个特殊的闹钟屏幕,当闹钟时间到来时我会显示它。这很好用。但是,如果用户打盹警报,我也希望在通知列表中有一个条目。但是,当我尝试使用 NotificationBuilder 使用常规通知时,它会与我的警报活动同时出现在屏幕上(不是全屏 - 使用透明度)。必须有一种方法可以将通知放入列表中,这样用户就会在列表中看到它,但不会在闹钟时间弹出它。

【问题讨论】:

    标签: android notifications android-8.0-oreo


    【解决方案1】:

    您需要为您创建的频道使用低优先级

    NotificationManager.IMPORTANCE_LOW  or NotificationManager.IMPORTANCE_MIN 
    

    在 Android Oreo 中,您需要使用通知渠道来创建通知。

    例如:当您创建一个频道时,ID 为“Alerts”, 您可以定义它的属性,例如它的 重要性 、声音等...

    使用的各种重要性属性如下

    IMPORTANCE_DEFAULT: 默认通知重要性:到处显示,发出噪音,但不会造成视觉干扰。 常数值:3

    IMPORTANCE_HIGH: 更高的通知重要性:随处可见,制造噪音和偷看。 可以使用全屏意图。

    [这将继续显示在其他应用程序上,并再次像 whatsapp 消息通知一样] 之所以如此,是因为它的重要性。 常数值:4

    IMPORTANCE_LOW 通知重要性低:随处可见,但不打扰。 常数值:2

    IMPORTANCE_MIN 最小通知重要性:仅在首屏下方的阴影中显示。 常数值:1

    因此,一旦您定义了通道属性,它就只能由用户通过通知意图进行更改。 我在这里提到:https://stackoverflow.com/a/54199316/7039593

    使用低优先级/最小最小重要性可能会解决您的问题

    【讨论】:

      【解决方案2】:

      这是通过将通知优先级设置为 MINIMUM 来完成的。 例如:

      NotificationCompat.Builder b = new NotificationCompat.Builder(context, "MyReminder");
      b.setPriority(NotificationCompat.PRIORITY_MIN);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          String id = MyApp.getNotificationChannel(context);
          b.setChannelId(id);
      }
      

      注意 Oreo 及更高版本您还需要设置通知通道的优先级:

      final private static String MyChannelID = "MyChannel_1";
      private static NotificationChannel channel;
      
      static private String getNotificationChannel(Context context) {
          // Create the NotificationChannel, but only on API 26+ because
          // the NotificationChannel class is new and not in the support library
          if (channel == null) {
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      
                  CharSequence name = "My Reminders";
                  String description = "My Reminders pre-scheduled using My app";
                  int importance = NotificationManager.IMPORTANCE_MIN;
                  channel = new NotificationChannel(MyChannelID, name, importance);
                  channel.setDescription(description);
                  channel.enableLights(true);
                  // Sets the notification light color for notifications posted to this
                  // channel, if the device supports this feature.
                  channel.setLightColor(Color.RED);
      
                  channel.enableVibration(true);
      
                  // Register the channel with the system; you can't change the importance
                  // or other notification behaviors after this
                  getNotificationManager().createNotificationChannel(channel);
              }
          }
          return MyChannelID;
      }
      
      
      static public NotificationManager getNotificationManager() {
          Context context = App.getContext();
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              return context.getSystemService(NotificationManager.class);
          }
          else {
              return (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
          }
      }
      

      我没有检查每个优先级的行为,但 MINIMUM 和 IMPORTANCE_MIN 有效。如果您想要获得更具体的行为,您可以查看文档并尝试其他优先级。

      【讨论】:

      • 仍然弹出通知。
      【解决方案3】:

      你使用setNotificationSilent()

      像这样:

      new NotificationCompat.Builder(this)
          .setContentTitle("dasf")
          .setStyle(new NotificationCompat.BigTextStyle().bigText("adsf"))
          .setContentText(getText(R.string.text))
          .setSubText("adsf" + " " + getString(R.string.bdt))
          .setNotificationSilent()
          .setSmallIcon(R.drawable.ic_notification);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-10
        • 2013-05-30
        • 1970-01-01
        • 1970-01-01
        • 2023-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多