Переглянути джерело

New interface methods `getScheduled` and `getTriggered` (iOS)

Sebastián Katzer 11 роки тому
батько
коміт
8418c15290

+ 6 - 2
src/ios/APPLocalNotification.h

@@ -36,10 +36,14 @@
 - (void) isScheduled:(CDVInvokedUrlCommand*)command;
 // Check if a notification with an ID was triggered
 - (void) isTriggered:(CDVInvokedUrlCommand*)command;
-// Retrieve a list of ids from all pending notifications
+// List all ids from all pending notifications
 - (void) getScheduledIds:(CDVInvokedUrlCommand*)command;
-// Retrieve a list of ids from all triggered notifications
+// List all ids from all triggered notifications
 - (void) getTriggeredIds:(CDVInvokedUrlCommand*)command;
+// List all properties for given scheduled notifications
+- (void) getScheduled:(CDVInvokedUrlCommand*)command;
+// List all properties for given triggered notifications
+- (void) getTriggered:(CDVInvokedUrlCommand*)command;
 // Inform if the app has the permission to show notifications
 - (void) hasPermission:(CDVInvokedUrlCommand*)command;
 // Register permission to show notifications

+ 79 - 21
src/ios/APPLocalNotification.m

@@ -117,7 +117,7 @@
 }
 
 /**
- * Cancels all currently scheduled notifications.
+ * Cancel all currently scheduled notifications.
  */
 - (void) cancelAll:(CDVInvokedUrlCommand*)command
 {
@@ -157,7 +157,35 @@
 }
 
 /**
- * List of ids from all currently pending notifications.
+ * Check if a notification with an ID was triggered.
+ *
+ * @param id
+ *      The ID of the notification
+ */
+- (void) isTriggered:(CDVInvokedUrlCommand*)command
+{
+    [self.commandDelegate runInBackground:^{
+        NSString* id = [[command arguments]
+                        objectAtIndex:0];
+
+        CDVPluginResult* result;
+        UILocalNotification* notification;
+
+        notification = [[UIApplication sharedApplication]
+                        triggeredLocalNotificationWithId:id];
+
+        bool isTriggered = notification != NULL;
+
+        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
+                                     messageAsBool:isTriggered];
+
+        [self.commandDelegate sendPluginResult:result
+                                    callbackId:command.callbackId];
+    }];
+}
+
+/**
+ * List all ids from all pending notifications.
  */
 - (void) getScheduledIds:(CDVInvokedUrlCommand*)command
 {
@@ -177,27 +205,48 @@
 }
 
 /**
- * Checks wether a notification with an ID was triggered.
- *
- * @param id
- *      The ID of the notification
+ * List all ids from all triggered notifications.
  */
-- (void) isTriggered:(CDVInvokedUrlCommand*)command
+- (void) getTriggeredIds:(CDVInvokedUrlCommand*)command
 {
     [self.commandDelegate runInBackground:^{
-        NSString* id = [[command arguments]
-                        objectAtIndex:0];
-
         CDVPluginResult* result;
-        UILocalNotification* notification;
+        NSArray* triggeredIds;
 
-        notification = [[UIApplication sharedApplication]
-                        triggeredLocalNotificationWithId:id];
+        triggeredIds = [[UIApplication sharedApplication]
+                        triggeredLocalNotificationIds];
 
-        bool isTriggered = notification != NULL;
+        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
+                                    messageAsArray:triggeredIds];
+
+        [self.commandDelegate sendPluginResult:result
+                                    callbackId:command.callbackId];
+    }];
+}
+
+/**
+ * List all properties for given scheduled notifications.
+ *
+ * @param ids
+ *      The IDs of the notifications
+ */
+- (void) getScheduled:(CDVInvokedUrlCommand*)command
+{
+    [self.commandDelegate runInBackground:^{
+        NSArray* ids = command.arguments;
+        NSArray* notifications;
+        CDVPluginResult* result;
+
+        if (ids.count == 0) {
+            notifications = [[UIApplication sharedApplication]
+                             scheduledLocalNotificationOptions];
+        } else {
+            notifications = [[UIApplication sharedApplication]
+                             scheduledLocalNotificationOptions:ids];
+        }
 
         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
-                                     messageAsBool:isTriggered];
+                                    messageAsArray:notifications];
 
         [self.commandDelegate sendPluginResult:result
                                     callbackId:command.callbackId];
@@ -205,19 +254,28 @@
 }
 
 /**
- * Retrieves a list of ids from all currently triggered notifications.
+ * List all properties for given triggered notifications.
+ *
+ * @param ids
+ *      The IDs of the notifications
  */
-- (void) getTriggeredIds:(CDVInvokedUrlCommand*)command
+- (void) getTriggered:(CDVInvokedUrlCommand *)command
 {
     [self.commandDelegate runInBackground:^{
+        NSArray* ids = command.arguments;
+        NSArray* notifications;
         CDVPluginResult* result;
-        NSArray* triggeredIds;
 
-        triggeredIds = [[UIApplication sharedApplication]
-                        triggeredLocalNotificationIds];
+        if (ids.count == 0) {
+            notifications = [[UIApplication sharedApplication]
+                             triggeredLocalNotificationOptions];
+        } else {
+            notifications = [[UIApplication sharedApplication]
+                             triggeredLocalNotificationOptions:ids];
+        }
 
         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
-                                    messageAsArray:triggeredIds];
+                                    messageAsArray:notifications];
 
         [self.commandDelegate sendPluginResult:result
                                     callbackId:command.callbackId];

+ 8 - 0
src/ios/UIApplication+APPLocalNotification.h

@@ -33,5 +33,13 @@
 - (UILocalNotification*) scheduledLocalNotificationWithId:(NSString*)id;
 // Get the triggered local notification by ID
 - (UILocalNotification*) triggeredLocalNotificationWithId:(NSString*)id;
+// List of properties from all scheduled notifications
+- (NSArray*) scheduledLocalNotificationOptions;
+// List of properties from given scheduled notifications
+- (NSArray*) scheduledLocalNotificationOptions:(NSArray*)ids;
+// List of properties from all triggered notifications
+- (NSArray*) triggeredLocalNotificationOptions;
+// List of properties from given triggered notifications
+- (NSArray*) triggeredLocalNotificationOptions:(NSArray*)ids;
 
 @end

+ 98 - 14
src/ios/UIApplication+APPLocalNotification.m

@@ -97,15 +97,15 @@
  */
 - (NSArray*) triggeredLocalNotificationIds
 {
-    NSArray* triggeredNotifications = self.triggeredLocalNotifications;
-    NSMutableArray* triggeredNotificationIds = [[NSMutableArray alloc] init];
+    NSArray* notifications = self.triggeredLocalNotifications;
+    NSMutableArray* ids = [[NSMutableArray alloc] init];
 
-    for (UILocalNotification* notification in triggeredNotifications)
+    for (UILocalNotification* notification in notifications)
     {
-        [triggeredNotificationIds addObject:notification.options.id];
+        [ids addObject:notification.options.id];
     }
 
-    return triggeredNotificationIds;
+    return ids;
 }
 
 /**
@@ -113,22 +113,24 @@
  */
 - (NSArray*) scheduledLocalNotificationIds
 {
-    NSArray* scheduledNotifications = self.scheduledLocalNotifications;
-    NSMutableArray* scheduledNotificationIds = [[NSMutableArray alloc] init];
+    NSArray* notifications = self.scheduledLocalNotifications;
+    NSMutableArray* ids = [[NSMutableArray alloc] init];
 
-    for (UILocalNotification* notification in scheduledNotifications)
+    for (UILocalNotification* notification in notifications)
     {
-        if (notification)
-        {
-            [scheduledNotificationIds addObject:notification.options.id];
+        if (notification) {
+            [ids addObject:notification.options.id];
         }
     }
 
-    return scheduledNotificationIds;
+    return ids;
 }
 
 /**
  * Get the scheduled local notification by ID.
+ *
+ * @param id
+ *      Notification ID
  */
 - (UILocalNotification*) scheduledLocalNotificationWithId:(NSString*)id
 {
@@ -136,8 +138,7 @@
 
     for (UILocalNotification* notification in notifications)
     {
-        if (notification && [notification.options.id isEqualToString:id])
-        {
+        if (notification && [notification.options.id isEqualToString:id]) {
             return notification;
         }
     }
@@ -147,6 +148,9 @@
 
 /**
  * Get the triggered local notification by ID.
+ *
+ * @param id
+ *      Notification ID
  */
 - (UILocalNotification*) triggeredLocalNotificationWithId:(NSString*)id
 {
@@ -159,4 +163,84 @@
     return NULL;
 }
 
+/**
+ * List of properties from all scheduled notifications.
+ */
+- (NSArray*) scheduledLocalNotificationOptions
+{
+    NSArray* notifications = self.scheduledLocalNotifications;
+    NSMutableArray* options = [[NSMutableArray alloc] init];
+
+    for (UILocalNotification* notification in notifications)
+    {
+        if (notification) {
+            [options addObject:notification.userInfo];
+        }
+    }
+
+    return options;
+}
+
+/**
+ * List of properties from given scheduled notifications.
+ *
+ * @param ids
+ *      Notification IDs
+ */
+- (NSArray*) scheduledLocalNotificationOptions:(NSArray*)ids
+{
+    UILocalNotification* notification;
+    NSMutableArray* options = [[NSMutableArray alloc] init];
+
+    for (NSString* id in ids)
+    {
+        notification = [self scheduledLocalNotificationWithId:id];
+
+        if (notification) {
+            [options addObject:notification.userInfo];
+        }
+    }
+
+    return options;
+}
+
+/**
+ * List of properties from all triggered notifications.
+ */
+- (NSArray*) triggeredLocalNotificationOptions
+{
+    NSArray* notifications = self.triggeredLocalNotifications;
+    NSMutableArray* options = [[NSMutableArray alloc] init];
+
+    for (UILocalNotification* notification in notifications)
+    {
+        [options addObject:notification.userInfo];
+    }
+
+    return options;
+}
+
+/**
+ * List of properties from given triggered notifications.
+ *
+ * @param ids
+ *      Notification IDs
+ */
+- (NSArray*) triggeredLocalNotificationOptions:(NSArray*)ids
+{
+    UILocalNotification* notification;
+    NSMutableArray* options = [[NSMutableArray alloc] init];
+
+    for (NSString* id in ids)
+    {
+        notification = [self triggeredLocalNotificationWithId:id];
+
+        if (notification) {
+            [options addObject:notification.userInfo];
+        }
+    }
+
+    return options;
+}
+
 @end

+ 133 - 126
www/local-notification.js

@@ -102,7 +102,7 @@ exports.setDefaults = function (newDefaults) {
  *      The notification properties
  * @param {Function} callback
  *      A function to be called after the notification has been canceled
- * @param {Object} scope
+ * @param {Object?} scope
  *      The scope for the callback function
  */
 exports.add = function (props, callback, scope) {
@@ -135,7 +135,7 @@ exports.add = function (props, callback, scope) {
  *      The notification properties to update
  * @param {Function} callback
  *      A function to be called after the notification has been updated
- * @param {Object} scope
+ * @param {Object?} scope
  *      The scope for the callback function
  */
 exports.update = function (options, callback, scope) {
@@ -143,13 +143,13 @@ exports.update = function (options, callback, scope) {
 };
 
 /**
- * Clears the specified notification.
+ * Clear the specified notification.
  *
  * @param {String} id
  *      The ID of the notification
  * @param {Function} callback
  *      A function to be called after the notification has been cleared
- * @param {Object} scope
+ * @param {Object?} scope
  *      The scope for the callback function
  */
 exports.clear = function (id, callback, scope) {
@@ -159,11 +159,11 @@ exports.clear = function (id, callback, scope) {
 };
 
 /**
- * Clears all previously sheduled notifications.
+ * Clear all previously sheduled notifications.
  *
  * @param {Function} callback
  *      A function to be called after all notifications have been cleared
- * @param {Object} scope
+ * @param {Object?} scope
  *      The scope for the callback function
  */
 exports.clearAll = function (callback, scope) {
@@ -171,22 +171,20 @@ exports.clearAll = function (callback, scope) {
 };
 
 /**
- * Cancels the specified notifications.
+ * Cancel the specified notifications.
  *
  * @param {String[]} ids
  *      The IDs of the notifications
  * @param {Function} callback
  *      A function to be called after the notifications has been canceled
- * @param {Object} scope
+ * @param {Object?} scope
  *      The scope for the callback function
  */
 exports.cancel = function (ids, callback, scope) {
 
     ids = Array.isArray(ids) ? ids : [ids];
 
-    for (var i = 0; i < ids.length; i++) {
-        ids[i] = ids[i].toString();
-    }
+    ids = this.convertIds(ids);
 
     if (device.platform != 'iOS') {
         ids = ids[0];
@@ -196,11 +194,11 @@ exports.cancel = function (ids, callback, scope) {
 };
 
 /**
- * Removes all previously registered notifications.
+ * Remove all previously registered notifications.
  *
  * @param {Function} callback
  *      A function to be called after all notifications have been canceled
- * @param {Object} scope
+ * @param {Object?} scope
  *      The scope for the callback function
  */
 exports.cancelAll = function (callback, scope) {
@@ -208,39 +206,55 @@ exports.cancelAll = function (callback, scope) {
 };
 
 /**
- * Retrieves a list with all currently pending notifications.
+ * Check if a notification with an ID is scheduled.
  *
+ * @param {String} id
+ *      The ID of the notification
  * @param {Function} callback
  *      A callback function to be called with the list
- * @param {Object} scope
+ * @param {Object?} scope
  *      The scope for the callback function
  */
-exports.getScheduledIds = function (callback, scope) {
-    this.exec('getScheduledIds', null, callback, scope);
+exports.isScheduled = function (id, callback, scope) {
+    var notId = (id || '0').toString();
+
+    this.exec('isScheduled', notId, callback, scope);
 };
 
 /**
- * Checks wether a notification with an ID is scheduled.
+ * Check if a notification with an ID was triggered.
  *
  * @param {String} id
  *      The ID of the notification
  * @param {Function} callback
  *      A callback function to be called with the list
- * @param {Object} scope
+ * @param {Object?} scope
  *      The scope for the callback function
  */
-exports.isScheduled = function (id, callback, scope) {
+exports.isTriggered = function (id, callback, scope) {
     var notId = (id || '0').toString();
 
-    this.exec('isScheduled', notId, callback, scope);
+    this.exec('isTriggered', notId, callback, scope);
 };
 
 /**
- * Retrieves a list with all triggered notifications.
+ * List all currently pending notifications.
  *
  * @param {Function} callback
  *      A callback function to be called with the list
- * @param {Object} scope
+ * @param {Object?} scope
+ *      The scope for the callback function
+ */
+exports.getScheduledIds = function (callback, scope) {
+    this.exec('getScheduledIds', null, callback, scope);
+};
+
+/**
+ * List all triggered notifications.
+ *
+ * @param {Function} callback
+ *      A callback function to be called with the list
+ * @param {Object?} scope
  *      The scope for the callback function
  */
 exports.getTriggeredIds = function (callback, scope) {
@@ -248,19 +262,89 @@ exports.getTriggeredIds = function (callback, scope) {
 };
 
 /**
- * Checks wether a notification with an ID was triggered.
+ * List all properties for given scheduled notifications.
+ * If called without IDs, all notification will be returned.
  *
- * @param {String} id
- *      The ID of the notification
+ * @param {Number[]?} ids
+ *      Set of notification IDs
  * @param {Function} callback
  *      A callback function to be called with the list
- * @param {Object} scope
+ * @param {Object?} scope
  *      The scope for the callback function
  */
-exports.isTriggered = function (id, callback, scope) {
-    var notId = (id || '0').toString();
+exports.getScheduled = function () {
+    var args = Array.apply(null, arguments);
 
-    this.exec('isTriggered', notId, callback, scope);
+    if (typeof args[0] == 'function') {
+        args.unshift([]);
+    }
+
+    var ids      = args[0],
+        callback = args[1],
+        scope    = args[2];
+
+    if (!Array.isArray(ids)) {
+        ids = [ids];
+    }
+
+    ids = this.convertIds(ids);
+
+    this.exec('getScheduled', ids, callback, scope);
+};
+
+/**
+ * Retrieve the properties for all scheduled notifications.
+ *
+ * @param {Function} callback
+ *      A callback function to be called with the list
+ * @param {Object?} scope
+ *      The scope for the callback function
+ */
+exports.getAllScheduled = function (callback, scope) {
+    this.exec('getScheduled', null, callback, scope);
+};
+
+/**
+ * List all properties for given triggered notifications.
+ * If called without IDs, all notification will be returned.
+ *
+ * @param {Number[]?} ids
+ *      Set of notification IDs
+ * @param {Function} callback
+ *      A callback function to be called with the list
+ * @param {Object?} scope
+ *      The scope for the callback function
+ */
+exports.getTriggered = function () {
+    var args = Array.apply(null, arguments);
+
+    if (typeof args[0] == 'function') {
+        args.unshift([]);
+    }
+
+    var ids      = args[0],
+        callback = args[1],
+        scope    = args[2];
+
+    if (!Array.isArray(ids)) {
+        ids = [ids];
+    }
+
+    ids = this.convertIds(ids);
+
+    this.exec('getTriggered', ids, callback, scope);
+};
+
+/**
+ * Retrieve the properties for all triggered notifications.
+ *
+ * @param {Function} callback
+ *      A callback function to be called with the list
+ * @param {Object?} scope
+ *      The scope for the callback function
+ */
+exports.getAllTriggered = function (callback, scope) {
+    this.exec('getTriggered', null, callback, scope);
 };
 
 /**
@@ -317,102 +401,6 @@ exports.promptForPermission = function (callback, scope) {
     exports.registerPermission.apply(this, arguments);
 };
 
-/**
- * Add new entries to the registry (more than one)
- *
- * @param {Object} options
- *      The notification properties
- * @param {Function} callback
- *      A function to be called after the notification has been added
- * @param {Object} scope
- *      The scope for the callback function
- *
- * @return {Number}
- *      The notification's ID
- */
-exports.addMultiple = function (notifications, callback, scope) {
-    var length = notifications.length;
-    var notificationsMerged = new Array(),
-        callbackFn = this.createCallbackFn(callback, scope);
-    for (var i=0;i<length;i++){
-        var options    = this.mergeWithDefaults(notifications[i]);
-        if (options.id) {
-            options.id = options.id.toString();
-        }
-
-        if (options.date === undefined) {
-            options.date = new Date();
-        }
-
-        if (options.title) {
-            options.title = options.title.toString();
-        }
-
-        if (options.message) {
-            options.message = options.message.toString();
-        }
-
-        if (typeof options.date == 'object') {
-            options.date = Math.round(options.date.getTime()/1000);
-        }
-
-        if (['WinCE', 'Win32NT'].indexOf(device.platform) > -1) {
-            callbackFn = function (cmd) {
-                eval(cmd);
-            };
-        }
-        notificationsMerged.push(options);
-    }
-
-    cordova.exec(callbackFn, null, 'LocalNotification', 'addMultiple', notificationsMerged);
-
-    return options.id;
-};
-
-/**
- * Clear the specified notifications (more than one).
- *
- * @param {String} id
- *      The ID of the notification
- * @param {Function} callback
- *      A function to be called after the notifications has been cleared.
- * @param {Object} scope
- *      The scope for the callback function
- */
-exports.clearMultiple = function (ids, callback, scope) {
-    var length = ids.length;
-    var idArray = new Array(),
-        callbackFn = this.createCallbackFn(callback, scope);
-    for (var i=0;i<length;i++){
-        var id         = ids[i].toString();
-        idArray.push(id);
-    }
-    var callbackFn = this.createCallbackFn(callback, scope);
-    cordova.exec(callbackFn, null, 'LocalNotification', 'clearMultiple', [ids]);
-};
-
-/**
- * Cancel the specified notifications (more than one).
- *
- * @param {String} id
- *      The ID of the notification
- * @param {Function} callback
- *      A function to be called after the notifications has been canceled
- * @param {Object} scope
- *      The scope for the callback function
- */
-exports.cancelMultiple = function (ids, callback, scope) {
-    var length = ids.length;
-    var idArray = new Array(),
-        callbackFn = this.createCallbackFn(callback, scope);
-    for (var i=0;i<length;i++){
-        var id         = ids[i].toString();
-        idArray.push(id);
-    }
-    var callbackFn = this.createCallbackFn(callback, scope);
-    cordova.exec(callbackFn, null, 'LocalNotification', 'cancelMultiple', [ids]);
-};
-
 /**
  * Occurs when a notification was added.
  *
@@ -601,6 +589,25 @@ exports.createCallbackFn = function (callbackFn, scope) {
     };
 };
 
+/**
+ * @private
+ *
+ * Convert the IDs to Strings.
+ *
+ * @param {String/Number[]} ids
+ *
+ * @return Array of Strings
+ */
+exports.convertIds = function (ids) {
+    var convertedIds = [];
+
+    for (var i = 0; i < ids.length; i++) {
+        convertedIds.push(ids[i].toString());
+    }
+
+    return convertedIds;
+};
+
 /**
  * @private
  *