Ver código fonte

Implement 'hasPermission' for Android (#1362)

* changed NoDisplay theme to Translucent
The ClickActivity crashes on Android 6.0.1
See https://web.archive.org/web/20151116170752/https://code.google.com/p/android-developer-preview/issues/detail?id=2353 for solution

* Implement hasPermission for Android (API version >= 22)
Alexander Köhn 8 anos atrás
pai
commit
251a7ccfe3

+ 2 - 2
plugin.xml

@@ -101,7 +101,7 @@
             <activity
                 android:name="de.appplant.cordova.plugin.localnotification.ClickActivity"
                 android:launchMode="singleInstance"
-                android:theme="@android:style/Theme.NoDisplay"
+                android:theme="@android:style/Theme.Translucent"
                 android:exported="false" />
 
             <receiver
@@ -121,7 +121,7 @@
             <activity
                 android:name="de.appplant.cordova.plugin.notification.ClickActivity"
                 android:launchMode="singleInstance"
-                android:theme="@android:style/Theme.NoDisplay"
+                android:theme="@android:style/Theme.Translucent"
                 android:exported="false" />
 
         </config-file>

+ 15 - 1
src/android/LocalNotification.java

@@ -134,7 +134,10 @@ public class LocalNotification extends CordovaPlugin {
 
         cordova.getThreadPool().execute(new Runnable() {
             public void run() {
-                if (action.equals("schedule")) {
+                if (action.equals("hasPermission")) {
+                    hasPermission(command);
+                }
+                else if (action.equals("schedule")) {
                     schedule(args);
                     command.success();
                 }
@@ -203,6 +206,17 @@ public class LocalNotification extends CordovaPlugin {
         return true;
     }
 
+    /**
+     * Ask if user has enabled permission for local notifications.
+     *
+     * @param command
+     *      The callback context used when calling back into JavaScript.
+     */
+    private void hasPermission (CallbackContext command) {
+        PluginResult result = new PluginResult(PluginResult.Status.OK, getNotificationMgr().hasPermission());
+        command.sendPluginResult(result);
+    }
+
     /**
      * Schedule multiple local notifications.
      *

+ 15 - 0
src/android/notification/Manager.java

@@ -26,6 +26,7 @@ package de.appplant.cordova.plugin.notification;
 import android.app.NotificationManager;
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.support.v4.app.NotificationManagerCompat;
 
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -68,6 +69,13 @@ public class Manager {
         return new Manager(context);
     }
 
+    /**
+     * Check if app has local notification permission.
+     */
+    public boolean hasPermission () {
+        return getNotMgrCompat().areNotificationsEnabled();
+    }
+
     /**
      * Schedule local notification specified by JSON object.
      *
@@ -454,5 +462,12 @@ public class Manager {
         return (NotificationManager) context
                 .getSystemService(Context.NOTIFICATION_SERVICE);
     }
+    
+    /**
+     * Notification manager compat for the application.
+     */
+    private NotificationManagerCompat getNotMgrCompat () {
+        return NotificationManagerCompat.from(context);
+    }
 
 }

+ 1 - 1
www/local-notification-core.js

@@ -419,7 +419,7 @@ exports.getAllTriggered = function (callback, scope) {
 exports.hasPermission = function (callback, scope) {
     var fn = this.createCallbackFn(callback, scope);
 
-    if (device.platform != 'iOS') {
+    if (device.platform != 'iOS' && device.platform != 'Android') {
         fn(true);
         return;
     }