diff --git a/src/ios/CDVAppDelegate+notification.h b/src/ios/CDVAppDelegate+notification.h index df3685d0e..d2d26bacf 100644 --- a/src/ios/CDVAppDelegate+notification.h +++ b/src/ios/CDVAppDelegate+notification.h @@ -1,10 +1,8 @@ // // CDVAppDelegate+notification.h -// pushtest // // Created by Robert Easterday on 10/26/12. // -// #import @@ -16,9 +14,4 @@ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler; -- (id) getCommandInstance:(NSString*)className; - -@property (nonatomic, retain) NSDictionary *launchNotification; -@property (nonatomic, retain) NSNumber *coldstart; - @end diff --git a/src/ios/CDVAppDelegate+notification.m b/src/ios/CDVAppDelegate+notification.m index 3a0b045fd..cddaeb4e0 100644 --- a/src/ios/CDVAppDelegate+notification.m +++ b/src/ios/CDVAppDelegate+notification.m @@ -1,25 +1,15 @@ // // CDVAppDelegate+notification.m -// pushtest // // Created by Robert Easterday on 10/26/12. // -// #import "CDVAppDelegate+notification.h" #import "PushPlugin.h" #import -static char launchNotificationKey; -static char coldstartKey; - @implementation CDVAppDelegate (notification) -- (id) getCommandInstance:(NSString*)className -{ - return [self.viewController getCommandInstance:className]; -} - // its dangerous to override a method from within a category. // Instead we will use method swizzling. we set this up in the load call. + (void)load @@ -103,32 +93,4 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti [NSNotificationCenter.defaultCenter postNotificationName:@"CordovaPluginPushDidReceiveNotificationResponse" object:nil userInfo:notificationInfo]; } -// The accessors use an Associative Reference since you can't define a iVar in a category -// http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocAssociativeReferences.html -- (NSMutableArray *)launchNotification -{ - return objc_getAssociatedObject(self, &launchNotificationKey); -} - -- (void)setLaunchNotification:(NSDictionary *)aDictionary -{ - objc_setAssociatedObject(self, &launchNotificationKey, aDictionary, OBJC_ASSOCIATION_RETAIN_NONATOMIC); -} - -- (NSNumber *)coldstart -{ - return objc_getAssociatedObject(self, &coldstartKey); -} - -- (void)setColdstart:(NSNumber *)aNumber -{ - objc_setAssociatedObject(self, &coldstartKey, aNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC); -} - -- (void)dealloc -{ - self.launchNotification = nil; // clear the association and release the object - self.coldstart = nil; -} - @end diff --git a/src/ios/PushPlugin.h b/src/ios/PushPlugin.h index d75029aa1..1b1ff7680 100644 --- a/src/ios/PushPlugin.h +++ b/src/ios/PushPlugin.h @@ -30,34 +30,15 @@ #import @interface PushPlugin : CDVPlugin -{ - NSDictionary *notificationMessage; - BOOL isInline; - BOOL clearBadge; - BOOL forceShow; - - NSMutableDictionary *handlerObj; - void (^completionHandler)(UIBackgroundFetchResult); -} @property (nonatomic, copy) NSString *callbackId; -@property (nonatomic, strong) NSDictionary *notificationMessage; -@property BOOL isInline; -@property BOOL coldstart; -@property BOOL clearBadge; -@property BOOL forceShow; -@property (nonatomic, strong) NSMutableDictionary *handlerObj; - - (void)init:(CDVInvokedUrlCommand*)command; - (void)unregister:(CDVInvokedUrlCommand*)command; - (void)subscribe:(CDVInvokedUrlCommand*)command; - (void)unsubscribe:(CDVInvokedUrlCommand*)command; - (void)clearNotification:(CDVInvokedUrlCommand*)command; -- (void)setNotificationMessage:(NSDictionary *)notification; -- (void)notificationReceived; - // VoIP Features - (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type; - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type; diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 84e3d4055..9572e7a0e 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -31,19 +31,23 @@ @interface PushPlugin () @property (nonatomic, strong) PushPluginFCM *pushPluginFCM; + @property (nonatomic, strong) NSDictionary *launchNotification; +@property (nonatomic, strong) NSDictionary *notificationMessage; +@property (nonatomic, strong) NSMutableDictionary *handlerObj; + +@property (nonatomic, assign) BOOL isInline; +@property (nonatomic, assign) BOOL clearBadge; +@property (nonatomic, assign) BOOL forceShow; +@property (nonatomic, assign) BOOL coldstart; + +@property (nonatomic, copy) void (^backgroundTaskcompletionHandler)(UIBackgroundFetchResult); @end @implementation PushPlugin -@synthesize notificationMessage; -@synthesize isInline; -@synthesize coldstart; @synthesize callbackId; -@synthesize clearBadge; -@synthesize forceShow; -@synthesize handlerObj; - (void)pluginInitialize { self.pushPluginFCM = [[PushPluginFCM alloc] initWithGoogleServicePlist]; @@ -182,7 +186,7 @@ - (void)init:(CDVInvokedUrlCommand *)command { [center setNotificationCategories:[settings categories]]; // If there is a pending startup notification, we will delay to allow JS event handlers to setup - if (notificationMessage) { + if (self.notificationMessage) { dispatch_async(dispatch_get_main_queue(), ^{ [self performSelector:@selector(notificationReceived) withObject:nil afterDelay: 0.5]; }); @@ -406,14 +410,14 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification { - (void)notificationReceived { NSLog(@"[PushPlugin] Notification received"); - if (notificationMessage && self.callbackId != nil) + if (self.notificationMessage && self.callbackId != nil) { NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:4]; NSMutableDictionary* additionalData = [NSMutableDictionary dictionaryWithCapacity:4]; - for (id key in notificationMessage) { + for (id key in self.notificationMessage) { if ([key isEqualToString:@"aps"]) { - id aps = [notificationMessage objectForKey:@"aps"]; + id aps = [self.notificationMessage objectForKey:@"aps"]; for(id key in aps) { NSLog(@"[PushPlugin] key: %@", key); @@ -448,17 +452,17 @@ - (void)notificationReceived { } } } else { - [additionalData setObject:[notificationMessage objectForKey:key] forKey:key]; + [additionalData setObject:[self.notificationMessage objectForKey:key] forKey:key]; } } - if (isInline) { + if (self.isInline) { [additionalData setObject:[NSNumber numberWithBool:YES] forKey:@"foreground"]; } else { [additionalData setObject:[NSNumber numberWithBool:NO] forKey:@"foreground"]; } - if (coldstart) { + if (self.coldstart) { [additionalData setObject:[NSNumber numberWithBool:YES] forKey:@"coldstart"]; } else { [additionalData setObject:[NSNumber numberWithBool:NO] forKey:@"coldstart"]; @@ -584,13 +588,13 @@ - (void)stopBackgroundTask:(NSTimer *)timer { NSLog(@"[PushPlugin] stopBackgroundTask called"); - if (handlerObj) { + if (self.handlerObj) { NSLog(@"[PushPlugin] handlerObj"); - completionHandler = [handlerObj[[timer userInfo]] copy]; - if (completionHandler) { + self.backgroundTaskcompletionHandler = [self.handlerObj[[timer userInfo]] copy]; + if (self.backgroundTaskcompletionHandler) { NSLog(@"[PushPlugin] stopBackgroundTask (remaining t: %f)", app.backgroundTimeRemaining); - completionHandler(UIBackgroundFetchResultNewData); - completionHandler = nil; + self.backgroundTaskcompletionHandler(UIBackgroundFetchResultNewData); + self.backgroundTaskcompletionHandler = nil; } } } @@ -661,8 +665,7 @@ - (void)handleNotificationSettingsWithAuthorizationOptions:(NSNumber *)authoriza }]; } -- (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull void (^)(BOOL))completionHandler -{ +- (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull void (^)(BOOL))completionHandler { [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { switch (settings.authorizationStatus) @@ -671,11 +674,19 @@ - (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull voi case UNAuthorizationStatusNotDetermined: completionHandler(NO); break; + case UNAuthorizationStatusAuthorized: + case UNAuthorizationStatusEphemeral: + case UNAuthorizationStatusProvisional: completionHandler(YES); break; } }]; } +- (void)dealloc { + self.launchNotification = nil; + self.coldstart = nil; +} + @end