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.