1010#import " PushPlugin.h"
1111#import < objc/runtime.h>
1212
13+ static char clobberedDelegateKey;
1314static char launchNotificationKey;
1415static char coldstartKey;
1516NSString *const pushPluginApplicationDidBecomeActiveNotification = @" pushPluginApplicationDidBecomeActiveNotification" ;
1617
18+ @interface WeakObjectContainer <T> : NSObject
19+
20+ @property (nonatomic , readonly , weak ) T object;
21+
22+ @end
23+
24+ @implementation WeakObjectContainer
25+
26+ - (instancetype ) initWithObject : (id )object
27+ {
28+ if (self = [super init ]) {
29+ _object = object;
30+ }
31+ return self;
32+ }
33+
34+ @end
1735
1836@implementation AppDelegate (notification)
1937
@@ -56,6 +74,7 @@ + (void)load
5674- (AppDelegate *)pushPluginSwizzledInit
5775{
5876 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter ];
77+ self.clobberedDelegate = center.delegate ;
5978 center.delegate = self;
6079
6180 [[NSNotificationCenter defaultCenter ]addObserver:self
@@ -190,6 +209,13 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
190209 willPresentNotification : (UNNotification *)notification
191210 withCompletionHandler : (void (^)(UNNotificationPresentationOptions options))completionHandler
192211{
212+ if (![notification.request.trigger isKindOfClass: [UNPushNotificationTrigger class ]]) {
213+ [self .clobberedDelegate userNotificationCenter: center
214+ willPresentNotification: notification
215+ withCompletionHandler: completionHandler];
216+ return ;
217+ }
218+
193219 NSLog ( @" NotificationCenter Handle push from foreground" );
194220 // custom code to handle push while app is in the foreground
195221 PushPlugin *pushHandler = [self getCommandInstance: @" PushNotification" ];
@@ -204,6 +230,13 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
204230didReceiveNotificationResponse : (UNNotificationResponse *)response
205231 withCompletionHandler : (void (^)(void ))completionHandler
206232{
233+ if (![response.notification.request.trigger isKindOfClass: [UNPushNotificationTrigger class ]]) {
234+ [self .clobberedDelegate userNotificationCenter: center
235+ didReceiveNotificationResponse: response
236+ withCompletionHandler: completionHandler];
237+ return ;
238+ }
239+
207240 NSLog (@" Push Plugin didReceiveNotificationResponse: actionIdentifier %@ , notification: %@ " , response.actionIdentifier ,
208241 response.notification .request .content .userInfo );
209242 NSMutableDictionary *userInfo = [response.notification.request.content.userInfo mutableCopy ];
@@ -260,6 +293,18 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
260293
261294// The accessors use an Associative Reference since you can't define a iVar in a category
262295// http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocAssociativeReferences.html
296+ - (id <UNUserNotificationCenterDelegate >)clobberedDelegate
297+ {
298+ WeakObjectContainer<id <UNUserNotificationCenterDelegate >> *weakDelegateContainer = objc_getAssociatedObject (self, &clobberedDelegateKey);
299+ return weakDelegateContainer.object ;
300+ }
301+
302+ - (void )setClobberedDelegate : (id <UNUserNotificationCenterDelegate >)clobberedDelegate
303+ {
304+ WeakObjectContainer<id <UNUserNotificationCenterDelegate >> *weakDelegateContainer = [[WeakObjectContainer alloc ] initWithObject: clobberedDelegate];
305+ objc_setAssociatedObject (self, &clobberedDelegateKey, weakDelegateContainer, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
306+ }
307+
263308- (NSMutableArray *)launchNotification
264309{
265310 return objc_getAssociatedObject (self, &launchNotificationKey);
0 commit comments