Android notification turn on banner setting programmatically

与世无争的帅哥 提交于 2019-12-12 08:45:43

问题


Is there any ways for turning on the banners setting (display on top of the status bar) in application notification programmatically?

<pre><code>
// send notification to NotificationManager   
Notification.Builder notificationBuilder =new Notification.Builder(context);  
notificationBuilder.setSmallIcon(R.drawable.icon_32x32)  
.setLargeIcon(bitmap)  
.setContentTitle("title")  
.setContentText("content")  
.setContentIntent(contentIntent)  
.setAutoCancel(true)  
.setDefaults(Notification.DEFAULT_ALL)        
.setPriority(Notification.PRIORITY_HIGH)  
.setCategory(Notification.CATEGORY_MESSAGE);  

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){                             
notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);  
}  

NotificationManager notificationManager =  
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  
notificationManager.notify(id, notificationBuilder.build());
</code></pre>

回答1:


Same problem. I solved this issue simply adding in the AndroidManifest.xml the ACCESS_NOTIFICATION_POLICY permission:

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

Nothing more is required (at least for me).




回答2:


Usually for settings you need to get the user to permission, you can do this in your java class.

You can send the user to the settings so they can give you the permission that you want. However, it sounds to me that you are looking for heads-up notifications. If that is the case:

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

In simple words, set a priority of high in your notification manager class.



来源:https://stackoverflow.com/questions/44711788/android-notification-turn-on-banner-setting-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!