单挑通知 - Android Lollipop

use*_*863 48 android android-notifications android-5.0-lollipop

我试图展示通知型的单挑,但我不能.我尝试了什么

final Notification.Builder notif = new Builder(getApplicationContext())
    .setContentTitle(getString(R.string.title))
    .setContentText(getString(R.string.text))
//  .setTicker(getString(R.string.tick)) removed, seems to not show at all
//  .setWhen(System.currentTimeMillis()) removed, match default
//  .setContentIntent(contentIntent) removed, I don't neet it
    .setColor(Color.parseColor(getString(R.color.yellow))) //ok
    .setSmallIcon(R.drawable.ic_small) //ok
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
//  .setCategory(Notification.CATEGORY_CALL) does not seem to make a difference
    .setPriority(Notification.PRIORITY_MAX); //does not seem to make a difference
//  .setVisibility(Notification.VISIBILITY_PRIVATE); //does not seem to make a difference

mNotificationManager.notify(Constants.NOTIFICATION_ID, notif.build());
Run Code Online (Sandbox Code Playgroud)

通知仅显示为栏中的图标.我使用的是API 21 API 21仿真器(不要使用L预览)我曾尝试:
android:Theme.Holo.NoActionBar,
android:Theme.Holo.NoActionBar.Fullscreen
NotificationCompat.Builder

SDK示例不可用.有谁知道怎么做?

我通过添加:

.setDefaults(Notification.DEFAULT_VIBRATE)
Run Code Online (Sandbox Code Playgroud)

这是最好的方法吗?

Myg*_*god 82

根据通知,您需要设置振动或铃声以使Heads-up工作.但是,这是一个快速入侵,不需要VIBRATE权限来生成平视通知:

notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);
Run Code Online (Sandbox Code Playgroud)

编辑:

不要滥用抬头通知.请参阅此处了解何时使用抬头通知:

MAX:用于关键和紧急通知,提醒用户注意时间紧迫或需要解决的条件才能继续执行特定任务.

HIGH:主要用于重要通信,例如消息或聊天事件,其内容对用户特别有用.高优先级通知会触发抬头通知显示.


小智 15

根据谷歌:https: //developer.android.com/design/patterns/notifications.html

如果通知的优先级标记为"高","最大"或"全屏",则会收到抬头通知.

因此,以下代码应生成抬头通知:

.setPriority(Notification.PRIORITY_MAX)
Run Code Online (Sandbox Code Playgroud)

应该够了.但显然还必须设置.setDefaults(Notification.DEFAULT_VIBRATE).希望Google能够在Android 5.0的最终版本中修复此问题.

不确定是否有bug或功能......

  • 似乎必须在通知上设置声音和/或振动以使其显示为"抬头".这实际上是有道理的,因为看到一个无声的"抬头"通知会很奇怪,否则它可能并不明显是系统通知. (7认同)
  • 虽然文档很少,但它在这里:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up`通知具有高优先级**,**使用铃声或振动` (3认同)
  • "Notification.PRIORITY_HIGH"足够高,您不必(也可能不应该)设置"PRIORITY_MAX",因为您希望将通知显示为单挑. (2认同)

Jor*_*sys 8

我的所有应用都没有显示通知,例如我有一个带有Android 5.1.1的Nexus 6,但我认为这是自Android 5.0以来的一个问题,我必须设置:

.setPriority(Notification.PRIORITY_HIGH)
Run Code Online (Sandbox Code Playgroud)

正确设置和管理通知优先级

Android支持通知的优先级标志.此标记允许您相对于其他通知影响通知的显示位置,并有助于确保用户始终首先看到最重要的通知.发布通知时,您可以从以下优先级中进行选择:

MAX用于关键和紧急通知,提醒用户注意时间紧迫或需要解决的条件才能继续执行特定任务.

HIGH主要用于重要通信,例如消息或聊天事件,其内容对用户特别有用.高优先级通知会触发抬头通知显示.

DEFAULT用于所有不属于此处描述的任何其他优先级的通知,如果应用程序未优先处理其自己的通知

LOW用于您希望通知用户但不太紧急的通知.低优先级通知往往显示在列表的底部,这使得它们成为公共或无向社交更新等内容的良好选择:用户已要求收到有关它们的通知,但这些通知不应优先于紧急或直接沟通.

MIN用于上下文或背景信息,例如天气信息或上下文位置信息.最低优先级通知不会出现在状态栏中.用户在扩展通知阴影时发现它们.


uni*_*xia 6

要设置优先级,请使用setPriority函数(在API 16中引入)以及Notification Builder的setDefaults(在API 11中添加).根据您的应用需求选择优先级DEFAULT,HIGH,LOW,MAX,MIN.也可以在此处选择默认值.

一个小片段:

notification = NotificationBuilder(service)
notification.setPriority(Notification.PRIORITY_MAX)
notification.setDefaults(Notification.DEFAULT_ALL)
Run Code Online (Sandbox Code Playgroud)