简体   繁体   English

Android:如何将通知放在通知区域之上?

[英]Android: How can I put my notification on top of notification area?

I'm trying to put my notification on top of notification area. 我正在尝试将通知放在通知区域之上。

A solution is to set the parameter "when" to my notification object with a future time like: 解决方案是将参数“when”设置为我的通知对象,其未来时间如下:

notification.when = System.currentTimeMills()*2; 

The code that I'm using in this: 我正在使用的代码:

        long timeNotification = System.currentTimeMillis()*2;
        Notification notification = new Notification(statusIcon,c.getResources().getString(R.string.app_name),timeNotification);
        notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        notification.when = timeNotification;
        notification.priority = Notification.PRIORITY_MAX;

but some apps (like Facebook) are able to put a simple notification with their current time over mine. 但是有些应用程序(比如Facebook)可以用他们当前的时间对我进行简单的通知。

If I refresh my notification it remains under these ones. 如果我刷新我的通知,它仍然在这些通知之下。

What parameters I have to set to put my Notification to the top of the notifications area? 我必须设置哪些参数才能将我的Notification放在通知区域的顶部?

You should do this. 你应该做这个。 Other answers seem outdated. 其他答案似乎已过时。

NotificationCompat.Builder mBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.some_small_icon)
            .setContentTitle("Title")
            .setContentText("This is a test notification with MAX priority")
            .setPriority(Notification.PRIORITY_MAX);

setPriority(Notification.PRIORITY_MAX) is important. setPriority(Notification.PRIORITY_MAX)很重要。 It can also be replaced with any of the following as per requirement. 根据要求,它也可以替换为以下任何一种。

Different Priority Levels Info: 不同的优先级信息:

PRIORITY_MAX -- Use 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. PRIORITY_MAX - 用于关键和紧急通知,提醒用户注意时间紧迫或需要在继续执行特定任务之前需要解决的情况。

PRIORITY_HIGH -- Use primarily for important communication, such as message or chat events with content that is particularly interesting for the user. PRIORITY_HIGH - 主要用于重要的通信,例如消息或聊天事件,其内容对用户特别有用。 High-priority notifications trigger the heads-up notification display. 高优先级通知会触发抬头通知显示。

PRIORITY_DEFAULT -- Use for all notifications that don't fall into any of the other priorities described here. PRIORITY_DEFAULT - 用于不属于此处描述的任何其他优先级的所有通知。

PRIORITY_LOW -- Use for notifications that you want the user to be informed about, but that are less urgent. PRIORITY_LOW - 用于通知您希望用户了解但不太紧急的通知。 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. 低优先级通知往往显示在列表的底部,这使得它们成为公共或无向社交更新等内容的良好选择:用户已要求收到有关它们的通知,但这些通知不应优先于紧急或直接沟通。

PRIORITY_MIN -- Use for contextual or background information such as weather information or contextual location information. PRIORITY_MIN - 用于上下文或背景信息,例如天气信息或上下文位置信息。 Minimum-priority notifications do not appear in the status bar. 最低优先级通知不会出现在状态栏中。 The user discovers them on expanding the notification shade. 用户在扩展通知阴影时发现它们。

For more details check the following link: http://developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority 有关更多详细信息,请查看以下链接: http//developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority

You can make your notification Ongoing , when it will appear higher then other usual notification. 您可以将通知Ongoing ,此时它会显示为高于其他常规通知。 But in this case user would not be able to clear it manually. 但在这种情况下,用户将无法手动清除它。

In order to do this set flags to your Notification object: 为此,请设置Notification对象的标志:

notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR

Try setting priority of the notification to high 尝试将通知的priority设置为高

documentation > Notification Priority 文档> 通知优先级

Also check this question may it could help you Pin Notification to top of notification area 同时检查此问题是否可以帮助您将通知固定到通知区域的顶部

Please note that if you want a "heads-up" notification ie, one that displays over the top of the current user window you must have the following set in your builder: 请注意,如果您需要“抬头”通知,即显示在当前用户窗口顶部的通知,则必须在构建器中设置以下内容:

setDefaults(NotificationCompat.DEFAULT_VIBRATE)

The reference is in the javadoc : 引用位于javadoc中

A notification that vibrates is more likely to be presented as a heads-up notification, on some platforms. 在某些平台上,振动通知更有可能作为抬头通知呈现。

Complete example for a heads-up notification: 提前通知的完整示例:

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.some_small_icon)
            .setContentTitle("Title")
            .setContentText("This is a test notification with MAX priority")
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(NotificationCompat.DEFAULT_VIBRATE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM