|
|
@@ -23,20 +23,28 @@
|
|
|
|
|
|
#import "APPLocalNotification.h"
|
|
|
#import "APPLocalNotificationOptions.h"
|
|
|
-#import "UIApplication+APPLocalNotification.h"
|
|
|
-#import "UILocalNotification+APPLocalNotification.h"
|
|
|
+#import "UNUserNotificationCenter+APPLocalNotification.h"
|
|
|
+#import "UNNotificationRequest+APPLocalNotification.h"
|
|
|
+#import "UNMutableNotificationContent+APPLocalNotification.h"
|
|
|
|
|
|
- @import UserNotifications;
|
|
|
+#import "APPLocalNotificationOptions.ios9.h"
|
|
|
+#import "UIApplication+APPLocalNotification.ios9.h"
|
|
|
+#import "UILocalNotification+APPLocalNotification.ios9.h"
|
|
|
+
|
|
|
+#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
|
|
|
|
|
|
@interface APPLocalNotification ()
|
|
|
|
|
|
-// Retrieves the application state
|
|
|
-@property (readonly, getter=applicationState) NSString* applicationState;
|
|
|
+// Property reader for [self app]
|
|
|
+@property (readonly, getter=app) UIApplication* app;
|
|
|
+// Property reader for [self center]
|
|
|
+@property (readonly, getter=center) UNUserNotificationCenter* center;
|
|
|
// All events will be queued until deviceready has been fired
|
|
|
@property (readwrite, assign) BOOL deviceready;
|
|
|
// Event queue
|
|
|
@property (readonly, nonatomic, retain) NSMutableArray* eventQueue;
|
|
|
-// Needed when calling `registerPermission`
|
|
|
+
|
|
|
+// IOS9: TODO remove later
|
|
|
@property (nonatomic, retain) CDVInvokedUrlCommand* command;
|
|
|
|
|
|
@end
|
|
|
@@ -73,24 +81,34 @@
|
|
|
NSArray* notifications = command.arguments;
|
|
|
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- for (NSDictionary* options in notifications) {
|
|
|
- UILocalNotification* notification;
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ for (NSDictionary* options in notifications) {
|
|
|
+ UNMutableNotificationContent* notification;
|
|
|
+
|
|
|
+ notification = [[UNMutableNotificationContent alloc]
|
|
|
+ initWithOptions:options];
|
|
|
|
|
|
- notification = [[UILocalNotification alloc]
|
|
|
- initWithOptions:options];
|
|
|
- notification.timeZone = [NSTimeZone defaultTimeZone];
|
|
|
- notification.repeatInterval = 0;
|
|
|
+ [self scheduleNotification:notification];
|
|
|
+ //[self fireEvent:@"add" notification:notification];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (NSDictionary* options in notifications) {
|
|
|
+ UILocalNotification* notification;
|
|
|
|
|
|
- [self scheduleLocalNotification:[notification copy]];
|
|
|
- [self fireEvent:@"schedule" notification:notification];
|
|
|
+ notification = [[UILocalNotification alloc]
|
|
|
+ initWithOptions:options];
|
|
|
|
|
|
- if (notifications.count > 1) {
|
|
|
- [NSThread sleepForTimeInterval:0.01];
|
|
|
+ [self scheduleLocalNotification:[notification copy]];
|
|
|
+ [self fireEvent:@"schedule" localnotification:notification];
|
|
|
+
|
|
|
+ if (notifications.count > 1) {
|
|
|
+ [NSThread sleepForTimeInterval:0.01];
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[self execCallback:command];
|
|
|
- }];
|
|
|
+ }];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -104,22 +122,43 @@
|
|
|
NSArray* notifications = command.arguments;
|
|
|
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- for (NSDictionary* options in notifications) {
|
|
|
- NSNumber* id = [options objectForKey:@"id"];
|
|
|
- UILocalNotification* notification;
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ for (NSDictionary* options in notifications) {
|
|
|
+ NSNumber* id = [options objectForKey:@"id"];
|
|
|
+ UNNotificationRequest* notification;
|
|
|
+
|
|
|
+ notification = [self.center getNotificationWithId:id];
|
|
|
+
|
|
|
+ if (!notification)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ // [self updateNotification:[notification copy]
|
|
|
+ // withOptions:options];
|
|
|
+ //
|
|
|
+ // [self fireEvent:@"update" notification:notification];
|
|
|
+ //
|
|
|
+ // if (notifications.count > 1) {
|
|
|
+ // [NSThread sleepForTimeInterval:0.01];
|
|
|
+ // }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (NSDictionary* options in notifications) {
|
|
|
+ NSNumber* id = [options objectForKey:@"id"];
|
|
|
+ UILocalNotification* notification;
|
|
|
|
|
|
- notification = [self.app localNotificationWithId:id];
|
|
|
+ notification = [self.app localNotificationWithId:id];
|
|
|
|
|
|
- if (!notification)
|
|
|
- continue;
|
|
|
+ if (!notification)
|
|
|
+ continue;
|
|
|
|
|
|
- [self updateLocalNotification:[notification copy]
|
|
|
- withOptions:options];
|
|
|
+ [self updateLocalNotification:[notification copy]
|
|
|
+ withOptions:options];
|
|
|
|
|
|
- [self fireEvent:@"update" notification:notification];
|
|
|
+ [self fireEvent:@"update" localnotification:notification];
|
|
|
|
|
|
- if (notifications.count > 1) {
|
|
|
- [NSThread sleepForTimeInterval:0.01];
|
|
|
+ if (notifications.count > 1) {
|
|
|
+ [NSThread sleepForTimeInterval:0.01];
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -136,16 +175,30 @@
|
|
|
- (void) cancel:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- for (NSNumber* id in command.arguments) {
|
|
|
- UILocalNotification* notification;
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ for (NSNumber* id in command.arguments) {
|
|
|
+ UNNotificationRequest* notification;
|
|
|
|
|
|
- notification = [self.app localNotificationWithId:id];
|
|
|
+ notification = [self.center getNotificationWithId:id];
|
|
|
|
|
|
- if (!notification)
|
|
|
- continue;
|
|
|
+ if (!notification)
|
|
|
+ continue;
|
|
|
|
|
|
- [self.app cancelLocalNotification:notification];
|
|
|
- [self fireEvent:@"cancel" notification:notification];
|
|
|
+ [self.center cancelNotification:notification];
|
|
|
+ [self fireEvent:@"cancel" notification:notification];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (NSNumber* id in command.arguments) {
|
|
|
+ UILocalNotification* notification;
|
|
|
+
|
|
|
+ notification = [self.app localNotificationWithId:id];
|
|
|
+
|
|
|
+ if (!notification)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ [self.app cancelLocalNotification:notification];
|
|
|
+ [self fireEvent:@"cancel" localnotification:notification];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
[self execCallback:command];
|
|
|
@@ -158,7 +211,7 @@
|
|
|
- (void) cancelAll:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- [self cancelAllLocalNotifications];
|
|
|
+ [self cancelAllNotifications];
|
|
|
[self fireEvent:@"cancelall"];
|
|
|
[self execCallback:command];
|
|
|
}];
|
|
|
@@ -173,16 +226,30 @@
|
|
|
- (void) clear:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- for (NSNumber* id in command.arguments) {
|
|
|
- UILocalNotification* notification;
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ for (NSNumber* id in command.arguments) {
|
|
|
+ UNNotificationRequest* notification;
|
|
|
+
|
|
|
+ notification = [self.center getNotificationWithId:id];
|
|
|
+
|
|
|
+ if (!notification)
|
|
|
+ continue;
|
|
|
|
|
|
- notification = [self.app localNotificationWithId:id];
|
|
|
+ [self.center clearNotification:notification];
|
|
|
+ [self fireEvent:@"clear" notification:notification];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (NSNumber* id in command.arguments) {
|
|
|
+ UILocalNotification* notification;
|
|
|
+
|
|
|
+ notification = [self.app localNotificationWithId:id];
|
|
|
|
|
|
- if (!notification)
|
|
|
- continue;
|
|
|
+ if (!notification)
|
|
|
+ continue;
|
|
|
|
|
|
- [self.app clearLocalNotification:notification];
|
|
|
- [self fireEvent:@"clear" notification:notification];
|
|
|
+ [self.app clearLocalNotification:notification];
|
|
|
+ [self fireEvent:@"clear" localnotification:notification];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
[self execCallback:command];
|
|
|
@@ -195,7 +262,7 @@
|
|
|
- (void) clearAll:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- [self clearAllLocalNotifications];
|
|
|
+ [self clearAllNotifications];
|
|
|
[self fireEvent:@"clearall"];
|
|
|
[self execCallback:command];
|
|
|
}];
|
|
|
@@ -241,20 +308,23 @@
|
|
|
* The notification life cycle type
|
|
|
*/
|
|
|
- (void) isPresent:(CDVInvokedUrlCommand*)command
|
|
|
- type:(APPLocalNotificationType)type;
|
|
|
+ type:(APPNotificationType)type;
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
NSNumber* id = [command argumentAtIndex:0];
|
|
|
BOOL exist;
|
|
|
|
|
|
- CDVPluginResult* result;
|
|
|
-
|
|
|
- if (type == NotifcationTypeAll) {
|
|
|
- exist = [self.app localNotificationExist:id];
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ exist = [self.center notificationExist:id type:type];
|
|
|
} else {
|
|
|
- exist = [self.app localNotificationExist:id type:type];
|
|
|
+ if (type == NotifcationTypeAll) {
|
|
|
+ exist = [self.app localNotificationExist:id];
|
|
|
+ } else {
|
|
|
+ exist = [self.app localNotificationExist:id type:type];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ CDVPluginResult* result;
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
messageAsBool:exist];
|
|
|
|
|
|
@@ -296,18 +366,22 @@
|
|
|
* The IDs of the notifications
|
|
|
*/
|
|
|
- (void) getIds:(CDVInvokedUrlCommand*)command
|
|
|
- byType:(APPLocalNotificationType)type;
|
|
|
+ byType:(APPNotificationType)type;
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- CDVPluginResult* result;
|
|
|
NSArray* ids;
|
|
|
|
|
|
- if (type == NotifcationTypeAll) {
|
|
|
- ids = [self.app localNotificationIds];
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ ids = [self.center getNotificationIdsByType:type];
|
|
|
} else {
|
|
|
- ids = [self.app localNotificationIdsByType:type];
|
|
|
+ if (type == NotifcationTypeAll) {
|
|
|
+ ids = [self.app localNotificationIds];
|
|
|
+ } else {
|
|
|
+ ids = [self.app localNotificationIdsByType:type];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ CDVPluginResult* result;
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
messageAsArray:ids];
|
|
|
|
|
|
@@ -380,21 +454,26 @@
|
|
|
* The ID of the notification
|
|
|
*/
|
|
|
- (void) getOption:(CDVInvokedUrlCommand*)command
|
|
|
- byType:(APPLocalNotificationType)type;
|
|
|
+ byType:(APPNotificationType)type;
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
NSArray* ids = command.arguments;
|
|
|
NSArray* notifications;
|
|
|
- CDVPluginResult* result;
|
|
|
|
|
|
- if (type == NotifcationTypeAll) {
|
|
|
- notifications = [self.app localNotificationOptionsById:ids];
|
|
|
- }
|
|
|
- else {
|
|
|
- notifications = [self.app localNotificationOptionsByType:type
|
|
|
- andId:ids];
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ notifications = [self.center getNotificationOptionsByType:type
|
|
|
+ andId:ids];
|
|
|
+ } else {
|
|
|
+ if (type == NotifcationTypeAll) {
|
|
|
+ notifications = [self.app localNotificationOptionsById:ids];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ notifications = [self.app localNotificationOptionsByType:type
|
|
|
+ andId:ids];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ CDVPluginResult* result;
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
messageAsDictionary:[notifications firstObject]];
|
|
|
|
|
|
@@ -412,27 +491,43 @@
|
|
|
* The IDs of the notifications
|
|
|
*/
|
|
|
- (void) getOptions:(CDVInvokedUrlCommand*)command
|
|
|
- byType:(APPLocalNotificationType)type;
|
|
|
+ byType:(APPNotificationType)type;
|
|
|
{
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
NSArray* ids = command.arguments;
|
|
|
NSArray* notifications;
|
|
|
- CDVPluginResult* result;
|
|
|
|
|
|
- if (type == NotifcationTypeAll && ids.count == 0) {
|
|
|
- notifications = [self.app localNotificationOptions];
|
|
|
- }
|
|
|
- else if (type == NotifcationTypeAll) {
|
|
|
- notifications = [self.app localNotificationOptionsById:ids];
|
|
|
- }
|
|
|
- else if (ids.count == 0) {
|
|
|
- notifications = [self.app localNotificationOptionsByType:type];
|
|
|
- }
|
|
|
- else {
|
|
|
- notifications = [self.app localNotificationOptionsByType:type
|
|
|
- andId:ids];
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ if (type == NotifcationTypeAll && ids.count == 0) {
|
|
|
+ notifications = [self.center getNotificationOptions];
|
|
|
+ }
|
|
|
+ else if (type == NotifcationTypeAll) {
|
|
|
+ notifications = [self.center getNotificationOptionsById:ids];
|
|
|
+ }
|
|
|
+ else if (ids.count == 0) {
|
|
|
+ notifications = [self.center getNotificationOptionsByType:type];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ notifications = [self.center getNotificationOptionsByType:type
|
|
|
+ andId:ids];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (type == NotifcationTypeAll && ids.count == 0) {
|
|
|
+ notifications = [self.app localNotificationOptions];
|
|
|
+ }
|
|
|
+ else if (type == NotifcationTypeAll) {
|
|
|
+ notifications = [self.app localNotificationOptionsById:ids];
|
|
|
+ }
|
|
|
+ else if (ids.count == 0) {
|
|
|
+ notifications = [self.app localNotificationOptionsByType:type];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ notifications = [self.app localNotificationOptionsByType:type
|
|
|
+ andId:ids];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ CDVPluginResult* result;
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
messageAsArray:notifications];
|
|
|
|
|
|
@@ -447,18 +542,35 @@
|
|
|
*/
|
|
|
- (void) hasPermission:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
- [self.commandDelegate runInBackground:^{
|
|
|
- CDVPluginResult* result;
|
|
|
- BOOL hasPermission;
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ [self.commandDelegate runInBackground:^{
|
|
|
+ [self.center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings* settings) {
|
|
|
+ BOOL authorized = settings.authorizationStatus == UNAuthorizationStatusAuthorized;
|
|
|
+ BOOL enabled = settings.notificationCenterSetting == UNNotificationSettingEnabled;
|
|
|
+ BOOL permitted = authorized && enabled;
|
|
|
+ CDVPluginResult* result;
|
|
|
+
|
|
|
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
+ messageAsBool:permitted];
|
|
|
+
|
|
|
+ [self.commandDelegate sendPluginResult:result
|
|
|
+ callbackId:command.callbackId];
|
|
|
+ }];
|
|
|
+ }];
|
|
|
+ } else {
|
|
|
+ [self.commandDelegate runInBackground:^{
|
|
|
+ CDVPluginResult* result;
|
|
|
+ BOOL hasPermission;
|
|
|
|
|
|
- hasPermission = [self.app hasPermissionToScheduleLocalNotifications];
|
|
|
+ hasPermission = [self.app hasPermissionToScheduleLocalNotifications];
|
|
|
|
|
|
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
- messageAsBool:hasPermission];
|
|
|
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
+ messageAsBool:hasPermission];
|
|
|
|
|
|
- [self.commandDelegate sendPluginResult:result
|
|
|
- callbackId:command.callbackId];
|
|
|
- }];
|
|
|
+ [self.commandDelegate sendPluginResult:result
|
|
|
+ callbackId:command.callbackId];
|
|
|
+ }];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -466,16 +578,27 @@
|
|
|
*/
|
|
|
- (void) registerPermission:(CDVInvokedUrlCommand*)command
|
|
|
{
|
|
|
- if ([[UIApplication sharedApplication]
|
|
|
- respondsToSelector:@selector(registerUserNotificationSettings:)])
|
|
|
- {
|
|
|
- _command = command;
|
|
|
-
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
- [self.app registerPermissionToScheduleLocalNotifications];
|
|
|
+ UNAuthorizationOptions options =
|
|
|
+ (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert);
|
|
|
+
|
|
|
+ [self.center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError* e) {
|
|
|
+ [self hasPermission:command];
|
|
|
+ }];
|
|
|
}];
|
|
|
} else {
|
|
|
- [self hasPermission:command];
|
|
|
+ if ([[UIApplication sharedApplication]
|
|
|
+ respondsToSelector:@selector(registerUserNotificationSettings:)])
|
|
|
+ {
|
|
|
+ _command = command;
|
|
|
+
|
|
|
+ [self.commandDelegate runInBackground:^{
|
|
|
+ [self.app registerPermissionToScheduleLocalNotifications];
|
|
|
+ }];
|
|
|
+ } else {
|
|
|
+ [self hasPermission:command];
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -485,47 +608,219 @@
|
|
|
/**
|
|
|
* Schedule the local notification.
|
|
|
*/
|
|
|
-- (void) scheduleLocalNotification:(UILocalNotification*)notification
|
|
|
+- (void) scheduleNotification:(UNMutableNotificationContent*)notification
|
|
|
{
|
|
|
- [self cancelForerunnerLocalNotification:notification];
|
|
|
- [self.app scheduleLocalNotification:notification];
|
|
|
+ [self.center addNotificationRequest:notification.request
|
|
|
+ withCompletionHandler:^(NSError* e) {
|
|
|
+ [self fireEvent:@"add" notification:notification.request];
|
|
|
+ }];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Update the local notification.
|
|
|
*/
|
|
|
-- (void) updateLocalNotification:(UILocalNotification*)notification
|
|
|
- withOptions:(NSDictionary*)newOptions
|
|
|
+- (void) updateNotification:(UILocalNotification*)notification
|
|
|
+ withOptions:(NSDictionary*)newOptions
|
|
|
{
|
|
|
NSMutableDictionary* options = [notification.userInfo mutableCopy];
|
|
|
|
|
|
[options addEntriesFromDictionary:newOptions];
|
|
|
[options setObject:[NSDate date] forKey:@"updatedAt"];
|
|
|
|
|
|
- notification = [[UILocalNotification alloc]
|
|
|
- initWithOptions:options];
|
|
|
-
|
|
|
- [self scheduleLocalNotification:notification];
|
|
|
+// notification = [[UILocalNotification alloc]
|
|
|
+// initWithOptions:options];
|
|
|
+//
|
|
|
+// [self scheduleLocalNotification:notification];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Cancel all local notifications.
|
|
|
*/
|
|
|
-- (void) cancelAllLocalNotifications
|
|
|
+- (void) cancelAllNotifications
|
|
|
{
|
|
|
- [self.app cancelAllLocalNotifications];
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ [self.center cancelAllNotifications];
|
|
|
+ } else {
|
|
|
+ [self.app cancelAllLocalNotifications];
|
|
|
+ }
|
|
|
+
|
|
|
[self.app setApplicationIconBadgeNumber:0];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Clear all local notifications.
|
|
|
*/
|
|
|
-- (void) clearAllLocalNotifications
|
|
|
+- (void) clearAllNotifications
|
|
|
{
|
|
|
- [self.app clearAllLocalNotifications];
|
|
|
+ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
|
|
|
+ [self.center clearAllNotifications];
|
|
|
+ } else {
|
|
|
+ [self.app clearAllLocalNotifications];
|
|
|
+ }
|
|
|
+
|
|
|
[self.app setApplicationIconBadgeNumber:0];
|
|
|
}
|
|
|
|
|
|
+#pragma mark -
|
|
|
+#pragma mark Delegates
|
|
|
+
|
|
|
+/**
|
|
|
+ * Called when a notification is delivered to a foreground app.
|
|
|
+ */
|
|
|
+- (void) userNotificationCenter:(UNUserNotificationCenter *)center
|
|
|
+ willPresentNotification:(UNNotification *)notification
|
|
|
+ withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
|
|
|
+{
|
|
|
+ [self fireEvent:@"trigger" notification:notification.request];
|
|
|
+ completionHandler(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Called to let your app know which action was selected by the user for a given
|
|
|
+ * notification.
|
|
|
+ */
|
|
|
+- (void) userNotificationCenter:(UNUserNotificationCenter *)center
|
|
|
+ didReceiveNotificationResponse:(UNNotificationResponse *)response
|
|
|
+ withCompletionHandler:(void (^)())completionHandler
|
|
|
+{
|
|
|
+ UNNotificationRequest* notification = response.notification.request;
|
|
|
+
|
|
|
+ [self fireEvent:@"click" notification:notification];
|
|
|
+
|
|
|
+ if ([notification.options isRepeating]) {
|
|
|
+ [self.center clearNotification:notification];
|
|
|
+ [self fireEvent:@"clear" notification:notification];
|
|
|
+ } else {
|
|
|
+ [self.center cancelNotification:notification];
|
|
|
+ [self fireEvent:@"cancel" notification:notification];
|
|
|
+ }
|
|
|
+
|
|
|
+ completionHandler();
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark -
|
|
|
+#pragma mark Life Cycle
|
|
|
+
|
|
|
+/**
|
|
|
+ * Registers obervers after plugin was initialized.
|
|
|
+ */
|
|
|
+- (void) pluginInitialize
|
|
|
+{
|
|
|
+ eventQueue = [[NSMutableArray alloc] init];
|
|
|
+
|
|
|
+ self.center.delegate = self;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark -
|
|
|
+#pragma mark Helper
|
|
|
+
|
|
|
+/**
|
|
|
+ * Retrieves the application state
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * Either "background" or "foreground"
|
|
|
+ */
|
|
|
+- (NSString*) applicationState
|
|
|
+{
|
|
|
+ UIApplicationState state = [self.app applicationState];
|
|
|
+
|
|
|
+ bool isActive = state == UIApplicationStateActive;
|
|
|
+
|
|
|
+ return isActive ? @"foreground" : @"background";
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Simply invokes the callback without any parameter.
|
|
|
+ */
|
|
|
+- (void) execCallback:(CDVInvokedUrlCommand*)command
|
|
|
+{
|
|
|
+ CDVPluginResult *result = [CDVPluginResult
|
|
|
+ resultWithStatus:CDVCommandStatus_OK];
|
|
|
+
|
|
|
+ [self.commandDelegate sendPluginResult:result
|
|
|
+ callbackId:command.callbackId];
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Short hand for shared application instance.
|
|
|
+ */
|
|
|
+- (UIApplication*) app
|
|
|
+{
|
|
|
+ return [UIApplication sharedApplication];
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Short hand for current notification center.
|
|
|
+ */
|
|
|
+- (UNUserNotificationCenter*) center
|
|
|
+{
|
|
|
+ return [UNUserNotificationCenter currentNotificationCenter];
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Fire general event.
|
|
|
+ */
|
|
|
+- (void) fireEvent:(NSString*)event
|
|
|
+{
|
|
|
+ [self fireEvent:event notification:NULL];
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Fire event for local notification.
|
|
|
+ */
|
|
|
+- (void) fireEvent:(NSString*)event
|
|
|
+ notification:(UNNotificationRequest*)notification
|
|
|
+{
|
|
|
+ NSString* js;
|
|
|
+ NSString* appState = [self applicationState];
|
|
|
+ NSString* params = [NSString stringWithFormat:@"\"%@\"", appState];
|
|
|
+
|
|
|
+ if (notification) {
|
|
|
+ NSString* args = [notification encodeToJSON];
|
|
|
+ params = [NSString stringWithFormat:@"%@,'%@'", args, appState];
|
|
|
+ }
|
|
|
+
|
|
|
+ js = [NSString stringWithFormat:
|
|
|
+ @"cordova.plugins.notification.local.core.fireEvent('%@', %@)",
|
|
|
+ event, params];
|
|
|
+
|
|
|
+ if (deviceready) {
|
|
|
+ [self.commandDelegate evalJs:js];
|
|
|
+ } else {
|
|
|
+ [self.eventQueue addObject:js];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark -
|
|
|
+#pragma mark ios 9
|
|
|
+
|
|
|
+/**
|
|
|
+ * Schedule the local notification.
|
|
|
+ */
|
|
|
+- (void) scheduleLocalNotification:(UILocalNotification*)notification
|
|
|
+{
|
|
|
+ [self cancelForerunnerLocalNotification:notification];
|
|
|
+ [self.app scheduleLocalNotification:notification];
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Update the local notification.
|
|
|
+ */
|
|
|
+- (void) updateLocalNotification:(UILocalNotification*)notification
|
|
|
+ withOptions:(NSDictionary*)newOptions
|
|
|
+{
|
|
|
+ NSMutableDictionary* options = [notification.userInfo mutableCopy];
|
|
|
+
|
|
|
+ [options addEntriesFromDictionary:newOptions];
|
|
|
+ [options setObject:[NSDate date] forKey:@"updatedAt"];
|
|
|
+
|
|
|
+ notification = [[UILocalNotification alloc]
|
|
|
+ initWithOptions:options];
|
|
|
+
|
|
|
+ [self scheduleLocalNotification:notification];
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Cancel a maybe given forerunner with the same ID.
|
|
|
*/
|
|
|
@@ -558,14 +853,11 @@
|
|
|
&& notification.timeIntervalSinceFireDate > seconds)
|
|
|
{
|
|
|
[self.app cancelLocalNotification:notification];
|
|
|
- [self fireEvent:@"cancel" notification:notification];
|
|
|
+ [self fireEvent:@"cancel" localnotification:notification];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#pragma mark -
|
|
|
-#pragma mark Delegates
|
|
|
-
|
|
|
/**
|
|
|
* Calls the cancel or trigger event after a local notification was received.
|
|
|
* Cancels the local notification if autoCancel was set to true.
|
|
|
@@ -580,16 +872,16 @@
|
|
|
NSTimeInterval timeInterval = [notification timeIntervalSinceLastTrigger];
|
|
|
NSString* event = timeInterval < 0.2 && deviceready ? @"trigger" : @"click";
|
|
|
|
|
|
- [self fireEvent:event notification:notification];
|
|
|
+ [self fireEvent:event localnotification:notification];
|
|
|
|
|
|
if (![event isEqualToString:@"click"])
|
|
|
return;
|
|
|
|
|
|
if ([notification isRepeating]) {
|
|
|
- [self fireEvent:@"clear" notification:notification];
|
|
|
+ [self fireEvent:@"clear" localnotification:notification];
|
|
|
} else {
|
|
|
[self.app cancelLocalNotification:notification];
|
|
|
- [self fireEvent:@"cancel" notification:notification];
|
|
|
+ [self fireEvent:@"cancel" localnotification:notification];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -624,17 +916,6 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#pragma mark -
|
|
|
-#pragma mark Life Cycle
|
|
|
-
|
|
|
-/**
|
|
|
- * Registers obervers after plugin was initialized.
|
|
|
- */
|
|
|
-- (void) pluginInitialize
|
|
|
-{
|
|
|
- eventQueue = [[NSMutableArray alloc] init];
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* Clears all single repeating notifications which are older then 5 days
|
|
|
* before the app terminates.
|
|
|
@@ -644,98 +925,11 @@
|
|
|
[self cancelAllNotificationsWhichAreOlderThen:432000];
|
|
|
}
|
|
|
|
|
|
-#pragma mark -
|
|
|
-#pragma mark Helper
|
|
|
-
|
|
|
-/**
|
|
|
- * Retrieves the application state
|
|
|
- *
|
|
|
- * @return
|
|
|
- * Either "background" or "foreground"
|
|
|
- */
|
|
|
-- (NSString*) applicationState
|
|
|
-{
|
|
|
- UIApplicationState state = [self.app applicationState];
|
|
|
-
|
|
|
- bool isActive = state == UIApplicationStateActive;
|
|
|
-
|
|
|
- return isActive ? @"foreground" : @"background";
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Simply invokes the callback without any parameter.
|
|
|
- */
|
|
|
-- (void) execCallback:(CDVInvokedUrlCommand*)command
|
|
|
-{
|
|
|
- CDVPluginResult *result = [CDVPluginResult
|
|
|
- resultWithStatus:CDVCommandStatus_OK];
|
|
|
-
|
|
|
- [self.commandDelegate sendPluginResult:result
|
|
|
- callbackId:command.callbackId];
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Short hand for shared application instance.
|
|
|
- */
|
|
|
-- (UIApplication*) app
|
|
|
-{
|
|
|
- return [UIApplication sharedApplication];
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Fire general event.
|
|
|
- */
|
|
|
-- (void) fireEvent:(NSString*)event
|
|
|
-{
|
|
|
- [self fireEvent:event notification:NULL];
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* Fire event for local notification.
|
|
|
*/
|
|
|
-- (void) fireEvent:(NSString*)event notification:(UILocalNotification*)notification
|
|
|
+- (void) fireEvent:(NSString*)event localnotification:(UILocalNotification*)notification
|
|
|
{
|
|
|
- if (IsAtLeastiOSVersion(@"10.0")) {
|
|
|
- UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
|
|
|
- content.title = [NSString localizedUserNotificationStringForKey:notification.alertTitle arguments:nil];
|
|
|
- content.body = [NSString localizedUserNotificationStringForKey:notification.alertBody
|
|
|
- arguments:nil];
|
|
|
- content.sound = [UNNotificationSound defaultSound];
|
|
|
-
|
|
|
-
|
|
|
- NSDate *fireDate = notification.fireDate;
|
|
|
- if(fireDate==nil) {
|
|
|
- fireDate = [NSDate date];
|
|
|
- }
|
|
|
- NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
|
|
|
- // Extract all date components into dateComponents
|
|
|
- NSDateComponents *dateComponents = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
|
|
|
- | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
|
|
|
- fromDate:fireDate];
|
|
|
- [dateComponents setTimeZone:[NSTimeZone defaultTimeZone]];
|
|
|
-
|
|
|
- /// 4. update application icon badge number
|
|
|
- //content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
|
|
|
-
|
|
|
- // Deliver the notification at the fire date.
|
|
|
- UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:NO];
|
|
|
-
|
|
|
- NSString *identifier = @"DefaultNotificationIdentifier";
|
|
|
- if(notification.userInfo!=nil && [notification.userInfo objectForKey:@"id"]!=nil) {
|
|
|
- identifier = [notification.userInfo objectForKey:@"id"];
|
|
|
- }
|
|
|
-
|
|
|
- UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
|
|
|
-
|
|
|
- /// 3. schedule localNotification
|
|
|
- UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
- [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
|
|
|
- if (!error) {
|
|
|
- NSLog(@"add NotificationRequest succeeded!");
|
|
|
- }
|
|
|
- }];
|
|
|
- }
|
|
|
-
|
|
|
NSString* js;
|
|
|
NSString* params = [NSString stringWithFormat:
|
|
|
@"\"%@\"", self.applicationState];
|