diff --git a/src/ios/AppDelegate+notification.h b/src/ios/AppDelegate+notification.h index 9479b1839..bde4fa17c 100644 --- a/src/ios/AppDelegate+notification.h +++ b/src/ios/AppDelegate+notification.h @@ -21,5 +21,6 @@ extern NSString *const pushPluginApplicationDidBecomeActiveNotification; @property (nonatomic, retain) NSDictionary *launchNotification; @property (nonatomic, retain) NSNumber *coldstart; +@property (nonatomic, retain) id clobberedDelegate; @end diff --git a/src/ios/AppDelegate+notification.m b/src/ios/AppDelegate+notification.m index b04c47687..0ef4f1a19 100644 --- a/src/ios/AppDelegate+notification.m +++ b/src/ios/AppDelegate+notification.m @@ -10,10 +10,28 @@ #import "PushPlugin.h" #import +static char clobberedDelegateKey; static char launchNotificationKey; static char coldstartKey; NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginApplicationDidBecomeActiveNotification"; +@interface WeakObjectContainer : NSObject + +@property (nonatomic, readonly, weak) T object; + +@end + +@implementation WeakObjectContainer + +- (instancetype) initWithObject:(id)object +{ + if (self = [super init]) { + _object = object; + } + return self; +} + +@end @implementation AppDelegate (notification) @@ -56,6 +74,7 @@ + (void)load - (AppDelegate *)pushPluginSwizzledInit { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + self.clobberedDelegate = center.delegate; center.delegate = self; [[NSNotificationCenter defaultCenter]addObserver:self @@ -190,6 +209,14 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { + [self.clobberedDelegate userNotificationCenter:center + willPresentNotification:notification + withCompletionHandler:completionHandler]; + + if (![notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { + return; + } + NSLog( @"NotificationCenter Handle push from foreground" ); // custom code to handle push while app is in the foreground PushPlugin *pushHandler = [self getCommandInstance:@"PushNotification"]; @@ -204,6 +231,14 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { + [self.clobberedDelegate userNotificationCenter:center + didReceiveNotificationResponse:response + withCompletionHandler:completionHandler]; + + if (![response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { + return; + } + NSLog(@"Push Plugin didReceiveNotificationResponse: actionIdentifier %@, notification: %@", response.actionIdentifier, response.notification.request.content.userInfo); NSMutableDictionary *userInfo = [response.notification.request.content.userInfo mutableCopy]; @@ -260,6 +295,18 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center // 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 +- (id)clobberedDelegate +{ + WeakObjectContainer> *weakDelegateContainer = objc_getAssociatedObject(self, &clobberedDelegateKey); + return weakDelegateContainer.object; +} + +- (void)setClobberedDelegate:(id)clobberedDelegate +{ + WeakObjectContainer> *weakDelegateContainer = [[WeakObjectContainer alloc] initWithObject:clobberedDelegate]; + objc_setAssociatedObject(self, &clobberedDelegateKey, weakDelegateContainer, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + - (NSMutableArray *)launchNotification { return objc_getAssociatedObject(self, &launchNotificationKey);