|
|
@@ -20,12 +20,14 @@
|
|
|
*/
|
|
|
|
|
|
#import "APPLocalNotification.h"
|
|
|
+#import "APPLocalNotificationOptions.h"
|
|
|
+#import "UIApplication+APPLocalNotification.h"
|
|
|
+#import "UILocalNotification+APPLocalNotification.h"
|
|
|
+
|
|
|
#import <Availability.h>
|
|
|
|
|
|
@interface APPLocalNotification ()
|
|
|
|
|
|
-// Retrieves all scheduled notifications
|
|
|
-@property (readonly, getter=scheduledNotifications) NSArray* scheduledNotifications;
|
|
|
// Retrieves the application state
|
|
|
@property (readonly, getter=applicationState) NSString* applicationState;
|
|
|
// All events will be queued until deviceready has been fired
|
|
|
@@ -37,13 +39,13 @@
|
|
|
|
|
|
@implementation APPLocalNotification
|
|
|
|
|
|
-@synthesize deviceready, eventQueue, applicationState, scheduledNotifications;
|
|
|
+@synthesize deviceready, eventQueue;
|
|
|
|
|
|
#pragma mark -
|
|
|
-#pragma mark Plugin interface methods
|
|
|
+#pragma mark Interface
|
|
|
|
|
|
/**
|
|
|
- * Executes all queued events.
|
|
|
+ * Execute all queued events.
|
|
|
*/
|
|
|
- (void) deviceready:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
@@ -57,29 +59,24 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Schedules a new local notification.
|
|
|
+ * Schedule a new local notification.
|
|
|
*
|
|
|
- * @param {NSMutableDictionary} properties
|
|
|
- * The properties of the notification
|
|
|
+ * @param properties
|
|
|
+ * A dict of properties
|
|
|
*/
|
|
|
- (void) add:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- NSArray* arguments = [command arguments];
|
|
|
- NSMutableDictionary* properties = [arguments objectAtIndex:0];
|
|
|
-
|
|
|
- NSString* id = [properties objectForKey:@"id"];
|
|
|
+ NSDictionary* options = [[command arguments]
|
|
|
+ objectAtIndex:0];
|
|
|
|
|
|
- if ([self isNotificationScheduledWithId:id]) {
|
|
|
- UILocalNotification* notification = [self notificationWithId:id];
|
|
|
+ UILocalNotification* notification;
|
|
|
|
|
|
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC),
|
|
|
- dispatch_get_main_queue(), ^{
|
|
|
- [self cancelNotification:notification fireEvent:NO];
|
|
|
- });
|
|
|
- }
|
|
|
+ notification = [[UILocalNotification alloc]
|
|
|
+ initWithOptions:options];
|
|
|
|
|
|
- [self scheduleNotificationWithProperties:properties];
|
|
|
+ [self scheduleLocalNotification:notification];
|
|
|
+ [self fireEvent:@"add" localNotification:notification];
|
|
|
[self execCallback:command];
|
|
|
}];
|
|
|
}
|
|
|
@@ -87,21 +84,22 @@
|
|
|
/**
|
|
|
* Cancels a given local notification.
|
|
|
*
|
|
|
- * @param {NSString} id
|
|
|
+ * @param id
|
|
|
* The ID of the local notification
|
|
|
*/
|
|
|
- (void) cancel:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- NSArray* arguments = [command arguments];
|
|
|
- NSString* id = [arguments objectAtIndex:0];
|
|
|
+ NSString* id = [[command arguments]
|
|
|
+ objectAtIndex:0];
|
|
|
|
|
|
- UILocalNotification* notification = [self notificationWithId:id];
|
|
|
+ UILocalNotification* notification;
|
|
|
|
|
|
- if (notification) {
|
|
|
- [self cancelNotification:notification fireEvent:YES];
|
|
|
- }
|
|
|
+ notification = [[UIApplication sharedApplication]
|
|
|
+ scheduledLocalNotificationWithId:id];
|
|
|
|
|
|
+ [self cancelLocalNotification:notification];
|
|
|
+ [self fireEvent:@"cancel" localNotification:notification];
|
|
|
[self execCallback:command];
|
|
|
}];
|
|
|
}
|
|
|
@@ -112,37 +110,31 @@
|
|
|
- (void) cancelAll:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- NSArray* notifications = self.scheduledNotifications;
|
|
|
-
|
|
|
- for (UILocalNotification* notification in notifications) {
|
|
|
- [self cancelNotification:notification fireEvent:YES];
|
|
|
- }
|
|
|
-
|
|
|
- [[UIApplication sharedApplication]
|
|
|
- cancelAllLocalNotifications];
|
|
|
-
|
|
|
- [[UIApplication sharedApplication]
|
|
|
- setApplicationIconBadgeNumber:0];
|
|
|
-
|
|
|
+ [self cancelAllLocalNotifications];
|
|
|
+ [self fireEvent:@"cancelall"];
|
|
|
[self execCallback:command];
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Checks wether a notification with an ID is scheduled.
|
|
|
+ * If a notification by ID is scheduled.
|
|
|
*
|
|
|
- * @param {NSString} id
|
|
|
+ * @param id
|
|
|
* The ID of the notification
|
|
|
- * @param callback
|
|
|
- * The callback function to be called with the result
|
|
|
*/
|
|
|
- (void) isScheduled:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- NSArray* arguments = [command arguments];
|
|
|
- NSString* id = [arguments objectAtIndex:0];
|
|
|
- bool isScheduled = [self isNotificationScheduledWithId:id];
|
|
|
+ NSString* id = [[command arguments]
|
|
|
+ objectAtIndex:0];
|
|
|
+
|
|
|
CDVPluginResult* result;
|
|
|
+ UILocalNotification* notification;
|
|
|
+
|
|
|
+ notification = [[UIApplication sharedApplication]
|
|
|
+ scheduledLocalNotificationWithId:id];
|
|
|
+
|
|
|
+ bool isScheduled = notification != NULL;
|
|
|
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
messageAsBool:isScheduled];
|
|
|
@@ -153,25 +145,16 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Retrieves a list of ids from all currently pending notifications.
|
|
|
- *
|
|
|
- * @param callback
|
|
|
- * The callback function to be called with the result
|
|
|
+ * List of ids from all currently pending notifications.
|
|
|
*/
|
|
|
- (void) getScheduledIds:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- NSArray* notifications = self.scheduledNotifications;
|
|
|
-
|
|
|
- NSMutableArray* scheduledIds = [[NSMutableArray alloc] init];
|
|
|
CDVPluginResult* result;
|
|
|
+ NSArray* scheduledIds;
|
|
|
|
|
|
- for (UILocalNotification* notification in notifications)
|
|
|
- {
|
|
|
- NSString* id = [notification.userInfo objectForKey:@"id"];
|
|
|
-
|
|
|
- [scheduledIds addObject:id];
|
|
|
- }
|
|
|
+ scheduledIds = [[UIApplication sharedApplication]
|
|
|
+ scheduledLocalNotificationIds];
|
|
|
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
messageAsArray:scheduledIds];
|
|
|
@@ -184,18 +167,22 @@
|
|
|
/**
|
|
|
* Checks wether a notification with an ID was triggered.
|
|
|
*
|
|
|
- * @param {NSString} id
|
|
|
+ * @param id
|
|
|
* The ID of the notification
|
|
|
- * @param callback
|
|
|
- * The callback function to be called with the result
|
|
|
*/
|
|
|
- (void) isTriggered:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- NSArray* arguments = [command arguments];
|
|
|
- NSString* id = [arguments objectAtIndex:0];
|
|
|
- bool isTriggered = [self isNotificationTriggeredWithId:id];
|
|
|
+ NSString* id = [[command arguments]
|
|
|
+ objectAtIndex:0];
|
|
|
+
|
|
|
CDVPluginResult* result;
|
|
|
+ UILocalNotification* notification;
|
|
|
+
|
|
|
+ notification = [[UIApplication sharedApplication]
|
|
|
+ triggeredLocalNotificationWithId:id];
|
|
|
+
|
|
|
+ bool isTriggered = notification != NULL;
|
|
|
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
messageAsBool:isTriggered];
|
|
|
@@ -207,31 +194,18 @@
|
|
|
|
|
|
/**
|
|
|
* Retrieves a list of ids from all currently triggered notifications.
|
|
|
- *
|
|
|
- * @param callback
|
|
|
- * The callback function to be called with the result
|
|
|
*/
|
|
|
- (void) getTriggeredIds:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- NSArray* notifications = self.scheduledNotifications;
|
|
|
-
|
|
|
- NSMutableArray* scheduledIds = [[NSMutableArray alloc] init];
|
|
|
CDVPluginResult* result;
|
|
|
+ NSArray* triggeredIds;
|
|
|
|
|
|
- for (UILocalNotification* notification in notifications)
|
|
|
- {
|
|
|
- if (![self isNotificationTriggered:notification]) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- NSString* id = [notification.userInfo objectForKey:@"id"];
|
|
|
-
|
|
|
- [scheduledIds addObject:id];
|
|
|
- }
|
|
|
+ triggeredIds = [[UIApplication sharedApplication]
|
|
|
+ triggeredLocalNotificationIds];
|
|
|
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
- messageAsArray:scheduledIds];
|
|
|
+ messageAsArray:triggeredIds];
|
|
|
|
|
|
[self.commandDelegate sendPluginResult:result
|
|
|
callbackId:command.callbackId];
|
|
|
@@ -239,17 +213,17 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Informs if the app has the permission to show
|
|
|
+ * Inform if the app has the permission to show
|
|
|
* badges and local notifications.
|
|
|
- *
|
|
|
- * @param callback
|
|
|
- * The function to be exec as the callback
|
|
|
*/
|
|
|
- (void) hasPermission:(CDVInvokedUrlCommand *)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
CDVPluginResult* result;
|
|
|
- BOOL hasPermission = [self hasPermissionToSheduleNotifications];
|
|
|
+ BOOL hasPermission;
|
|
|
+
|
|
|
+ hasPermission = [[UIApplication sharedApplication]
|
|
|
+ hasPermissionToScheduleLocalNotifications];
|
|
|
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
messageAsBool:hasPermission];
|
|
|
@@ -261,174 +235,114 @@
|
|
|
|
|
|
/**
|
|
|
* Ask for permission to show badges.
|
|
|
- *
|
|
|
- * @param callback
|
|
|
- * The function to be exec as the callback
|
|
|
*/
|
|
|
-- (void) promptForPermission:(CDVInvokedUrlCommand *)command
|
|
|
+- (void) registerPermission:(CDVInvokedUrlCommand *)command
|
|
|
{
|
|
|
-#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
|
|
|
- UIUserNotificationType types;
|
|
|
- UIUserNotificationSettings *settings;
|
|
|
-
|
|
|
- types = UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound;
|
|
|
-
|
|
|
- settings = [UIUserNotificationSettings settingsForTypes:types
|
|
|
- categories:nil];
|
|
|
-
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
[[UIApplication sharedApplication]
|
|
|
- registerUserNotificationSettings:settings];
|
|
|
+ registerPermissionToScheduleLocalNotifications];
|
|
|
}];
|
|
|
-#endif
|
|
|
}
|
|
|
|
|
|
#pragma mark -
|
|
|
-#pragma mark Plugin core methods
|
|
|
+#pragma mark Core Logic
|
|
|
|
|
|
/**
|
|
|
- * Schedules a new local notification and fies the coresponding event.
|
|
|
- *
|
|
|
- * @param {NSMutableDictionary} properties
|
|
|
- * The properties of the notification
|
|
|
+ * Schedule the local notification.
|
|
|
*/
|
|
|
-- (void) scheduleNotificationWithProperties:(NSMutableDictionary*)properties
|
|
|
+- (void) scheduleLocalNotification:(UILocalNotification*)notification
|
|
|
{
|
|
|
- UILocalNotification* notification = [self notificationWithProperties:
|
|
|
- properties];
|
|
|
+ [self cancelForerunnerLocalNotification:notification];
|
|
|
|
|
|
- NSDictionary* userInfo = notification.userInfo;
|
|
|
- NSString* id = [userInfo objectForKey:@"id"];
|
|
|
- NSString* json = [userInfo objectForKey:@"json"];
|
|
|
+ NSString* state = self.applicationState;
|
|
|
|
|
|
- [self fireEvent:@"add" id:id json:json];
|
|
|
+ if ([state isEqualToString:@"background"]) {
|
|
|
+ [[UIApplication sharedApplication]
|
|
|
+ presentLocalNotificationNow:notification];
|
|
|
+ }
|
|
|
|
|
|
[[UIApplication sharedApplication]
|
|
|
scheduleLocalNotification:notification];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Cancels the given local notification
|
|
|
- * and fires the cancel event.
|
|
|
- *
|
|
|
- * @param {NSString} id
|
|
|
- * The ID of the local notification
|
|
|
+ * Cancel the local notification.
|
|
|
*/
|
|
|
-- (void) cancelNotification:(UILocalNotification*)notification
|
|
|
- fireEvent:(BOOL)fireEvent
|
|
|
+- (void) cancelLocalNotification:(UILocalNotification*)notification
|
|
|
{
|
|
|
- NSDictionary* userInfo = notification.userInfo;
|
|
|
- NSString* id = [userInfo objectForKey:@"id"];
|
|
|
- NSString* json = [userInfo objectForKey:@"json"];
|
|
|
-
|
|
|
- if (notification==nil) {
|
|
|
- NSLog(@"cancelNotification: Notification equals nil");
|
|
|
- }else{
|
|
|
- [[UIApplication sharedApplication]
|
|
|
- cancelLocalNotification:notification];
|
|
|
- }
|
|
|
+ if (!notification)
|
|
|
+ return;
|
|
|
|
|
|
- if (fireEvent) {
|
|
|
- [self fireEvent:@"cancel" id:id json:json];
|
|
|
- }
|
|
|
+ [[UIApplication sharedApplication]
|
|
|
+ cancelLocalNotification:notification];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Cancels all local notification with are older then
|
|
|
- * a specific amount of seconds
|
|
|
- *
|
|
|
- * @param {float} seconds
|
|
|
- * The time interval in seconds
|
|
|
+ * Cancel all currently scheduled notifications.
|
|
|
*/
|
|
|
-- (void) cancelAllNotificationsWhichAreOlderThen:(float)seconds
|
|
|
+- (void) cancelAllLocalNotifications
|
|
|
{
|
|
|
- NSDate* now = [NSDate date];
|
|
|
-
|
|
|
- NSArray* notifications = self.scheduledNotifications;
|
|
|
+ NSArray* notifications;
|
|
|
|
|
|
- for (UILocalNotification* notification in notifications)
|
|
|
- {
|
|
|
- NSDate* fireDate = notification.fireDate;
|
|
|
- NSTimeInterval fireDateDistance = [now timeIntervalSinceDate:
|
|
|
- fireDate];
|
|
|
+ notifications = [[UIApplication sharedApplication]
|
|
|
+ scheduledLocalNotifications];
|
|
|
|
|
|
- if (notification.repeatInterval == NSCalendarUnitEra
|
|
|
- && fireDateDistance > seconds) {
|
|
|
- [self cancelNotification:notification fireEvent:YES];
|
|
|
- }
|
|
|
+ for (UILocalNotification* notification in notifications) {
|
|
|
+ [self cancelLocalNotification:notification];
|
|
|
}
|
|
|
+
|
|
|
+ [[UIApplication sharedApplication]
|
|
|
+ cancelAllLocalNotifications];
|
|
|
+
|
|
|
+ [[UIApplication sharedApplication]
|
|
|
+ setApplicationIconBadgeNumber:0];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Creates an notification object based on the given properties.
|
|
|
- *
|
|
|
- * @param {NSMutableDictionary} properties
|
|
|
- * The properties for the local notification
|
|
|
- * @return {UILocalNotification}
|
|
|
+ * Cancel a maybe given forerunner with the same ID.
|
|
|
*/
|
|
|
-- (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options
|
|
|
+- (void) cancelForerunnerLocalNotification:(UILocalNotification*)notification
|
|
|
{
|
|
|
- UILocalNotification* notification = [[UILocalNotification alloc] init];
|
|
|
-
|
|
|
- double timestamp = [[options objectForKey:@"date"] doubleValue];
|
|
|
- NSString* msg = [options objectForKey:@"message"];
|
|
|
- NSString* title = [options objectForKey:@"title"];
|
|
|
- NSString* sound = [options objectForKey:@"sound"];
|
|
|
- NSString* repeat = [options objectForKey:@"repeat"];
|
|
|
- NSInteger badge = [[options objectForKey:@"badge"] intValue];
|
|
|
+ NSString* id = notification.options.id;
|
|
|
+ UILocalNotification* forerunner;
|
|
|
|
|
|
- notification.fireDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
|
|
|
- notification.timeZone = [NSTimeZone defaultTimeZone];
|
|
|
- notification.userInfo = [self userDict:options];
|
|
|
- notification.applicationIconBadgeNumber = badge;
|
|
|
-
|
|
|
- notification.repeatInterval = [[[self repeatDict] objectForKey:repeat]
|
|
|
- intValue];
|
|
|
-
|
|
|
- if (![self stringIsNullOrEmpty:msg])
|
|
|
- {
|
|
|
- if (![self stringIsNullOrEmpty:title]) {
|
|
|
- notification.alertBody = [NSString stringWithFormat:
|
|
|
- @"%@\n%@", title, msg];
|
|
|
- } else {
|
|
|
- notification.alertBody = msg;
|
|
|
- }
|
|
|
- }
|
|
|
+ forerunner = [[UIApplication sharedApplication]
|
|
|
+ scheduledLocalNotificationWithId:id];
|
|
|
|
|
|
- if (sound != (NSString*)[NSNull null])
|
|
|
- {
|
|
|
- if ([sound isEqualToString:@""]) {
|
|
|
- notification.soundName = UILocalNotificationDefaultSoundName;
|
|
|
- } else {
|
|
|
- notification.soundName = sound;
|
|
|
- }
|
|
|
- }
|
|
|
+ if (!forerunner)
|
|
|
+ return;
|
|
|
|
|
|
- return notification;
|
|
|
+ [self cancelLocalNotification:forerunner];
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
- * If the app has the permission to show badges.
|
|
|
+ * Cancels all local notification with are older then
|
|
|
+ * a specific amount of seconds
|
|
|
+ *
|
|
|
+ * @param {float} seconds
|
|
|
+ * The time interval in seconds
|
|
|
*/
|
|
|
-- (BOOL) hasPermissionToSheduleNotifications
|
|
|
+- (void) cancelAllNotificationsWhichAreOlderThen:(float)seconds
|
|
|
{
|
|
|
-#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
|
|
|
- UIUserNotificationType types;
|
|
|
- UIUserNotificationSettings *settings;
|
|
|
-
|
|
|
- settings = [[UIApplication sharedApplication]
|
|
|
- currentUserNotificationSettings];
|
|
|
+ NSArray* notifications;
|
|
|
|
|
|
- types = UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound;
|
|
|
+ notifications = [[UIApplication sharedApplication]
|
|
|
+ scheduledLocalNotifications];
|
|
|
|
|
|
- return (settings.types & types);
|
|
|
-#else
|
|
|
- return YES;
|
|
|
-#endif
|
|
|
+ for (UILocalNotification* notification in notifications)
|
|
|
+ {
|
|
|
+ if (notification && notification.repeatInterval == NSCalendarUnitEra
|
|
|
+ && notification.timeIntervalSinceFireDate > seconds)
|
|
|
+ {
|
|
|
+ [self cancelLocalNotification:notification];
|
|
|
+ [self fireEvent:@"cancel" localNotification:notification];
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
#pragma mark -
|
|
|
-#pragma mark Plugin delegate and life cycle methods
|
|
|
+#pragma mark Delegates
|
|
|
|
|
|
/**
|
|
|
* Calls the cancel or trigger event after a local notification was received.
|
|
|
@@ -436,38 +350,36 @@
|
|
|
*/
|
|
|
- (void) didReceiveLocalNotification:(NSNotification*)localNotification
|
|
|
{
|
|
|
+ UIApplication* app = [UIApplication sharedApplication];
|
|
|
UILocalNotification* notification = [localNotification object];
|
|
|
|
|
|
- NSDictionary* userInfo = notification.userInfo;
|
|
|
- NSString* id = [userInfo objectForKey:@"id"];
|
|
|
- NSString* json = [userInfo objectForKey:@"json"];
|
|
|
- BOOL autoCancel = [[userInfo objectForKey:@"autoCancel"] boolValue];
|
|
|
+ BOOL autoCancel = notification.options.autoCancel;
|
|
|
+ NSTimeInterval timeInterval = notification.timeIntervalSinceFireDate;
|
|
|
|
|
|
- NSDate* now = [NSDate date];
|
|
|
- NSDate* fireDate = notification.fireDate;
|
|
|
- NSTimeInterval fireDateDistance = [now timeIntervalSinceDate:fireDate];
|
|
|
- NSString* event = (fireDateDistance < 1) ? @"trigger" : @"click";
|
|
|
+ NSString* event = (timeInterval <= 1 && deviceready) ? @"trigger" : @"click";
|
|
|
|
|
|
- if ([[self applicationState] isEqualToString:@"foreground"]) {
|
|
|
- event = @"trigger";
|
|
|
- }
|
|
|
+ app.applicationIconBadgeNumber -= 1;
|
|
|
+
|
|
|
+ [self fireEvent:event localNotification:notification];
|
|
|
|
|
|
if (autoCancel && [event isEqualToString:@"click"]) {
|
|
|
- [self cancelNotification:notification fireEvent:YES];
|
|
|
+ [self cancelLocalNotification:notification];
|
|
|
+ [self fireEvent:@"cancel" localNotification:notification];
|
|
|
}
|
|
|
-
|
|
|
- [self fireEvent:event id:id json:json];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Calls the cancel or trigger event after a local notification was received.
|
|
|
+ * Called when app has started
|
|
|
+ * (by clicking on a local notification).
|
|
|
*/
|
|
|
- (void) didFinishLaunchingWithOptions:(NSNotification*)notification
|
|
|
{
|
|
|
NSDictionary* launchOptions = [notification userInfo];
|
|
|
|
|
|
- UILocalNotification* localNotification = [launchOptions objectForKey:
|
|
|
- UIApplicationLaunchOptionsLocalNotificationKey];
|
|
|
+ UILocalNotification* localNotification;
|
|
|
+
|
|
|
+ localNotification = [launchOptions objectForKey:
|
|
|
+ UIApplicationLaunchOptionsLocalNotificationKey];
|
|
|
|
|
|
if (localNotification) {
|
|
|
[self didReceiveLocalNotification:
|
|
|
@@ -476,6 +388,9 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+#pragma mark -
|
|
|
+#pragma mark Life Cycle
|
|
|
+
|
|
|
/**
|
|
|
* Registers obervers for the following events after plugin was initialized.
|
|
|
* didReceiveLocalNotification:
|
|
|
@@ -483,8 +398,10 @@
|
|
|
*/
|
|
|
- (void) pluginInitialize
|
|
|
{
|
|
|
- NSNotificationCenter* notificationCenter = [NSNotificationCenter
|
|
|
- defaultCenter];
|
|
|
+ NSNotificationCenter* notificationCenter;
|
|
|
+
|
|
|
+ notificationCenter = [NSNotificationCenter
|
|
|
+ defaultCenter];
|
|
|
|
|
|
eventQueue = [[NSMutableArray alloc] init];
|
|
|
|
|
|
@@ -509,153 +426,12 @@
|
|
|
}
|
|
|
|
|
|
#pragma mark -
|
|
|
-#pragma mark Plugin helper methods
|
|
|
-
|
|
|
-/**
|
|
|
- * Retrurns a key-value dictionary for repeat intervals.
|
|
|
- *
|
|
|
- * @return {NSMutableDictionary}
|
|
|
- */
|
|
|
-- (NSMutableDictionary*) repeatDict
|
|
|
-{
|
|
|
- NSMutableDictionary* repeatDict = [[NSMutableDictionary alloc] init];
|
|
|
-
|
|
|
- [repeatDict setObject:
|
|
|
- [NSNumber numberWithInt:NSCalendarUnitSecond] forKey:@"secondly"];
|
|
|
- [repeatDict setObject:
|
|
|
- [NSNumber numberWithInt:NSCalendarUnitMinute] forKey:@"minutely"];
|
|
|
- [repeatDict setObject:
|
|
|
- [NSNumber numberWithInt:NSCalendarUnitHour] forKey:@"hourly"];
|
|
|
- [repeatDict setObject:
|
|
|
- [NSNumber numberWithInt:NSCalendarUnitDay] forKey:@"daily"];
|
|
|
- [repeatDict setObject:
|
|
|
- [NSNumber numberWithInt:NSCalendarUnitWeekOfYear] forKey:@"weekly"];
|
|
|
- [repeatDict setObject:
|
|
|
- [NSNumber numberWithInt:NSCalendarUnitMonth] forKey:@"monthly"];
|
|
|
- [repeatDict setObject:
|
|
|
- [NSNumber numberWithInt:NSCalendarUnitYear] forKey:@"yearly"];
|
|
|
- [repeatDict setObject:
|
|
|
- [NSNumber numberWithInt:NSCalendarUnitEra] forKey:@""];
|
|
|
-
|
|
|
- return repeatDict;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Returns the userDict for a local notification.
|
|
|
- *
|
|
|
- * @param {NSMutableDictionary} options
|
|
|
- * The properties for the local notification
|
|
|
- * @return {NSDictionary}
|
|
|
- */
|
|
|
-- (NSDictionary*) userDict:(NSMutableDictionary*)options
|
|
|
-{
|
|
|
- NSString* id = [options objectForKey:@"id"];
|
|
|
- NSString* ac = [options objectForKey:@"autoCancel"];
|
|
|
- NSString* js = [options objectForKey:@"json"];
|
|
|
-
|
|
|
- return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
- id, @"id", ac, @"autoCancel", js, @"json", nil];
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Checks weather the given string is empty or not.
|
|
|
- *
|
|
|
- * @param {NSString} str The string to be check
|
|
|
- * @return {BOOL}
|
|
|
- */
|
|
|
-- (BOOL) stringIsNullOrEmpty:(NSString*)str
|
|
|
-{
|
|
|
- if (str == (NSString*)[NSNull null]) {
|
|
|
- return YES;
|
|
|
- }
|
|
|
-
|
|
|
- if ([str isEqualToString:@""]) {
|
|
|
- return YES;
|
|
|
- }
|
|
|
-
|
|
|
- return NO;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Checks wether a notification with an ID is scheduled or not.
|
|
|
- *
|
|
|
- * @param id
|
|
|
- * The ID of the notification
|
|
|
- * @return BOOL
|
|
|
- */
|
|
|
-- (BOOL) isNotificationScheduledWithId:(NSString*)id
|
|
|
-{
|
|
|
- UILocalNotification* notification = [self notificationWithId:id];
|
|
|
-
|
|
|
- return notification != NULL;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Checks wether a notification with an ID was triggered or not.
|
|
|
- *
|
|
|
- * @param id
|
|
|
- * The ID of the notification
|
|
|
- * @return BOOL
|
|
|
- */
|
|
|
-- (BOOL) isNotificationTriggeredWithId:(NSString*)id
|
|
|
-{
|
|
|
- UILocalNotification* notification = [self notificationWithId:id];
|
|
|
-
|
|
|
- if (notification == NULL) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
-
|
|
|
- return [self isNotificationTriggered:notification];
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Checks wether a notification was triggered or not.
|
|
|
- *
|
|
|
- * @param notification
|
|
|
- * The notification
|
|
|
- * @return BOOL
|
|
|
- */
|
|
|
-- (BOOL) isNotificationTriggered:(UILocalNotification*)notification
|
|
|
-{
|
|
|
- NSDate* now = [NSDate date];
|
|
|
- NSDate* fireDate = notification.fireDate;
|
|
|
-
|
|
|
- bool isLaterThanOrEqualTo = !([now compare:fireDate] == NSOrderedAscending);
|
|
|
-
|
|
|
- return isLaterThanOrEqualTo;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Retrieves the local notification by its ID.
|
|
|
- *
|
|
|
- * @param {NSString} id
|
|
|
- * The ID of the notification
|
|
|
- * @return UILocalNotification*
|
|
|
- */
|
|
|
-- (UILocalNotification*) notificationWithId:(NSString*)id
|
|
|
-{
|
|
|
- NSArray* notifications = self.scheduledNotifications;
|
|
|
-
|
|
|
- for (UILocalNotification* notification in notifications)
|
|
|
- {
|
|
|
- NSString* notId = NULL;
|
|
|
- if ([[notification.userInfo objectForKey:@"id"] isKindOfClass:[NSString class]] ) {
|
|
|
- notId = [notification.userInfo objectForKey:@"id"];
|
|
|
- } else {
|
|
|
- notId = [[notification.userInfo objectForKey:@"id"] stringValue];
|
|
|
- }
|
|
|
- if ([notId isEqualToString:id]) {
|
|
|
- return notification;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return NULL;
|
|
|
-}
|
|
|
+#pragma mark Helper
|
|
|
|
|
|
/**
|
|
|
* Retrieves the application state
|
|
|
*
|
|
|
- * @return {NSString}
|
|
|
+ * @return
|
|
|
* Either "background" or "foreground"
|
|
|
*/
|
|
|
- (NSString*) applicationState
|
|
|
@@ -669,35 +445,8 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Retrieves all scheduled notifications.
|
|
|
- *
|
|
|
- * @return {NSArray}
|
|
|
- * A list of all scheduled local notifications
|
|
|
- */
|
|
|
-- (NSArray*) scheduledNotifications
|
|
|
-{
|
|
|
- NSMutableArray* notificationsWithoutNIL = [[NSMutableArray alloc]
|
|
|
- init];
|
|
|
-
|
|
|
- NSArray* notifications = [[UIApplication sharedApplication]
|
|
|
- scheduledLocalNotifications];
|
|
|
-
|
|
|
- for (UILocalNotification* notification in notifications)
|
|
|
- {
|
|
|
- if (notification) {
|
|
|
- [notificationsWithoutNIL addObject:notification];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return notificationsWithoutNIL;
|
|
|
-}
|
|
|
-
|
|
|
-#pragma mark -
|
|
|
-#pragma mark Plugin callback methods
|
|
|
-
|
|
|
-/**
|
|
|
- * Simply invokes the callback without any parameter.
|
|
|
- */
|
|
|
+ * Simply invokes the callback without any parameter.
|
|
|
+ */
|
|
|
- (void) execCallback:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
CDVPluginResult *result = [CDVPluginResult
|
|
|
@@ -708,26 +457,35 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Fires the given event.
|
|
|
- *
|
|
|
- * @param {NSString} event
|
|
|
- * The Name of the event
|
|
|
- * @param {NSString} id
|
|
|
- * The ID of the notification
|
|
|
- * @param {NSString} json
|
|
|
- * A custom (JSON) string
|
|
|
+ * Fire general event.
|
|
|
*/
|
|
|
-- (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json
|
|
|
+- (void) fireEvent:(NSString*)event
|
|
|
{
|
|
|
- NSString* appState = self.applicationState;
|
|
|
+ [self fireEvent:event localNotification:NULL];
|
|
|
+}
|
|
|
|
|
|
+/**
|
|
|
+ * Fire event for local notification.
|
|
|
+ */
|
|
|
+- (void) fireEvent:(NSString*)event localNotification:(UILocalNotification*)notification
|
|
|
+{
|
|
|
+ NSString* js;
|
|
|
NSString* params = [NSString stringWithFormat:
|
|
|
- @"\"%@\",\"%@\",\\'%@\\'",
|
|
|
- id, appState, json];
|
|
|
+ @"\"%@\"", self.applicationState];
|
|
|
+
|
|
|
+ if (notification) {
|
|
|
+ NSString* id = notification.options.id;
|
|
|
+ NSString* json = notification.options.json;
|
|
|
+ NSString* args = [notification.options encodeToJSON];
|
|
|
+
|
|
|
+ params = [NSString stringWithFormat:
|
|
|
+ @"\"%@\",\"%@\",\\'%@\\',JSON.parse(\\'%@\\')",
|
|
|
+ id, self.applicationState, json, args];
|
|
|
+ }
|
|
|
|
|
|
- NSString* js = [NSString stringWithFormat:
|
|
|
- @"setTimeout('plugin.notification.local.on%@(%@)',0)",
|
|
|
- event, params];
|
|
|
+ js = [NSString stringWithFormat:
|
|
|
+ @"setTimeout('plugin.notification.local.on%@(%@)',0)",
|
|
|
+ event, params];
|
|
|
|
|
|
if (deviceready) {
|
|
|
[self.commandDelegate evalJs:js];
|