I want to enable floating notification using Android Code.Normally users didn't know about the setting. so i need to enable this as default.
3 Answers
Bad news I'm afraid.
As you probably are aware, this requires the permission SYSTEM_ALERT_WINDOW.
Since Android M google has begun locking down this permission to reduce clutter. What is a little unusual about this permission is it requires the user to go to an actual settings screen The ordinary Android M permission flow does not work for this. To quote the API:
If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen
You use the Settings class to check if you already have the permission and when you don't, you need to explain and direct the user to the relevant settings screen via 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 ever affect devices running 23+ as older devices should get the permission automatically, but don't rely on checking SDK_INT, rely instead on canDrawOverlays, as there are exceptions for some pre-marshmallow devices

- 1
- 1

- 20,807
- 14
- 73
- 124
-
9
-
4i think WhatsApp, FB is whitelisted from google for their notification – Moustafa EL-Saghier Nov 24 '20 at 11:14
I was also facing same issue and need to enable it from settings but after adding permission in manifest file it worked perfectly.
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
Tested on version 9.

- 339
- 3
- 14
-
-
2adding this enables floating notification ? this doesn't work for me :( – Keivan.k Aug 11 '20 at 04:44
-
It does yes. Make sure to double check your importance and vibrate settings when building the notification – georkost Feb 15 '21 at 10:58
I struggled with that and found a way. (In my case I'm using the OneSignal React-Native SDK)
The solution was to create a "category" (on the OneSignal console) that has the "urgent" importance :)
After that, when you send a push, you have to refer to the channel_id of this category. Doc: https://documentation.onesignal.com/docs/android-notification-categories

- 2,294
- 17
- 21