浏览代码

Add new methods `isScheduled` and `getScheduledIds` (#97 #95)

Sebastián Katzer 11 年之前
父节点
当前提交
4e6ec9bb39

+ 18 - 0
README.md

@@ -58,6 +58,8 @@ More informations can be found [here](https://build.phonegap.com/plugins/413).
 - [bugfix:] Callbacks for non-repeating notifications were not called if they were not created in the current app instance on iOS.
 - [bugfix:] Callbacks for non-repeating notifications were not called if they were not created in the current app instance on iOS.
 - [enhancement:] Added 'secondly' and 'minutely' as new repeat time aliases.
 - [enhancement:] Added 'secondly' and 'minutely' as new repeat time aliases.
 - [bugfix:] `sound:null` didnt work for Android. The default sound was played.
 - [bugfix:] `sound:null` didnt work for Android. The default sound was played.
+- [feature:] New interface `isScheduled` to check wether a notification with an ID is pending.
+- [feature:] New interface `getScheduledIds` to retrieve a list with all currently pending notifications.
 
 
 #### Version 0.7.2 (09.02.2014)
 #### Version 0.7.2 (09.02.2014)
 - [enhancement:] Avoid blocking the main thread (on Android) **(dpogue)**.
 - [enhancement:] Avoid blocking the main thread (on Android) **(dpogue)**.
@@ -178,6 +180,22 @@ window.plugin.notification.local.on_callback_ = function (id, state, json) {};
 ```
 ```
 **Note:** The *ontrigger* callback is only invoked in background if the app is not suspended!
 **Note:** The *ontrigger* callback is only invoked in background if the app is not suspended!
 
 
+### isScheduled()
+Checks wether a notification with an ID is scheduled. It takes the ID as an argument and a callback function to be called with the result.
+```javascript
+window.plugin.notification.local.isScheduled(id, function (isScheduled) {
+
+});
+```
+
+### getScheduledIds()
+Checks wether a notification with an ID is scheduled. It takes a callback function to be called with the result as an array.
+```javascript
+window.plugin.notification.local.getScheduledIds(function (pendingIds) {
+
+});
+```
+
 ### getDefaults()
 ### getDefaults()
 Gives an overview about all available notification properties for the platform and their default values. The function returns an object.
 Gives an overview about all available notification properties for the platform and their default values. The function returns an object.
 ```javascript
 ```javascript

+ 45 - 0
src/android/LocalNotification.java

@@ -29,6 +29,7 @@ import org.apache.cordova.CallbackContext;
 import org.apache.cordova.CordovaInterface;
 import org.apache.cordova.CordovaInterface;
 import org.apache.cordova.CordovaPlugin;
 import org.apache.cordova.CordovaPlugin;
 import org.apache.cordova.CordovaWebView;
 import org.apache.cordova.CordovaWebView;
+import org.apache.cordova.PluginResult;
 import org.json.JSONArray;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.json.JSONObject;
@@ -107,6 +108,20 @@ public class LocalNotification extends CordovaPlugin {
             return true;
             return true;
         }
         }
 
 
+        if (action.equalsIgnoreCase("isScheduled")) {
+            String id = args.optString(0);
+
+            isScheduled(id, callbackContext);
+
+            return true;
+        }
+
+        if (action.equalsIgnoreCase("getScheduledIds")) {
+            getScheduledIds(callbackContext);
+
+            return true;
+        }
+
         // Returning false results in a "MethodNotFound" error.
         // Returning false results in a "MethodNotFound" error.
         return false;
         return false;
     }
     }
@@ -187,6 +202,36 @@ public class LocalNotification extends CordovaPlugin {
         nc.cancelAll();
         nc.cancelAll();
     }
     }
 
 
+    /**
+     * Checks wether a notification with an ID is scheduled.
+     *
+     * @param id
+     *          The notification ID to be check.
+     * @param callbackContext
+     */
+    public static void isScheduled (String id, CallbackContext callbackContext) {
+        SharedPreferences settings = getSharedPreferences();
+        Map<String, ?> alarms      = settings.getAll();
+        boolean isScheduled        = alarms.containsKey(id);
+        PluginResult result        = new PluginResult(PluginResult.Status.OK, isScheduled);
+
+        callbackContext.sendPluginResult(result);
+    }
+
+    /**
+     * Retrieves a list with all currently pending notifications.
+     *
+     * @param callbackContext
+     */
+    public static void getScheduledIds (CallbackContext callbackContext) {
+        SharedPreferences settings = getSharedPreferences();
+        Map<String, ?> alarms      = settings.getAll();
+        Set<String> alarmIds       = alarms.keySet();
+        JSONArray pendingIds       = new JSONArray(alarmIds);
+
+        callbackContext.success(pendingIds);
+    }
+
     /**
     /**
      * Persist the information of this alarm to the Android Shared Preferences.
      * Persist the information of this alarm to the Android Shared Preferences.
      * This will allow the application to restore the alarm upon device reboot.
      * This will allow the application to restore the alarm upon device reboot.

+ 4 - 0
src/ios/APPLocalNotification.h

@@ -35,5 +35,9 @@ extern NSString *const kAPP_LOCALNOTIFICATION;
 - (void) cancel:(CDVInvokedUrlCommand*)command;
 - (void) cancel:(CDVInvokedUrlCommand*)command;
 // Entfernt alle registrierten Einträge
 // Entfernt alle registrierten Einträge
 - (void) cancelAll:(CDVInvokedUrlCommand*)command;
 - (void) cancelAll:(CDVInvokedUrlCommand*)command;
+// Checks wether a notification with an ID is scheduled
+- (void) isScheduled:(CDVInvokedUrlCommand*)command;
+// Retrieves a list with all currently pending notifications
+- (void) getScheduledIds:(CDVInvokedUrlCommand*)command;
 
 
 @end
 @end

+ 47 - 0
src/ios/APPLocalNotification.m

@@ -110,6 +110,53 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
     }];
     }];
 }
 }
 
 
+/**
+ * Checks wether a notification with an ID is scheduled.
+*/
+- (void) isScheduled:(CDVInvokedUrlCommand*)command
+{
+    [self.commandDelegate runInBackground:^{
+        CDVPluginResult* result;
+        NSArray* arguments = [command arguments];
+        NSString* id       = [arguments objectAtIndex:0];
+        NSString* key      = [kAPP_LOCALNOTIFICATION stringByAppendingString:id];
+        NSData* data       = [[NSUserDefaults standardUserDefaults] objectForKey:key];
+        bool isScheduled   = data != NULL;
+
+        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
+                                    messageAsBool:isScheduled];
+
+        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+    }];
+}
+
+/**
+ * Retrieves a list with all currently pending notifications.
+ */
+- (void) getScheduledIds:(CDVInvokedUrlCommand*)command
+{
+    [self.commandDelegate runInBackground:^{
+        NSMutableArray* scheduledIds = [[NSMutableArray alloc] init];
+        NSDictionary* entries        = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
+        CDVPluginResult* result;
+
+        for (NSString* key in [entries allKeys])
+        {
+            if ([key hasPrefix:kAPP_LOCALNOTIFICATION])
+            {
+                NSString* id = [key stringByReplacingOccurrencesOfString:kAPP_LOCALNOTIFICATION withString:@""];
+
+                [scheduledIds addObject:id];
+            }
+        }
+
+        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
+                                    messageAsArray:scheduledIds];
+
+        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+    }];
+}
+
 /**
 /**
  * Entfernt den zur ID passenden Eintrag.
  * Entfernt den zur ID passenden Eintrag.
  *
  *

+ 20 - 4
src/wp8/LocalNotification.cs

@@ -41,7 +41,7 @@ namespace Cordova.Extension.Commands
         /// <summary>
         /// <summary>
         /// Sets application live tile
         /// Sets application live tile
         /// </summary>
         /// </summary>
-        public void add(string jsonArgs)
+        public void add (string jsonArgs)
         {
         {
             string[] args   = JsonHelper.Deserialize<string[]>(jsonArgs);
             string[] args   = JsonHelper.Deserialize<string[]>(jsonArgs);
             Options options = JsonHelper.Deserialize<Options>(args[0]);
             Options options = JsonHelper.Deserialize<Options>(args[0]);
@@ -66,7 +66,7 @@ namespace Cordova.Extension.Commands
         /// <summary>
         /// <summary>
         /// Clears the application live tile
         /// Clears the application live tile
         /// </summary>
         /// </summary>
-        public void cancel(string jsonArgs)
+        public void cancel (string jsonArgs)
         {
         {
             string[] args         = JsonHelper.Deserialize<string[]>(jsonArgs);
             string[] args         = JsonHelper.Deserialize<string[]>(jsonArgs);
             string notificationID = args[0];
             string notificationID = args[0];
@@ -79,7 +79,7 @@ namespace Cordova.Extension.Commands
         /// <summary>
         /// <summary>
         /// Clears the application live tile
         /// Clears the application live tile
         /// </summary>
         /// </summary>
-        public void cancelAll(string jsonArgs)
+        public void cancelAll (string jsonArgs)
         {
         {
             // Application Tile is always the first Tile, even if it is not pinned to Start.
             // Application Tile is always the first Tile, even if it is not pinned to Start.
             ShellTile AppTile = ShellTile.ActiveTiles.First();
             ShellTile AppTile = ShellTile.ActiveTiles.First();
@@ -106,10 +106,26 @@ namespace Cordova.Extension.Commands
             DispatchCommandResult();
             DispatchCommandResult();
         }
         }
 
 
+        /// <summary>
+        /// Checks wether a notification with an ID is scheduled
+        /// </summary>
+        public void isScheduled (string jsonArgs)
+        {
+            DispatchCommandResult();
+        }
+
+        /// <summary>
+        /// Retrieves a list with all currently pending notifications
+        /// </summary>
+        public void getScheduledIds (string jsonArgs)
+        {
+            DispatchCommandResult();
+        }
+
         /// <summary>
         /// <summary>
         /// Creates tile data
         /// Creates tile data
         /// </summary>
         /// </summary>
-        private FlipTileData CreateTileData(Options options)
+        private FlipTileData CreateTileData (Options options)
         {
         {
             FlipTileData tile = new FlipTileData();
             FlipTileData tile = new FlipTileData();
 
 

+ 23 - 0
www/local-notification.js

@@ -145,6 +145,29 @@ LocalNotification.prototype = {
         cordova.exec(null, null, 'LocalNotification', 'cancelAll', []);
         cordova.exec(null, null, 'LocalNotification', 'cancelAll', []);
     },
     },
 
 
+    /**
+     * @async
+     *
+     * Retrieves a list with all currently pending notifications.
+     *
+     * @param {Function} callback
+     */
+    getScheduledIds: function (callback) {
+        cordova.exec(callback, null, 'LocalNotification', 'getScheduledIds', []);
+    },
+
+    /**
+     * @async
+     *
+     * Checks wether a notification with an ID is scheduled.
+     *
+     * @param {String}   id
+     * @param {Function} callback
+     */
+    isScheduled: function (id, callback) {
+        cordova.exec(callback, null, 'LocalNotification', 'isScheduled', [id.toString()]);
+    },
+
     /**
     /**
      * Occurs when a notification was added.
      * Occurs when a notification was added.
      *
      *