Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

like image 227
SachinS Avatar asked Mar 04 '17 15:03

SachinS


People also ask

Are notifications enabled by default on Android?

Android Push Notifications For Android, the notifications by default appear on the lock screen, and when the phone is unlocked, are visible as small icons on the notification bar at the top of the screen.

How do I force app notifications?

If you don't find the relevant settings in the app, you can turn on Android notifications for specific apps under Settings > Apps & Notifications > [App name] > Notifications.


1 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

like image 166
Nick Cardoso Avatar answered Sep 28 '22 21:09

Nick Cardoso