Explorar o código

Readded support for update() for iOS

Sebastián Katzer %!s(int64=8) %!d(string=hai) anos
pai
achega
c8ecc57052

+ 2 - 2
src/ios/APPLocalNotification.h

@@ -40,8 +40,8 @@
 
 // Schedule notifications
 - (void) schedule:(CDVInvokedUrlCommand*)command;
-//// Update set of notifications
-//- (void) update:(CDVInvokedUrlCommand*)command;
+// Update set of notifications
+- (void) update:(CDVInvokedUrlCommand*)command;
 // Clear notifications by id
 - (void) clear:(CDVInvokedUrlCommand*)command;
 // Clear all notifications

+ 61 - 72
src/ios/APPLocalNotification.m

@@ -104,60 +104,36 @@
      }];
 }
 
-///**
-// * Update a set of notifications.
-// *
-// * @param properties
-// *      A dict of properties for each notification
-// */
-//- (void) update:(CDVInvokedUrlCommand*)command
-//{
-//    NSArray* notifications = command.arguments;
-//
-//    [self.commandDelegate runInBackground:^{
-//        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
-//            for (NSDictionary* options in notifications) {
-//                NSNumber* id = [options objectForKey:@"id"];
-//                UNNotificationRequest* notification;
-//
-//                notification = [_center getNotificationWithId:id];
-//
-//                if (!notification)
-//                    continue;
-//
-//                //            [self updateNotification:[notification copy]
-//                //                         withOptions:options];
-//                //
-//                //            [self fireEvent:@"update" notification:notification];
-//                //
-//                //            if (notifications.count > 1) {
-//                //                [NSThread sleepForTimeInterval:0.01];
-//                //            }
-//            }
-//        } else {
-//            for (NSDictionary* options in notifications) {
-//                NSNumber* id = [options objectForKey:@"id"];
-//                UILocalNotification* notification;
-//
-//                notification = [_app localNotificationWithId:id];
-//
-//                if (!notification)
-//                    continue;
-//
-//                [self updateLocalNotification:[notification copy]
-//                                  withOptions:options];
-//
-//                [self fireEvent:@"update" localnotification:notification];
-//
-//                if (notifications.count > 1) {
-//                    [NSThread sleepForTimeInterval:0.01];
-//                }
-//            }
-//        }
-//
-//        [self execCallback:command];
-//    }];
-//}
+/**
+ * Update notifications.
+ *
+ * @param [Array<Hash>] properties A list of key-value properties.
+ *
+ * @return [ Void ]
+ */
+- (void) update:(CDVInvokedUrlCommand*)command
+{
+    NSArray* notifications = command.arguments;
+
+    [self.commandDelegate runInBackground:^{
+        for (NSDictionary* options in notifications) {
+            NSNumber* id = [options objectForKey:@"id"];
+            UNNotificationRequest* notification;
+            
+            notification = [_center getNotificationWithId:id];
+            
+            if (!notification)
+                continue;
+            
+            [self updateNotification:[notification copy]
+                         withOptions:options];
+            
+            [self fireEvent:@"update" notification:notification];
+        }
+        
+        [self execCallback:command];
+    }];
+}
 
 /**
  * Clear notifications by id.
@@ -476,36 +452,46 @@
 
 /**
  * Schedule the local notification.
+ *
+ * @param [ APPNotificationContent* ] notification The notification to schedule.
+ *
+ * @return [ Void ]
  */
 - (void) scheduleNotification:(APPNotificationContent*)notification
 {
     __weak APPLocalNotification* weakSelf  = self;
     UNNotificationRequest* request = notification.request;
+    NSString* event = [notification.request wasUpdated] ? @"update" : @"add";
 
     [_center addNotificationCategory:notification.category];
 
     [_center addNotificationRequest:request withCompletionHandler:^(NSError* e) {
         __strong APPLocalNotification* strongSelf = weakSelf;
-        [strongSelf fireEvent:@"add" notification:request];
+        [strongSelf fireEvent:event notification:request];
     }];
 }
 
-///**
-// * Update the local notification.
-// */
-//- (void) updateNotification:(UILocalNotification*)notification
-//                withOptions:(NSDictionary*)newOptions
-//{APPNotificationRequest*
-//    NSMutableDictionary* options = [notification.userInfo mutableCopy];
-//
-//    [options addEntriesFromDictionary:newOptions];
-//    [options setObject:[NSDate date] forKey:@"updatedAt"];
-//
-////    notification = [[UILocalNotification alloc]
-////                    initWithOptions:options];
-////
-////    [self scheduleLocalNotification:notification];
-//}
+/**
+ * Update the local notification.
+ *
+ * @param [ UNNotificationRequest* ] notification The notification to update.
+ * @param [ NSDictionary* ] options The options to update.
+ *
+ * @return [ Void ]
+ */
+- (void) updateNotification:(UNNotificationRequest*)notification
+                withOptions:(NSDictionary*)newOptions
+{
+    NSMutableDictionary* options = [notification.content.userInfo mutableCopy];
+
+    [options addEntriesFromDictionary:newOptions];
+    [options setObject:[NSDate date] forKey:@"updatedAt"];
+
+    APPNotificationContent*
+    newNotification = [[APPNotificationContent alloc] initWithOptions:options];
+    
+    [self scheduleNotification:newNotification];
+}
 
 #pragma mark -
 #pragma mark UNUserNotificationCenterDelegate
@@ -517,7 +503,10 @@
         willPresentNotification:(UNNotification *)notification
           withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
 {
-    [self fireEvent:@"trigger" notification:notification.request];
+    if (![notification.request wasUpdated]) {
+        [self fireEvent:@"trigger" notification:notification.request];
+    }
+
     completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
 }
 

+ 2 - 0
src/ios/UNNotificationRequest+APPLocalNotification.h

@@ -27,6 +27,8 @@
 
 // The options provided by the plug-in
 - (APPNotificationOptions*) options;
+// If the notification was updated
+- (BOOL) wasUpdated;
 // Encode the user info dict to JSON
 - (NSString*) encodeToJSON;
 

+ 10 - 0
src/ios/UNNotificationRequest+APPLocalNotification.m

@@ -64,6 +64,16 @@ static char optionsKey;
     return options;
 }
 
+/**
+ * If the notification was updated.
+ *
+ * @return [ BOOL ]
+ */
+- (BOOL) wasUpdated
+{
+    return [self.content userInfo][@"updatedAt"] != NULL;
+}
+
 /**
  * Encode the user info dict to JSON.
  */

+ 32 - 35
www/local-notification-core.js

@@ -83,41 +83,38 @@ exports.schedule = function (msgs, callback, scope, args) {
     }
 };
 
-// /**
-//  * Update existing notifications specified by IDs in options.
-//  *
-//  * @param {Object} notifications
-//  *      The notification properties to update
-//  * @param {Function} callback
-//  *      A function to be called after the notification has been updated
-//  * @param {Object?} scope
-//  *      The scope for the callback function
-//  * @param {Object?} args
-//  *      skipPermission:true schedules the notifications immediatly without
-//  *                          registering or checking for permission
-//  */
-// exports.update = function (msgs, callback, scope, args) {
-//     var fn = function(granted) {
-
-//         if (!granted) return;
-
-//         var notifications = Array.isArray(msgs) ? msgs : [msgs];
-
-//         for (var i = 0; i < notifications.length; i++) {
-//             var notification = notifications[i];
-
-//             this.convertProperties(notification);
-//         }
-
-//         this.exec('update', notifications, callback, scope);
-//     };
-
-//     if (args && args.skipPermission) {
-//         fn.call(this, true);
-//     } else {
-//         this.registerPermission(fn, this);
-//     }
-// };
+/**
+ * Schedule notifications.
+ *
+ * @param [ Array ]    notifications The notifications to schedule.
+ * @param [ Function ] callback      The function to be exec as the callback.
+ * @param [ Object ]   scope         The callback function's scope.
+ * @param [ Object ]   args          Optional flags how to schedule.
+ *
+ * @return [ Void ]
+ */
+exports.update = function (msgs, callback, scope, args) {
+    var fn = function(granted) {
+
+        if (!granted) return;
+
+        var notifications = Array.isArray(msgs) ? msgs : [msgs];
+
+        for (var i = 0; i < notifications.length; i++) {
+            var notification = notifications[i];
+
+            this.convertProperties(notification);
+        }
+
+        this.exec('update', notifications, callback, scope);
+    };
+
+    if (args && args.skipPermission) {
+        fn.call(this, true);
+    } else {
+        this.requestPermission(fn, this);
+    }
+};
 
 /**
  * Clear the specified notifications by id.

+ 13 - 16
www/local-notification.js

@@ -57,22 +57,19 @@ exports.schedule = function (notifications, callback, scope, args) {
     this.core.schedule(notifications, callback, scope, args);
 };
 
-// /**
-//  * Update existing notifications specified by IDs in options.
-//  *
-//  * @param {Object} notifications
-//  *      The notification properties to update
-//  * @param {Function} callback
-//  *      A function to be called after the notification has been updated
-//  * @param {Object?} scope
-//  *      The scope for the callback function
-//  * @param {Object?} args
-//  *      skipPermission:true schedules the notifications immediatly without
-//  *                          registering or checking for permission
-//  */
-// exports.update = function (notifications, callback, scope, args) {
-//     this.core.update(notifications, callback, scope, args);
-// };
+/**
+ * Update notifications.
+ *
+ * @param [ Array ]    notifications The notifications to schedule.
+ * @param [ Function ] callback      The function to be exec as the callback.
+ * @param [ Object ]   scope         The callback function's scope.
+ * @param [ Object ]   args          Optional flags how to schedule.
+ *
+ * @return [ Void ]
+ */
+exports.update = function (notifications, callback, scope, args) {
+    this.core.update(notifications, callback, scope, args);
+};
 
 /**
  * Clear the specified notifications by id.