Commit 8865a7e4 by Sebastián Katzer

Such a commit I would never accept as a PR ;)

parent f39efbd5
...@@ -22,22 +22,17 @@ ...@@ -22,22 +22,17 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h> #import <Cordova/CDVPlugin.h>
@interface APPLocalNotification : CDVPlugin { @interface APPLocalNotification : CDVPlugin
} // Schedules a new local notification
// Schlüssel-Präfix für alle archivierten Meldungen
extern NSString *const kAPP_LOCALNOTIFICATION;
// Fügt einen neuen Eintrag hinzu
- (void) add:(CDVInvokedUrlCommand*)command; - (void) add:(CDVInvokedUrlCommand*)command;
// Entfernt die zur ID passende Meldung // Cancels a given local notification
- (void) cancel:(CDVInvokedUrlCommand*)command; - (void) cancel:(CDVInvokedUrlCommand*)command;
// Entfernt alle registrierten Einträge // Cancels all currently scheduled notifications
- (void) cancelAll:(CDVInvokedUrlCommand*)command; - (void) cancelAll:(CDVInvokedUrlCommand*)command;
// Checks wether a notification with an ID is scheduled // Checks wether a notification with an ID is scheduled
- (void) isScheduled:(CDVInvokedUrlCommand*)command; - (void) isScheduled:(CDVInvokedUrlCommand*)command;
// Retrieves a list with all currently pending notifications // Retrieves a list of ids from all currently pending notifications
- (void) getScheduledIds:(CDVInvokedUrlCommand*)command; - (void) getScheduledIds:(CDVInvokedUrlCommand*)command;
@end @end
...@@ -23,64 +23,68 @@ ...@@ -23,64 +23,68 @@
@interface APPLocalNotification (Private) @interface APPLocalNotification (Private)
// Archiviert die Meldungen, sodass sie später abgerufen werden kann // Schedules a new local notification and fies the coresponding event
- (void) archiveNotification:(UILocalNotification*)notification; - (void) scheduleNotificationWithProperties:(NSMutableDictionary*)properties;
// Entfernt den zur ID passenden Eintrag // Cancels the given local notification and fires the cancel event
- (void) cancelNotificationWithId:(NSString*)id fireEvent:(BOOL)fireEvent; - (void) cancelNotification:(UILocalNotification*)notification fireEvent:(BOOL)fireEvent;
// Nachschlagewerk für Zeitintervallangaben // Cancels all local notification with are older then
- (void) cancelAllNotificationsWhichAreOlderThen:(float)seconds;
// Retrurns a key-value dictionary for repeat intervals
- (NSMutableDictionary*) repeatDict; - (NSMutableDictionary*) repeatDict;
// Alle zusätzlichen Metadaten der Notification als Hash // Returns the userDict for a local notification
- (NSDictionary*) userDict:(NSMutableDictionary*)options; - (NSDictionary*) userDict:(NSMutableDictionary*)options;
// Erstellt die Notification und setzt deren Eigenschaften // Creates an notification object based on the given properties
- (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options; - (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options;
// Ruft die JS-Callbacks auf, nachdem eine Notification eingegangen ist // Calls the cancel or trigger event after a local notification was received
- (void) didReceiveLocalNotification:(NSNotification*)localNotification; - (void) didReceiveLocalNotification:(NSNotification*)localNotification;
// Ruft die JS-Callbacks auf, nachdem eine Notification eingegangen ist // Calls the cancel or trigger event after a local notification was received
- (void) didFinishLaunchingWithOptions:(NSNotification*)notification; - (void) didFinishLaunchingWithOptions:(NSNotification*)notification;
// Hilfsmethode gibt an, ob er String NULL oder Empty ist // Registers obervers for the following events after plugin was initialized.
- (BOOL) strIsNullOrEmpty:(NSString*)str; - (void) pluginInitialize;
// Checks wether a notification with an ID is scheduled or not. // Clears all single repeating notifications which are older then 5 days
- (BOOL) isNotificationScheduledWithId:(NSString*) id; - (void) onAppTerminate;
// Checks weather the given string is empty or not
- (BOOL) stringIsNullOrEmpty:(NSString*)str;
// Checks wether a notification with an ID is scheduled or not
- (BOOL) isNotificationScheduledWithId:(NSString*)id;
// Retrieves the local notification by its ID
- (UILocalNotification*) notificationWithId:(NSString*)id;
// Fires the given event // Fires the given event
- (void) fireEvent:(NSString*) event id:(NSString*) id json:(NSString*) json; - (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json;
@end @end
// Schlüssel-Präfix für alle archivierten Meldungen
NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
@implementation APPLocalNotification @implementation APPLocalNotification
/** /**
* Fügt eine neue Notification-Eintrag hinzu. * Schedules a new local notification.
* *
* @param {NSMutableDictionary} options * @param {NSMutableDictionary} properties
* Die Eigenschaften der Notification * The properties of the notification
*/ */
- (void) add:(CDVInvokedUrlCommand*)command - (void) add:(CDVInvokedUrlCommand*)command
{ {
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
NSArray* arguments = [command arguments]; NSArray* arguments = [command arguments];
NSMutableDictionary* options = [arguments objectAtIndex:0]; NSMutableDictionary* properties = [arguments objectAtIndex:0];
UILocalNotification* notification = [self notificationWithProperties:options];
NSDictionary* userInfo = notification.userInfo;
NSString* id = [userInfo objectForKey:@"id"];
NSString* json = [userInfo objectForKey:@"json"];
[self cancelNotificationWithId:id fireEvent:NO]; NSString* id = [properties objectForKey:@"id"];
[self archiveNotification:notification];
[self fireEvent:@"add" id:id json:json]; if ([self isNotificationScheduledWithId:id]) {
UILocalNotification* notification = [self notificationWithId:id];
[[UIApplication sharedApplication] [self cancelNotification:notification fireEvent:NO];
scheduleLocalNotification:notification]; }
[self scheduleNotificationWithProperties:properties];
}]; }];
} }
/** /**
* Entfernt die zur ID passende Meldung. * Cancels a given local notification.
* *
* @param {NSString} id Die ID der Notification * @param {NSString} id
* The ID of the local notification
*/ */
- (void) cancel:(CDVInvokedUrlCommand*)command - (void) cancel:(CDVInvokedUrlCommand*)command
{ {
...@@ -88,38 +92,40 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION"; ...@@ -88,38 +92,40 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
NSArray* arguments = [command arguments]; NSArray* arguments = [command arguments];
NSString* id = [arguments objectAtIndex:0]; NSString* id = [arguments objectAtIndex:0];
[self cancelNotificationWithId:id fireEvent:YES]; UILocalNotification* notification = [self notificationWithId:id];
if (notification) {
[self cancelNotification:notification fireEvent:YES];
}
}]; }];
} }
/** /**
* Entfernt alle registrierten Einträge. * Cancels all currently scheduled notifications.
*/ */
- (void) cancelAll:(CDVInvokedUrlCommand*)command - (void) cancelAll:(CDVInvokedUrlCommand*)command
{ {
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
NSDictionary* entries = [[NSUserDefaults standardUserDefaults] NSArray* notifications = [[UIApplication sharedApplication]
dictionaryRepresentation]; scheduledLocalNotifications];
for (NSString* key in [entries allKeys]) for (UILocalNotification* notification in notifications)
{
if ([key hasPrefix:kAPP_LOCALNOTIFICATION])
{ {
[self cancelNotificationWithId:key fireEvent:YES]; [self cancelNotification:notification fireEvent:YES];
}
} }
[[NSUserDefaults standardUserDefaults] synchronize]; [[UIApplication sharedApplication]
cancelAllLocalNotifications];
[[UIApplication sharedApplication] cancelAllLocalNotifications]; [[UIApplication sharedApplication]
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; setApplicationIconBadgeNumber:0];
}]; }];
} }
/** /**
* Checks wether a notification with an ID is scheduled. * Checks wether a notification with an ID is scheduled.
* *
* @param id * @param {NSString} id
* The ID of the notification * The ID of the notification
* @param callback * @param callback
* The callback function to be called with the result * The callback function to be called with the result
...@@ -171,118 +177,129 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION"; ...@@ -171,118 +177,129 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
} }
/** /**
* Entfernt den zur ID passenden Eintrag. * Schedules a new local notification and fies the coresponding event.
* *
* @param {NSString} id Die ID der Notification * @param {NSMutableDictionary} properties
* The properties of the notification
*/ */
- (void) cancelNotificationWithId:(NSString*)id fireEvent:(BOOL)fireEvent - (void) scheduleNotificationWithProperties:(NSMutableDictionary*)properties
{ {
if (![self strIsNullOrEmpty:id]) UILocalNotification* notification = [self notificationWithProperties:
{ properties];
NSString* key = ([id hasPrefix:kAPP_LOCALNOTIFICATION])
? id
: [kAPP_LOCALNOTIFICATION stringByAppendingString:id];
NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:key]; NSDictionary* userInfo = notification.userInfo;
NSString* id = [userInfo objectForKey:@"id"];
NSString* json = [userInfo objectForKey:@"json"];
if (data) [self fireEvent:@"add" id:id json:json];
{
UILocalNotification* notification = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key]; [[UIApplication sharedApplication]
[[UIApplication sharedApplication] cancelLocalNotification:notification]; scheduleLocalNotification:notification];
}
/**
* Cancels the given local notification
* and fires the cancel event.
*
* @param {NSString} id
* The ID of the local notification
*/
- (void) cancelNotification:(UILocalNotification*)notification
fireEvent:(BOOL)fireEvent
{
NSDictionary* userInfo = notification.userInfo;
NSString* id = [userInfo objectForKey:@"id"];
NSString* json = [userInfo objectForKey:@"json"];
[[UIApplication sharedApplication]
cancelLocalNotification:notification];
if (fireEvent) if (fireEvent)
{ {
NSString* json = [notification.userInfo objectForKey:@"json"];
[self fireEvent:@"cancel" id:id json:json]; [self fireEvent:@"cancel" id:id json:json];
} }
}
}
} }
/** /**
* Entfernt alle Meldungen, die älter als x Sekunden sind. * Cancels all local notification with are older then
* a specific amount of seconds
* *
* @param {float} seconds * @param {float} seconds
* The time interval in seconds
*/ */
- (void) cancelAllNotificationsWhichAreOlderThen:(float)seconds - (void) cancelAllNotificationsWhichAreOlderThen:(float)seconds
{ {
NSDictionary* entries = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
NSDate* now = [NSDate date]; NSDate* now = [NSDate date];
for (NSString* key in [entries allKeys]) NSArray* notifications = [[UIApplication sharedApplication]
{ scheduledLocalNotifications];
if ([key hasPrefix:kAPP_LOCALNOTIFICATION])
{
NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
if (data) for (UILocalNotification* notification in notifications)
{ {
UILocalNotification* notification = [NSKeyedUnarchiver unarchiveObjectWithData:data]; NSDate* fireDate = notification.fireDate;
NSTimeInterval fireDateDistance = [now timeIntervalSinceDate:
NSTimeInterval fireDateDistance = [now timeIntervalSinceDate:notification.fireDate]; fireDate];
NSString* id = [notification.userInfo objectForKey:@"id"];
if (notification.repeatInterval == NSEraCalendarUnit && fireDateDistance > seconds) { if (notification.repeatInterval == NSEraCalendarUnit
[self cancelNotificationWithId:id fireEvent:YES]; && fireDateDistance > seconds) {
} [self cancelNotification:notification fireEvent:YES];
}
} }
} }
} }
/** /**
* Archiviert die Meldungen, sodass sie später abgerufen werden kann. * Retrurns a key-value dictionary for repeat intervals.
* *
* @param {UILocalNotification} notification * @return {NSMutableDictionary}
*/
- (void) archiveNotification:(UILocalNotification*)notification
{
NSString* id = [notification.userInfo objectForKey:@"id"];
if (![self strIsNullOrEmpty:id])
{
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:notification];
NSString* key = [kAPP_LOCALNOTIFICATION stringByAppendingString:id];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
}
}
/**
* Nachschlagewerk für Zeitintervallangaben.
*/ */
- (NSMutableDictionary*) repeatDict - (NSMutableDictionary*) repeatDict
{ {
NSMutableDictionary* repeatDict = [[NSMutableDictionary alloc] init]; NSMutableDictionary* repeatDict = [[NSMutableDictionary alloc] init];
#ifdef NSCalendarUnitHour #ifdef NSCalendarUnitHour
[repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitSecond] forKey:@"secondly"]; [repeatDict setObject:
[repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitMinute] forKey:@"minutely"]; [NSNumber numberWithInt:NSCalendarUnitSecond] forKey:@"secondly"];
[repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitHour] forKey:@"hourly"]; [repeatDict setObject:
[repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitDay] forKey:@"daily"]; [NSNumber numberWithInt:NSCalendarUnitMinute] forKey:@"minutely"];
[repeatDict setObject:[NSNumber numberWithInt:NSWeekCalendarUnit] forKey:@"weekly"]; [repeatDict setObject:
[repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitMonth] forKey:@"monthly"]; [NSNumber numberWithInt:NSCalendarUnitHour] forKey:@"hourly"];
[repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitYear] forKey:@"yearly"]; [repeatDict setObject:
[NSNumber numberWithInt:NSCalendarUnitDay] forKey:@"daily"];
[repeatDict setObject:
[NSNumber numberWithInt:NSWeekCalendarUnit] forKey:@"weekly"];
[repeatDict setObject:
[NSNumber numberWithInt:NSCalendarUnitMonth] forKey:@"monthly"];
[repeatDict setObject:
[NSNumber numberWithInt:NSCalendarUnitYear] forKey:@"yearly"];
#else #else
[repeatDict setObject:[NSNumber numberWithInt:NSSecondCalendarUnit] forKey:@"secondly"]; [repeatDict setObject:
[repeatDict setObject:[NSNumber numberWithInt:NSMinuteCalendarUnit] forKey:@"minutely"]; [NSNumber numberWithInt:NSSecondCalendarUnit] forKey:@"secondly"];
[repeatDict setObject:[NSNumber numberWithInt:NSHourCalendarUnit] forKey:@"hourly"]; [repeatDict setObject:
[repeatDict setObject:[NSNumber numberWithInt:NSDayCalendarUnit] forKey:@"daily"]; [NSNumber numberWithInt:NSMinuteCalendarUnit] forKey:@"minutely"];
[repeatDict setObject:[NSNumber numberWithInt:NSWeekCalendarUnit] forKey:@"weekly"]; [repeatDict setObject:
[repeatDict setObject:[NSNumber numberWithInt:NSMonthCalendarUnit] forKey:@"monthly"]; [NSNumber numberWithInt:NSHourCalendarUnit] forKey:@"hourly"];
[repeatDict setObject:[NSNumber numberWithInt:NSYearCalendarUnit] forKey:@"yearly"]; [repeatDict setObject:
[NSNumber numberWithInt:NSDayCalendarUnit] forKey:@"daily"];
[repeatDict setObject:
[NSNumber numberWithInt:NSWeekCalendarUnit] forKey:@"weekly"];
[repeatDict setObject:
[NSNumber numberWithInt:NSMonthCalendarUnit] forKey:@"monthly"];
[repeatDict setObject:
[NSNumber numberWithInt:NSYearCalendarUnit] forKey:@"yearly"];
#endif #endif
[repeatDict setObject:[NSNumber numberWithInt:NSEraCalendarUnit] forKey:@""]; [repeatDict setObject:
[NSNumber numberWithInt:NSEraCalendarUnit] forKey:@""];
return repeatDict; return repeatDict;
} }
/** /**
* Alle zusätzlichen Metadaten der Notification als Hash. * Returns the userDict for a local notification.
*
* @param {NSMutableDictionary} options
* The properties for the local notification
* @return {NSDictionary}
*/ */
- (NSDictionary*) userDict:(NSMutableDictionary*)options - (NSDictionary*) userDict:(NSMutableDictionary*)options
{ {
...@@ -295,7 +312,11 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION"; ...@@ -295,7 +312,11 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
} }
/** /**
* Erstellt die Notification und setzt deren Eigenschaften. * Creates an notification object based on the given properties.
*
* @param {NSMutableDictionary} properties
* The properties for the local notification
* @return {UILocalNotification}
*/ */
- (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options - (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options
{ {
...@@ -310,16 +331,18 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION"; ...@@ -310,16 +331,18 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
notification.fireDate = [NSDate dateWithTimeIntervalSince1970:timestamp]; notification.fireDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
notification.timeZone = [NSTimeZone defaultTimeZone]; notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = [[[self repeatDict] objectForKey: repeat] intValue];
notification.userInfo = [self userDict:options]; notification.userInfo = [self userDict:options];
notification.applicationIconBadgeNumber = badge; notification.applicationIconBadgeNumber = badge;
if (![self strIsNullOrEmpty:msg]) notification.repeatInterval = [[[self repeatDict] objectForKey:repeat]
intValue];
if (![self stringIsNullOrEmpty:msg])
{ {
if (![self strIsNullOrEmpty:title]) if (![self stringIsNullOrEmpty:title])
{ {
notification.alertBody = [NSString stringWithFormat:@"%@\n%@", title, msg]; notification.alertBody = [NSString stringWithFormat:
@"%@\n%@", title, msg];
} }
else else
{ {
...@@ -342,58 +365,73 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION"; ...@@ -342,58 +365,73 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
} }
/** /**
* Ruft die JS-Callbacks auf, nachdem eine Notification eingegangen ist. * Calls the cancel or trigger event after a local notification was received.
* Cancels the local notification if autoCancel was set to true.
*/ */
- (void) didReceiveLocalNotification:(NSNotification*)localNotification - (void) didReceiveLocalNotification:(NSNotification*)localNotification
{ {
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
bool isActive = state == UIApplicationStateActive;
UILocalNotification* notification = [localNotification object]; UILocalNotification* notification = [localNotification object];
NSString* id = [notification.userInfo objectForKey:@"id"];
NSString* json = [notification.userInfo objectForKey:@"json"];
BOOL autoCancel = [[notification.userInfo objectForKey:@"autoCancel"] boolValue];
NSDate* now = [NSDate date]; NSDictionary* userInfo = notification.userInfo;
NSTimeInterval fireDateDistance = [now timeIntervalSinceDate:notification.fireDate]; NSString* id = [userInfo objectForKey:@"id"];
NSString* event = (fireDateDistance < 0.05) ? @"trigger" : @"click"; NSString* json = [userInfo objectForKey:@"json"];
BOOL autoCancel = [[userInfo objectForKey:@"autoCancel"] boolValue];
[self fireEvent:event id:id json:json]; NSDate* now = [NSDate date];
NSDate* fireDate = notification.fireDate;
NSTimeInterval fireDateDistance = [now timeIntervalSinceDate:fireDate];
NSString* event = (fireDateDistance < 1) ? @"trigger" : @"click";
if (autoCancel && !isActive) if (autoCancel && [event isEqualToString:@"click"])
{ {
[self cancelNotificationWithId:id fireEvent:YES]; [self cancelNotification:notification fireEvent:YES];
} }
[self fireEvent:event id:id json:json];
} }
/** /**
* Ruft die JS-Callbacks auf, nachdem eine Notification eingegangen ist. * Calls the cancel or trigger event after a local notification was received.
*/ */
- (void) didFinishLaunchingWithOptions:(NSNotification*)notification - (void) didFinishLaunchingWithOptions:(NSNotification*)notification
{ {
NSDictionary* launchOptions = [notification userInfo]; NSDictionary* launchOptions = [notification userInfo];
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
UILocalNotification* localNotification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) if (localNotification)
{ {
[self didReceiveLocalNotification:[NSNotification notificationWithName:CDVLocalNotification object:localNotification]]; [self didReceiveLocalNotification:
[NSNotification notificationWithName:CDVLocalNotification
object:localNotification]];
} }
} }
/** /**
* Registriert den Observer für LocalNotification Events. * Registers obervers for the following events after plugin was initialized.
* didReceiveLocalNotification:
* didFinishLaunchingWithOptions:
*/ */
- (void) pluginInitialize - (void) pluginInitialize
{ {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveLocalNotification:) NSNotificationCenter* notificationCenter = [NSNotificationCenter
name:CDVLocalNotification object:nil]; defaultCenter];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishLaunchingWithOptions:) [notificationCenter addObserver:self
name:UIApplicationDidFinishLaunchingNotification object:nil]; selector:@selector(didReceiveLocalNotification:)
name:CDVLocalNotification
object:nil];
[notificationCenter addObserver:self
selector:@selector(didFinishLaunchingWithOptions:)
name:CDVLocalNotification
object:nil];
} }
/** /**
* Löscht alle single-repeat Notifications, die älter als 5 Tage sind. * Clears all single repeating notifications which are older then 5 days
* before the app terminates.
*/ */
- (void) onAppTerminate - (void) onAppTerminate
{ {
...@@ -401,11 +439,24 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION"; ...@@ -401,11 +439,24 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
} }
/** /**
* Hilfsmethode gibt an, ob er String NULL oder Empty ist. * Checks weather the given string is empty or not.
*
* @param {NSString} str The string to be check
* @return {BOOL}
*/ */
- (BOOL) strIsNullOrEmpty:(NSString*)str - (BOOL) stringIsNullOrEmpty:(NSString*)str
{ {
return (str == (NSString*)[NSNull null] || [str isEqualToString:@""]) ? YES : NO; if (str == (NSString*)[NSNull null])
{
return YES;
}
if ([str isEqualToString:@""])
{
return YES;
}
return NO;
} }
/** /**
...@@ -415,7 +466,21 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION"; ...@@ -415,7 +466,21 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
* The ID of the notification * The ID of the notification
* @return BOOL * @return BOOL
*/ */
- (BOOL) isNotificationScheduledWithId:(NSString*) id - (BOOL) isNotificationScheduledWithId:(NSString*)id
{
UILocalNotification* notification = [self notificationWithId:id];
return notification != NULL;
}
/**
* Retrieves the local notification by its ID.
*
* @param {NSString} id
* The ID of the notification
* @return UILocalNotification*
*/
- (UILocalNotification*) notificationWithId:(NSString*)id
{ {
NSArray* notifications = [[UIApplication sharedApplication] NSArray* notifications = [[UIApplication sharedApplication]
scheduledLocalNotifications]; scheduledLocalNotifications];
...@@ -425,28 +490,38 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION"; ...@@ -425,28 +490,38 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
NSString* notId = [notification.userInfo objectForKey:@"id"]; NSString* notId = [notification.userInfo objectForKey:@"id"];
if ([notId isEqualToString:id]) { if ([notId isEqualToString:id]) {
return YES; return notification;
} }
} }
return NO; return NULL;
} }
/** /**
* Fires the given event. * Fires the given event.
* *
* @param {String} event The Name of the event * @param {NSString} event
* @param {String} id The ID of the notification * The Name of the event
* @param {String} json A custom (JSON) string * @param {NSString} id
* The ID of the notification
* @param {NSString} json
* A custom (JSON) string
*/ */
- (void) fireEvent:(NSString*) event id:(NSString*) id json:(NSString*) json - (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json
{ {
UIApplicationState state = [[UIApplication sharedApplication] applicationState]; UIApplicationState state = [[UIApplication sharedApplication]
applicationState];
bool isActive = state == UIApplicationStateActive; bool isActive = state == UIApplicationStateActive;
NSString* stateName = isActive ? @"foreground" : @"background"; NSString* stateName = isActive ? @"foreground" : @"background";
NSString* params = [NSString stringWithFormat:@"\"%@\",\"%@\",\\'%@\\'", id, stateName, json]; NSString* params = [NSString stringWithFormat:
NSString* js = [NSString stringWithFormat:@"setTimeout('plugin.notification.local.on%@(%@)',0)", event, params]; @"\"%@\",\"%@\",\\'%@\\'",
id, stateName, json];
NSString* js = [NSString stringWithFormat:
@"setTimeout('plugin.notification.local.on%@(%@)',0)",
event, params];
[self.commandDelegate evalJs:js]; [self.commandDelegate evalJs:js];
} }
......
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