Android:如何将我的通知放在通知区域的顶部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12758681/
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
Android: How can I put my notification on top of notification area?
提问by Meroelyth
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
的放在通知区域的顶部?
回答by Vineet Ashtekar
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. It can also be replaced with any of the following as per requirement.
setPriority(Notification.PRIORITY_MAX) 很重要。它也可以根据需要替换为以下任何一种。
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. High-priority notifications trigger the heads-up notification display.
PRIORITY_HIGH-- 主要用于重要的通信,例如包含用户特别感兴趣的内容的消息或聊天事件。高优先级通知会触发提醒通知显示。
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. 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_LOW-- 用于您希望通知用户但不太紧急的通知。低优先级通知往往显示在列表底部,这使它们成为公共或非定向社交更新等事件的不错选择:用户已要求收到有关它们的通知,但这些通知不应优先于紧急或直接沟通。
PRIORITY_MIN-- Use 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.
PRIORITY_MIN-- 用于上下文或背景信息,例如天气信息或上下文位置信息。最低优先级通知不会出现在状态栏中。用户在扩展通知栏时发现它们。
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
回答by HitOdessit
You can make your notification Ongoing
, when it will appear higher then other usual notification. But in this case user would notbe able to clear it manually.
您可以发出通知Ongoing
,当它显示的高于其他通常的通知时。但在这种情况下,用户将无法手动清除它。
In order to do this set flags to your Notification
object:
为了对您的Notification
对象执行此设置标志:
notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR
回答by Jagdeep Singh
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
另请检查此问题是否可以帮助您将通知固定到通知区域的顶部
回答by David Rawson
Please note that if you want a "heads-up" notification i.e., 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);