SachinS
SachinS

Reputation: 2253

Android - How to allow/enable "Floating notifications" setting as default for App using code

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.

App's Notification Setting

Upvotes: 13

Views: 22797

Answers (3)

amit bansode
amit bansode

Reputation: 339

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.

Upvotes: 7

Made in Moon
Made in Moon

Reputation: 2464

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

Upvotes: 0

Nick Cardoso
Nick Cardoso

Reputation: 21773

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

Upvotes: 15

Related Questions