Android - How to enable / enable the Floating Notifications option by default for an application using code

I want to enable floating notification using Android Code.Normally, users were not aware of the setting. so i need to enable this by default.

Configure application notifications

+8
android
source share
3 answers

I'm afraid the bad news.

As you probably know, this requires SYSTEM_ALERT_WINDOW permission.

Since Android M Google began to block this permission to reduce clutter. In this resolution, it is a little unusual that it requires the user to go to the actual settings screen. The normal Android M permission stream does not work for this . To quote the API:

If the application is intended for API level 23 or higher, the application user must explicitly grant this permission to the application through the permission control screen

You use the "Settings" class to check if you have permission , and if not, you need to explain and direct the user to the appropriate settings screen using the intent :

Intent i = new Intent(); i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); i.addCategory(Intent.CATEGORY_DEFAULT); i.setData(Uri.parse("package:" + context.getPackageName())); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); context.startActivity(i); 

This should only affect 23+ devices, since older devices should get permission automatically, but not rely on SDK_INT checking, instead rely on canDrawOverlays , as there are exceptions for some devices before marshmallows

+8
source share

I also ran into the same problem, and I need to enable it in the settings, but after adding permission to the manifest file, it worked fine.

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

Tested on version 9.

+1
source share
 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);//You need this 

Check out this page https://material.io/guidelines/patterns/notifications.html#notifications-settings-priority

-one
source share

All Articles