Ver código fonte

Queue events if device not yet ready and save launch details on windows

Sebastián Katzer 8 anos atrás
pai
commit
396829d83f

+ 2 - 2
src/ios/APPLocalNotification.h

@@ -26,9 +26,9 @@
 @interface APPLocalNotification : CDVPlugin <UNUserNotificationCenterDelegate>
 
 // Set launchDetails object
-- (void) launchDetails:(CDVInvokedUrlCommand*)command;
+- (void) launch:(CDVInvokedUrlCommand*)command;
 // Execute all queued events
-- (void) deviceready:(CDVInvokedUrlCommand*)command;
+- (void) ready:(CDVInvokedUrlCommand*)command;
 
 // Check permission to show notifications
 - (void) check:(CDVInvokedUrlCommand*)command;

+ 11 - 12
src/ios/APPLocalNotification.m

@@ -47,13 +47,12 @@
  *
  * @return [ Void ]
  */
-- (void) launchDetails:(CDVInvokedUrlCommand*)command
+- (void) launch:(CDVInvokedUrlCommand*)command
 {
     if (!_launchDetails)
         return;
 
     NSString* js;
-
     js = [NSString stringWithFormat:
           @"cordova.plugins.notification.local.launchDetails = {id:%@, action:'%@'}",
           _launchDetails[0], _launchDetails[1]];
@@ -68,7 +67,7 @@
  *
  * @return [ Void ]
  */
-- (void) deviceready:(CDVInvokedUrlCommand*)command
+- (void) ready:(CDVInvokedUrlCommand*)command
 {
     deviceready = YES;
 
@@ -119,18 +118,18 @@
         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];
     }];
 }
@@ -240,7 +239,7 @@
             default:
                 type = @"unknown";
         }
-        
+
         CDVPluginResult* result;
         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                      messageAsString:type];
@@ -316,11 +315,11 @@
 
         NSArray* notifications;
         notifications = [_center getNotificationOptionsById:ids];
-        
+
         CDVPluginResult* result;
         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                messageAsDictionary:[notifications firstObject]];
-        
+
         [self.commandDelegate sendPluginResult:result
                                     callbackId:command.callbackId];
     }];
@@ -489,7 +488,7 @@
 
     APPNotificationContent*
     newNotification = [[APPNotificationContent alloc] initWithOptions:options];
-    
+
     [self scheduleNotification:newNotification];
 }
 

+ 44 - 1
src/windows/LocalNotificationProxy.js

@@ -22,7 +22,41 @@
 var LocalNotification = LocalNotificationProxy.LocalNotification,
        ActivationKind = Windows.ApplicationModel.Activation.ActivationKind;
 
-var impl = new LocalNotificationProxy.LocalNotificationProxy();
+var impl  = new LocalNotificationProxy.LocalNotificationProxy(),
+    queue = [],
+    ready = false;
+
+/**
+ * Set launchDetails object.
+ *
+ * @param [ Function ] success Success callback
+ * @param [ Function ] error   Error callback
+ * @param [ Array ]    args    Interface arguments
+ *
+ * @return [ Void ]
+ */
+exports.launch = function (success, error, args) {
+    var plugin = cordova.plugins.notification.local;
+
+    if (args.length === 0 || plugin.launchDetails) return;
+
+    plugin.launchDetails = { id: args[0], action: args[1] };
+};
+
+/**
+ * To execute all queued events.
+ *
+ * @return [ Void ]
+ */
+exports.ready = function () {
+    ready = true;
+
+    for (var i = 0; i < queue.length; i++) {
+        exports.fireEvent.apply(exports, queue[i]);
+    }
+
+    queue = [];
+};
 
 /**
  * Check permission to show notifications.
@@ -301,6 +335,10 @@ exports.clicked = function (xml, input) {
         meta.text = input.first().current.value;
     }
 
+    if (!ready) {
+        exports.launch(null, null, [toast.id, event]);
+    }
+
     exports.fireEvent(event, toast, meta);
 };
 
@@ -317,6 +355,11 @@ exports.fireEvent = function (event, toast, data) {
     var meta   = Object.assign({ event: event }, data),
         plugin = cordova.plugins.notification.local.core;
 
+    if (!ready) {
+        queue.push(arguments);
+        return;
+    }
+
     if (toast) {
         plugin.fireEvent(event, exports.clone(toast), meta);
     } else {

+ 2 - 2
www/local-notification-util.js

@@ -362,13 +362,13 @@ exports.exec = function (action, args, callback, scope) {
 channel.deviceready.subscribe(function () {
     // Device is ready now, the listeners are registered
     // and all queued events can be executed.
-    exports.exec('deviceready');
+    exports.exec('ready');
 });
 
 // Called before 'deviceready' event
 channel.onCordovaReady.subscribe(function () {
     // Set launchDetails object
-    exports.exec('launchDetails');
+    exports.exec('launch');
     // Device plugin is ready now
     channel.onCordovaInfoReady.subscribe(function () {
         // Merge platform specifics into defaults