cwai
cwai

Reputation: 111

Android notification turn on banner setting programmatically

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

enter image description here

<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>

Upvotes: 11

Views: 8448

Answers (2)

Timon
Timon

Reputation: 76

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).

Upvotes: 3

dfabiano
dfabiano

Reputation: 82

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.

Upvotes: 1

Related Questions