单挑通知 - Android Lollipop

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26451893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:29:32  来源:igfitidea点击:

Heads-up Notification - Android Lollipop

androidandroid-5.0-lollipopandroid-notifications

提问by user1719863

I'm trying to show a notification-type heads-up but I could not. What I tried

我正在尝试显示通知类型的提示,但我不能。我试过的

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());

The notification is shown only as an icon in the bar. I'm using API 21 on API 21 emulator (not L preview) I have tried:
android:Theme.Holo.NoActionBar,
android:Theme.Holo.NoActionBar.Fullscreen
and NotificationCompat.Builder

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

SDK examples are not available. does anyone know how to do it?

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

I made it working by adding:

我通过添加以下内容使其工作:

.setDefaults(Notification.DEFAULT_VIBRATE)

is this the best way?

这是最好的方法吗?

回答by Mygod

According to Notifications, you are required to set a vibrate or ringtone to make Heads-up work. However, here's a quick hack that doesn't require VIBRATE permission to produce a head-up notification:

根据Notifications,您需要设置振动或铃声才能使 Heads-up 工作。但是,这里有一个不需要 VIBRATE 权限即可生成抬头通知的快速技巧:

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

EDIT:

编辑:

Don't abuse heads-up notification. See herefor when to use heads-up notification:

不要滥用单挑通知。请参阅此处了解何时使用提醒通知:

MAX: For critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.

HIGH: Primarily for important communication, such as messages or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.

MAX:用于提醒用户注意时间紧迫或需要在他们可以继续执行特定任务之前解决的情况的关键和紧急通知。

HIGH:主要用于重要的通信,例如包含用户特别感兴趣的内容的消息或聊天事件。高优先级通知会触发提醒通知显示。

回答by MLS

According to Google: https://developer.android.com/design/patterns/notifications.html

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

If a notification's priority is flagged as High, Max, or full-screen, it gets a heads-up notification.

如果通知的优先级被标记为高、最大或全屏,则会收到提醒通知。

So the following code should generate an heads-up notification:

所以下面的代码应该生成一个提示通知:

.setPriority(Notification.PRIORITY_MAX)

Should be enough. But apparently the .setDefaults(Notification.DEFAULT_VIBRATE) has to be set also. Hopefully Google will fix this in their final release of Android 5.0.

应该够了。但显然 .setDefaults(Notification.DEFAULT_VIBRATE) 也必须设置。希望谷歌将在他们最终版本的 Android 5.0 中解决这个问题。

Not sure if bug or feature...

不确定是错误还是功能...

回答by Jorgesys

All my apps doesn′t show the Notification, for example i have a Nexus 6 with Android 5.1.1, but i think this is an issuse since Android 5.0, i had to set:

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

.setPriority(Notification.PRIORITY_HIGH)

Correctly set and manage notification priority

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

Android supports a priority flag for notifications. This flag allows you to influence where your notification appears, relative to other notifications, and helps ensure that users always see their most important notifications first. You can choose from the following priority levels when posting a notification:

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

MAXUse for critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.

HIGHUse primarily for important communication, such as message or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.

DEFAULTUse for all notifications that don't fall into any of the other priorities described here and if the application does not prioritize its own notifications

LOWUse for notifications that you want the user to be informed about, but that are less urgent. Low-priority notifications tend to show up at the bottom of the list, which makes them a good choice for things like public or undirected social updates: The user has asked to be notified about them, but these notifications should never take precedence over urgent or direct communication.

MINUse for contextual or background information such as weather information or contextual location information. Minimum-priority notifications do not appear in the status bar. The user discovers them on expanding the notification shade.

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

HIGH主要用于重要的通信,例如包含用户特别感兴趣的内容的消息或聊天事件。高优先级通知会触发提醒通知显示。

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

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

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

回答by unixia

To set the priority, use the setPriorityfunction (introduced in API 16) alongwith setDefaults(added in API 11) of Notification Builder. Choose the priority DEFAULT, HIGH, LOW, MAX, MIN as per the requirement of your app. Defaults can also be chosen here.

要设置优先级,请使用通知生成器setPriority函数(在 API 16 中引入)和setDefaults(在 API 11 中添加)。根据您的应用程序的要求选择优先级DEFAULT、 HIGH 、 LOW 、 MAX 、 MIN 。也可以在此处选择默认值。

A small snippet:

一个小片段:

notification = NotificationBuilder(service)
notification.setPriority(Notification.PRIORITY_MAX)
notification.setDefaults(Notification.DEFAULT_ALL)

回答by situee

Should set high priority and use ringtones or vibrations.

应该设置高优先级并使用铃声或振动。

notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);

Ref: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up

参考:https: //developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up

Heads-up Notifications

With Android 5.0 (API level 21), notifications can appear in a small floating window (also called a heads-up notification) when the device is active (that is, the device is unlocked and its screen is on). These notifications appear similar to the compact form of your notification, except that the heads-up notification also shows action buttons. Users can act on, or dismiss, a heads-up notification without leaving the current app.

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 priorityand uses ringtones or vibrations

单挑通知

在 Android 5.0(API 级别 21)中,当设备处于活动状态(即设备已解锁且屏幕处于开启状态)时,通知可以显示在一个小的浮动窗口中(也称为提醒通知)。这些通知的外观类似于您的通知的紧凑形式,不同之处在于提醒通知还显示操作按钮。用户可以在不离开当前应用程序的情况下对提醒通知采取行动或关闭。

可能触发提醒通知的条件示例包括:

  • 用户的活动处于全屏模式(应用程序使用 fullScreenIntent),或
  • 通知具有高优先级并使用铃声或振动

回答by Megaetron

For devices running Android 8.0 (API level 26) and higher the notification channel requires high importance

对于运行 Android 8.0(API 级别 26)及更高版本的设备,通知渠道要求非常重要

new NotificationChannel("ID", "Channel Name", NotificationManager.IMPORTANCE_HIGH);

回答by live-love

You don't need to set vibrate. You only need to set sound. It's less intrusive. I don't get any sound on mine, but the notification displays on top. Make sure you use PRIORITY_HIGH and DEFAULT_SOUND.

您无需设置振动。你只需要设置声音。它的侵入性较小。我的没有任何声音,但通知显示在顶部。确保您使用 PRIORITY_HIGH 和 DEFAULT_SOUND。

 NotificationChannel channel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel("my_channel_01",
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_HIGH);
            mNotificationManager.createNotificationChannel(channel);
        }


        Notification notification =
                new NotificationCompat.Builder(this, "notify_001")
                        .setSmallIcon(R.drawable.ic_check)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!")
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setDefaults(NotificationCompat.DEFAULT_SOUND)
                        .setChannelId("my_channel_01").build();


        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, notification);

回答by Amit Desale

Add this line in your code to display heads up notification it's only working for Lollipop version

在您的代码中添加这一行以显示提示通知它仅适用于棒棒糖版本

notificationBuilder.setPriority(Notification.PRIORITY_HIGH);