I am trying to write a Cordova Plugin to have a Facebook chat head like floating icon for ionic hybrid apps which requires SYSTEM_ALERT_WINDOW Permission.
Since Android M onward requires the user to grant the permission at the first time the app starts, I am trying to use Cordova plugin's cordova.requestPermission(CordovaPlugin plugin, int requestCode, String permission)
method to prompt the user to grant permission (as in the documentation).
public class Floatie extends CordovaPlugin {
public static final String ACTION_START_FLOATIE = "startFloatie";
public static final int REQUEST_CODE = 0;
public static final String DRAW_OVER_OTHER_APPS = Manifest.permission.SYSTEM_ALERT_WINDOW;
private CallbackContext callbackContext;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals(ACTION_START_FLOATIE)) {
String message = args.getString(0);
this.callbackContext = callbackContext;
if(cordova.hasPermission(DRAW_OVER_OTHER_APPS)) {
Log.i("Floatie", "Has Permission");
}
else
{
getPermission(REQUEST_CODE);
}
return true;
}
return false;
}
protected void getPermission(int requestCode)
{
cordova.requestPermission(this, requestCode, DRAW_OVER_OTHER_APPS);
}
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException
{
for(int r:grantResults)
{
if(r == PackageManager.PERMISSION_DENIED)
{
Log.i("Floatie", "Permission Denied");
return;
}
}
Log.i("Floatie", "Permission Granted");
}
}
At the first launch, the app does not prompt permission request activity and instead the log prints "Permission Denied".
I am new to Ionic and Cordova and still couldn't work this out after spending hours on this. Any help will be highly appreciated.
Thanks in advance.
Seems that
SYSTEM_ALERT_WINDOW
is a special case which has its own permission request mechanism starting with API 23. There's a mention of this in the System Permissions documentation:And the corresponding reference entry for
SYSTEM_ALERT_WINDOW
says:So, in a nutshell, you can't use
cordova.requestPermission()
to requestSYSTEM_ALERT_WINDOW
, you've gotta send that custom intent: