Commit 10c6988c by Sebastián Katzer

`cancel` on iOS did not work (#37)

parent adf62bf2
...@@ -51,6 +51,7 @@ More informations can be found [here](https://build.phonegap.com/plugins/331). ...@@ -51,6 +51,7 @@ More informations can be found [here](https://build.phonegap.com/plugins/331).
- [bugfix:] Removed extra line break on iOS if `title` is null or empty. - [bugfix:] Removed extra line break on iOS if `title` is null or empty.
- [bugfix:] Notification on iOS will be canceled if a new one with the same ID was added. - [bugfix:] Notification on iOS will be canceled if a new one with the same ID was added.
- [enhancement:] Added `autoCancel` flag. - [enhancement:] Added `autoCancel` flag.
- [bugfix:] `cancel` on iOS did not work.
#### Version 0.6.3 (12.12.2013) #### Version 0.6.3 (12.12.2013)
- [bugfix:] Black screen on Android. - [bugfix:] Black screen on Android.
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
KIND, either express or implied. See the License for the KIND, either express or implied. See the License for the
specific language governing permissions and limitations specific language governing permissions and limitations
under the License. under the License.
*/ */
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h> #import <Cordova/CDVPlugin.h>
...@@ -26,9 +26,12 @@ ...@@ -26,9 +26,12 @@
} }
// Schlüssel-Präfix für alle archivierten Meldungen
extern NSString *const kAPP_LOCALNOTIFICATION;
// Fügt einen neuen Eintrag hinzu // Fügt einen neuen Eintrag hinzu
- (void) add:(CDVInvokedUrlCommand*)command; - (void) add:(CDVInvokedUrlCommand*)command;
// Entfernt den anhand der ID angegebenen Eintrag // Entfernt die zur ID passende Meldung
- (void) cancel:(CDVInvokedUrlCommand*)command; - (void) cancel:(CDVInvokedUrlCommand*)command;
// Entfernt alle registrierten Einträge // Entfernt alle registrierten Einträge
- (void) cancelAll:(CDVInvokedUrlCommand*)command; - (void) cancelAll:(CDVInvokedUrlCommand*)command;
......
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
@interface APPLocalNotification (Private) @interface APPLocalNotification (Private)
// Archiviert die Meldungen, sodass sie später abgerufen werden kann
- (void) archiveNotification:(UILocalNotification*)notification;
// Nachschlagewerk für Zeitintervallangaben
- (NSMutableDictionary*) repeatDict; - (NSMutableDictionary*) repeatDict;
// Alle zusätzlichen Metadaten der Notification als Hash // Alle zusätzlichen Metadaten der Notification als Hash
- (NSDictionary*) userDict:(NSMutableDictionary*)options; - (NSDictionary*) userDict:(NSMutableDictionary*)options;
...@@ -30,9 +33,14 @@ ...@@ -30,9 +33,14 @@
- (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options; - (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options;
// Ruft die JS-Callbacks auf, nachdem eine Notification eingegangen ist // Ruft die JS-Callbacks auf, nachdem eine Notification eingegangen ist
- (void) didReceiveLocalNotification:(NSNotification*)localNotification; - (void) didReceiveLocalNotification:(NSNotification*)localNotification;
// Hilfsmethode gibt an, ob er String NULL oder Empty ist
- (BOOL) strIsNullOrEmpty:(NSString*)str;
@end @end
// Schlüssel-Präfix für alle archivierten Meldungen
NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
@implementation APPLocalNotification @implementation APPLocalNotification
...@@ -50,13 +58,14 @@ ...@@ -50,13 +58,14 @@
NSString* notificationId = [notification.userInfo objectForKey:@"id"]; NSString* notificationId = [notification.userInfo objectForKey:@"id"];
[self cancelNotificationWithId:notificationId]; [self cancelNotificationWithId:notificationId];
[self archiveNotification:notification];
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}]; }];
} }
/** /**
* Entfernt den anhand der ID angegebenen Eintrag. * Entfernt die zur ID passende Meldung.
* *
* @param {NSString} id Die ID der Notification * @param {NSString} id Die ID der Notification
*/ */
...@@ -75,36 +84,65 @@ ...@@ -75,36 +84,65 @@
*/ */
- (void) cancelAll:(CDVInvokedUrlCommand*)command - (void) cancelAll:(CDVInvokedUrlCommand*)command
{ {
[self.commandDelegate runInBackground:^{
NSDictionary* entries = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
for (NSString* key in [entries allKeys])
{
if ([key hasPrefix:kAPP_LOCALNOTIFICATION])
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}
}
[[NSUserDefaults standardUserDefaults] synchronize];
[[UIApplication sharedApplication] cancelAllLocalNotifications]; [[UIApplication sharedApplication] cancelAllLocalNotifications];
}];
} }
/** /**
* Entfernt den anhand der ID angegebenen Eintrag. * Entfernt den zur ID passenden Eintrag.
* *
* @param {NSString} id Die ID der Notification * @param {NSString} id Die ID der Notification
*/ */
- (void) cancelNotificationWithId:(NSString*)notificationId - (void) cancelNotificationWithId:(NSString*)id
{ {
NSArray* notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; if (![self strIsNullOrEmpty:id])
{
NSString* key = [kAPP_LOCALNOTIFICATION stringByAppendingString:id];
NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
if (notificationId == (NSString*)[NSNull null] || [notificationId isEqualToString:@""]) if (data)
{ {
return; UILocalNotification* notification = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
} }
}
for (UILocalNotification* notification in notifications) /**
{ * Archiviert die Meldungen, sodass sie später abgerufen werden kann.
*
* @param {UILocalNotification} notification
*/
- (void) archiveNotification:(UILocalNotification*)notification
{
NSString* id = [notification.userInfo objectForKey:@"id"]; NSString* id = [notification.userInfo objectForKey:@"id"];
if ([notificationId isEqualToString:id]) if (![self strIsNullOrEmpty:id])
{ {
[[UIApplication sharedApplication] cancelLocalNotification:notification]; NSData* data = [NSKeyedArchiver archivedDataWithRootObject:notification];
} NSString* key = [kAPP_LOCALNOTIFICATION stringByAppendingString:id];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
} }
} }
/** /**
* @private * Nachschlagewerk für Zeitintervallangaben.
*/ */
- (NSMutableDictionary*) repeatDict - (NSMutableDictionary*) repeatDict
{ {
...@@ -139,7 +177,7 @@ ...@@ -139,7 +177,7 @@
{ {
UILocalNotification* notification = [[UILocalNotification alloc] init]; UILocalNotification* notification = [[UILocalNotification alloc] init];
double timestamp = [[options objectForKey:@"date"] doubleValue] + 1; double timestamp = [[options objectForKey:@"date"] doubleValue];
NSString* msg = [options objectForKey:@"message"]; NSString* msg = [options objectForKey:@"message"];
NSString* title = [options objectForKey:@"title"]; NSString* title = [options objectForKey:@"title"];
NSString* sound = [options objectForKey:@"sound"]; NSString* sound = [options objectForKey:@"sound"];
...@@ -154,9 +192,9 @@ ...@@ -154,9 +192,9 @@
notification.applicationIconBadgeNumber = badge; notification.applicationIconBadgeNumber = badge;
if (!(msg == (NSString*)[NSNull null] || [msg isEqualToString:@""])) if (![self strIsNullOrEmpty:msg])
{ {
if (!(title == (NSString*)[NSNull null] || [title isEqualToString:@""])) if (![self strIsNullOrEmpty:title])
{ {
notification.alertBody = [NSString stringWithFormat:@"%@\n%@", title, msg]; notification.alertBody = [NSString stringWithFormat:@"%@\n%@", title, msg];
} }
...@@ -196,14 +234,14 @@ ...@@ -196,14 +234,14 @@
if (autoCancel && !isActive) if (autoCancel && !isActive)
{ {
[[UIApplication sharedApplication] cancelLocalNotification:notification]; [self cancelNotificationWithId:id];
} }
if (callbackFn && callbackFn.length > 0) if (![self strIsNullOrEmpty:callbackFn])
{ {
NSString* callback = [NSString stringWithFormat:@"setTimeout('%@(%@)',0)", callbackFn, id]; NSString* js = [NSString stringWithFormat:@"setTimeout('%@(%@)',0)", callbackFn, id];
[self writeJavascript:callback]; [self writeJavascript:js];
} }
} }
...@@ -215,4 +253,12 @@ ...@@ -215,4 +253,12 @@
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveLocalNotification:) name:CDVLocalNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveLocalNotification:) name:CDVLocalNotification object:nil];
} }
/**
* Hilfsmethode gibt an, ob er String NULL oder Empty ist.
*/
- (BOOL) strIsNullOrEmpty:(NSString*)str
{
return (str == (NSString*)[NSNull null] || [str isEqualToString:@""]) ? YES : NO;
}
@end @end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment