延长 Lollipops "heads-up" 通知持续时间

Extend Lollipops "heads-up" notification duration

一直在尝试找到一种方法来创建持久提醒通知,就像接收 phone 电话或视频群聊电话一样。这些是否仅限于 Google/system 个应用程序?

我试过:

似乎在任何地方都找不到这样做。

它对我有用:

  • 将正在进行的标志设置为真
  • 设置全屏意图
  • 设置优先级为PRIORITY_HIGH
  • 将类别设置为 CATEGORY_CALL

Heads-Up Notifications(API 21 级):

Examples of conditions that may trigger heads-up notifications include:

  • The user's activity is in fullscreen mode (the app uses fullScreenIntent), or
  • The notification has high priority and uses ringtones or vibrations

第二种情况足以让我显示 heads-up 通知。这是一个示例代码:

Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("Title")
        .setContentText("Message")
        .setSmallIcon(R.drawable.ic_notification)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.your_sound))
//        .setOngoing(true)
        .build();

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(42, notification);

运行 这个示例代码每秒一次(取决于 your_sound.mp3 持续时间...)就成功了。

请注意,您可能希望在第一次播放后播放 silent.mp3。

只是,将setOngoing加到true。 .setOngoing(true);,这就是您在通知面板中制作通知(注意)的方式,显示时间更长,现在也没有通知不可滑动。