diff --git a/src/ios/AppDelegate+notification.h b/src/ios/AppDelegate+notification.h index 647424178..a81f3724b 100644 --- a/src/ios/AppDelegate+notification.h +++ b/src/ios/AppDelegate+notification.h @@ -12,7 +12,7 @@ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler; -- (void)applicationDidBecomeActive:(UIApplication *)application; +- (void)pushPluginOnApplicationDidBecomeActive:(UIApplication *)application; - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler; - (id) getCommandInstance:(NSString*)className; diff --git a/src/ios/AppDelegate+notification.m b/src/ios/AppDelegate+notification.m index d067db17d..2190c900c 100644 --- a/src/ios/AppDelegate+notification.m +++ b/src/ios/AppDelegate+notification.m @@ -23,21 +23,47 @@ - (id) getCommandInstance:(NSString*)className // Instead we will use method swizzling. we set this up in the load call. + (void)load { - Method original, swizzled; - - original = class_getInstanceMethod(self, @selector(init)); - swizzled = class_getInstanceMethod(self, @selector(swizzled_init)); - method_exchangeImplementations(original, swizzled); + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + Class class = [self class]; + + SEL originalSelector = @selector(init); + SEL swizzledSelector = @selector(pushPluginSwizzledInit); + + Method original = class_getInstanceMethod(class, originalSelector); + Method swizzled = class_getInstanceMethod(class, swizzledSelector); + + BOOL didAddMethod = + class_addMethod(class, + originalSelector, + method_getImplementation(swizzled), + method_getTypeEncoding(swizzled)); + + if (didAddMethod) { + class_replaceMethod(class, + swizzledSelector, + method_getImplementation(original), + method_getTypeEncoding(original)); + } else { + method_exchangeImplementations(original, swizzled); + } + }); } -- (AppDelegate *)swizzled_init +- (AppDelegate *)pushPluginSwizzledInit { - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createNotificationChecker:) - name:@"UIApplicationDidFinishLaunchingNotification" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(createNotificationChecker:) + name:UIApplicationDidFinishLaunchingNotification + object:nil]; + [[NSNotificationCenter defaultCenter]addObserver:self + selector:@selector(pushPluginOnApplicationDidBecomeActive:) + name:UIApplicationDidBecomeActiveNotification + object:nil]; // This actually calls the original init method over in AppDelegate. Equivilent to calling super // on an overrided method, this is not recursive, although it appears that way. neat huh? - return [self swizzled_init]; + return [self pushPluginSwizzledInit]; } // This code will be called immediately after application:didFinishLaunchingWithOptions:. We need @@ -127,10 +153,12 @@ - (BOOL)userHasRemoteNotificationsEnabled { } } -- (void)applicationDidBecomeActive:(UIApplication *)application { +- (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification { NSLog(@"active"); + UIApplication *application = notification.object; + PushPlugin *pushHandler = [self getCommandInstance:@"PushNotification"]; if (pushHandler.clearBadge) { NSLog(@"PushPlugin clearing badge");