Quellcode durchsuchen

Click event on iOS wasn't fired if app was not running (solves #87).

Sebastián Katzer vor 11 Jahren
Ursprung
Commit
3afc1f04a8
4 geänderte Dateien mit 49 neuen und 22 gelöschten Zeilen
  1. 1 0
      README.md
  2. 2 0
      src/ios/APPLocalNotification.h
  3. 42 22
      src/ios/APPLocalNotification.m
  4. 4 0
      www/local-notification.js

+ 1 - 0
README.md

@@ -64,6 +64,7 @@ More informations can be found [here](https://build.phonegap.com/plugins/413).
 - [feature:] New interface `getScheduledIds` to retrieve a list with all currently pending notifications.
 - [enhancement:] Support for bigview style notifications for Android devices.
 - [bugfix:] Sound didnt play properly on iOS/Android.
+- [bugfix:] click event on iOS wasn't fired if app was not running.
 
 #### Version 0.7.2 (09.02.2014)
 - [enhancement:] Avoid blocking the main thread (on Android) **(dpogue)**.

+ 2 - 0
src/ios/APPLocalNotification.h

@@ -24,6 +24,8 @@
 
 @interface APPLocalNotification : CDVPlugin
 
+// Executes all queued events
+- (void) deviceready:(CDVInvokedUrlCommand*)command;
 // Schedules a new local notification
 - (void) add:(CDVInvokedUrlCommand*)command;
 // Cancels a given local notification

+ 42 - 22
src/ios/APPLocalNotification.m

@@ -54,8 +54,33 @@
 
 @end
 
+@interface APPLocalNotification ()
+
+// All events will be queued until deviceready has been fired
+@property (readwrite, assign) BOOL deviceready;
+// Event queue
+@property (readonly, nonatomic, retain) NSMutableArray* eventQueue;
+
+@end
+
 @implementation APPLocalNotification
 
+@synthesize deviceready, eventQueue;
+
+/**
+ * Executes all queued events.
+ */
+- (void) deviceready:(CDVInvokedUrlCommand*)command
+{
+    deviceready = YES;
+
+    for (NSString* js in eventQueue) {
+        [self.commandDelegate evalJs:js];
+    }
+
+    [eventQueue removeAllObjects];
+}
+
 /**
  * Schedules a new local notification.
  *
@@ -109,8 +134,7 @@
         NSArray* notifications = [[UIApplication sharedApplication]
                                   scheduledLocalNotifications];
 
-        for (UILocalNotification* notification in notifications)
-        {
+        for (UILocalNotification* notification in notifications) {
             [self cancelNotification:notification fireEvent:YES];
         }
 
@@ -214,8 +238,7 @@
     [[UIApplication sharedApplication]
      cancelLocalNotification:notification];
 
-    if (fireEvent)
-    {
+    if (fireEvent) {
         [self fireEvent:@"cancel" id:id json:json];
     }
 }
@@ -339,13 +362,10 @@
 
     if (![self stringIsNullOrEmpty:msg])
     {
-        if (![self stringIsNullOrEmpty:title])
-        {
+        if (![self stringIsNullOrEmpty:title]) {
             notification.alertBody = [NSString stringWithFormat:
                                       @"%@\n%@", title, msg];
-        }
-        else
-        {
+        } else {
             notification.alertBody = msg;
         }
     }
@@ -354,9 +374,7 @@
     {
         if ([sound isEqualToString:@""]) {
             notification.soundName = UILocalNotificationDefaultSoundName;
-        }
-        else
-        {
+        } else {
             notification.soundName = sound;
         }
     }
@@ -382,8 +400,7 @@
     NSTimeInterval fireDateDistance = [now timeIntervalSinceDate:fireDate];
     NSString* event = (fireDateDistance < 1) ? @"trigger" : @"click";
 
-    if (autoCancel && [event isEqualToString:@"click"])
-    {
+    if (autoCancel && [event isEqualToString:@"click"]) {
         [self cancelNotification:notification fireEvent:YES];
     }
 
@@ -400,8 +417,7 @@
     UILocalNotification* localNotification = [launchOptions objectForKey:
                                               UIApplicationLaunchOptionsLocalNotificationKey];
 
-    if (localNotification)
-    {
+    if (localNotification) {
         [self didReceiveLocalNotification:
          [NSNotification notificationWithName:CDVLocalNotification
                                        object:localNotification]];
@@ -418,6 +434,8 @@
     NSNotificationCenter* notificationCenter = [NSNotificationCenter
                                                 defaultCenter];
 
+    eventQueue = [[NSMutableArray alloc] init];
+
     [notificationCenter addObserver:self
                            selector:@selector(didReceiveLocalNotification:)
                                name:CDVLocalNotification
@@ -425,7 +443,7 @@
 
     [notificationCenter addObserver:self
                            selector:@selector(didFinishLaunchingWithOptions:)
-                               name:CDVLocalNotification
+                               name:UIApplicationDidFinishLaunchingNotification
                              object:nil];
 }
 
@@ -446,13 +464,11 @@
  */
 - (BOOL) stringIsNullOrEmpty:(NSString*)str
 {
-    if (str == (NSString*)[NSNull null])
-    {
+    if (str == (NSString*)[NSNull null]) {
         return YES;
     }
 
-    if ([str isEqualToString:@""])
-    {
+    if ([str isEqualToString:@""]) {
         return YES;
     }
 
@@ -523,7 +539,11 @@
                     @"setTimeout('plugin.notification.local.on%@(%@)',0)",
                     event, params];
 
-    [self.commandDelegate evalJs:js];
+    if (deviceready) {
+        [self.commandDelegate evalJs:js];
+    } else {
+        [self.eventQueue addObject:js];
+    }
 }
 
 @end

+ 4 - 0
www/local-notification.js

@@ -212,4 +212,8 @@ channel.onCordovaReady.subscribe( function () {
     plugin.applyPlatformSpecificOptions();
 });
 
+channel.deviceready.subscribe( function () {
+    cordova.exec(null, null, 'LocalNotification', 'deviceready', []);
+});
+
 module.exports = plugin;