Browse Source

Callbacks for non-repeating notifications were not called if they were not created in the current app instance. Solves #87 partially.

Sebastián Katzer 11 years ago
parent
commit
d618a13f87
2 changed files with 43 additions and 3 deletions
  1. 1 0
      README.md
  2. 42 3
      src/ios/APPLocalNotification.m

+ 1 - 0
README.md

@@ -53,6 +53,7 @@ More informations can be found [here](https://build.phonegap.com/plugins/413).
 #### Version 0.7.3 (not yet released)
 - [bugfix:] cancel callbacks have not been fired after all notifications have been canceled on iOS.
 - [change:] The `oncancel` callback will be called at last if `autoCancel` is set to true (iOS).
+- [bugfix:] Callbacks for non-repeating notifications were not called if they were not created in the current app instance on iOS.
 
 #### Version 0.7.2 (09.02.2014)
 - [enhancement:] Avoid blocking the main thread (on Android) **(dpogue)**.

+ 42 - 3
src/ios/APPLocalNotification.m

@@ -118,8 +118,8 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
     if (![self strIsNullOrEmpty:id])
     {
         NSString* key = ([id hasPrefix:kAPP_LOCALNOTIFICATION])
-                        ? id
-                        : [kAPP_LOCALNOTIFICATION stringByAppendingString:id];
+        ? id
+        : [kAPP_LOCALNOTIFICATION stringByAppendingString:id];
 
         NSData* data  = [[NSUserDefaults standardUserDefaults] objectForKey:key];
 
@@ -140,6 +140,37 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
     }
 }
 
+/**
+ * Entfernt alle Meldungen, die älter als x Sekunden sind.
+ *
+ * @param {float} seconds
+ */
+- (void) cancelAllNotificationsWhichAreOlderThen:(float)seconds
+{
+    NSDictionary* entries = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
+    NSDate* now           = [NSDate date];
+
+    for (NSString* key in [entries allKeys])
+    {
+        if ([key hasPrefix:kAPP_LOCALNOTIFICATION])
+        {
+            NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
+
+            if (data)
+            {
+                UILocalNotification* notification = [NSKeyedUnarchiver unarchiveObjectWithData:data];
+
+                NSTimeInterval fireDateDistance   = [now timeIntervalSinceDate:notification.fireDate];
+                NSString* id                      = [notification.userInfo objectForKey:@"id"];
+
+                if (notification.repeatInterval == NSEraCalendarUnit && fireDateDistance > seconds) {
+                    [self cancelNotificationWithId:id fireEvent:YES];
+                }
+            }
+        }
+    }
+}
+
 /**
  * Archiviert die Meldungen, sodass sie später abgerufen werden kann.
  *
@@ -179,7 +210,7 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
     [repeatDict setObject:[NSNumber numberWithInt:NSYearCalendarUnit]  forKey:@"yearly"];
 #endif
 
-    [repeatDict setObject:[NSNumber numberWithInt:0]                   forKey:@""];
+    [repeatDict setObject:[NSNumber numberWithInt:NSEraCalendarUnit]   forKey:@""];
 
     return repeatDict;
 }
@@ -277,6 +308,14 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveLocalNotification:) name:CDVLocalNotification object:nil];
 }
 
+/**
+ * Löscht alle single-repeat Notifications, die älter als 5 Tage sind.
+ */
+- (void) onAppTerminate
+{
+    [self cancelAllNotificationsWhichAreOlderThen:10];
+}
+
 /**
  * Hilfsmethode gibt an, ob er String NULL oder Empty ist.
  */