Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit ab1410f

Browse files
committed
🍎 Add code to save previous UNUserNotificationCenterDelegate and call it for all non-push notifications
1 parent 532be4b commit ab1410f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/ios/AppDelegate+notification.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ extern NSString *const pushPluginApplicationDidBecomeActiveNotification;
2121

2222
@property (nonatomic, retain) NSDictionary *launchNotification;
2323
@property (nonatomic, retain) NSNumber *coldstart;
24+
@property (nonatomic, retain) id<UNUserNotificationCenterDelegate> clobberedDelegate;
2425

2526
@end

src/ios/AppDelegate+notification.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,28 @@
1010
#import "PushPlugin.h"
1111
#import <objc/runtime.h>
1212

13+
static char clobberedDelegateKey;
1314
static char launchNotificationKey;
1415
static char coldstartKey;
1516
NSString *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,14 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
190209
willPresentNotification:(UNNotification *)notification
191210
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
192211
{
212+
[self.clobberedDelegate userNotificationCenter:center
213+
willPresentNotification:notification
214+
withCompletionHandler:completionHandler];
215+
216+
if (![notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
217+
return;
218+
}
219+
193220
NSLog( @"NotificationCenter Handle push from foreground" );
194221
// custom code to handle push while app is in the foreground
195222
PushPlugin *pushHandler = [self getCommandInstance:@"PushNotification"];
@@ -204,6 +231,14 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
204231
didReceiveNotificationResponse:(UNNotificationResponse *)response
205232
withCompletionHandler:(void(^)(void))completionHandler
206233
{
234+
[self.clobberedDelegate userNotificationCenter:center
235+
didReceiveNotificationResponse:response
236+
withCompletionHandler:completionHandler];
237+
238+
if (![response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
239+
return;
240+
}
241+
207242
NSLog(@"Push Plugin didReceiveNotificationResponse: actionIdentifier %@, notification: %@", response.actionIdentifier,
208243
response.notification.request.content.userInfo);
209244
NSMutableDictionary *userInfo = [response.notification.request.content.userInfo mutableCopy];
@@ -260,6 +295,18 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
260295

261296
// The accessors use an Associative Reference since you can't define a iVar in a category
262297
// http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocAssociativeReferences.html
298+
- (id<UNUserNotificationCenterDelegate>)clobberedDelegate
299+
{
300+
WeakObjectContainer<id<UNUserNotificationCenterDelegate>> *weakDelegateContainer = objc_getAssociatedObject(self, &clobberedDelegateKey);
301+
return weakDelegateContainer.object;
302+
}
303+
304+
- (void)setClobberedDelegate:(id<UNUserNotificationCenterDelegate>)clobberedDelegate
305+
{
306+
WeakObjectContainer<id<UNUserNotificationCenterDelegate>> *weakDelegateContainer = [[WeakObjectContainer alloc] initWithObject:clobberedDelegate];
307+
objc_setAssociatedObject(self, &clobberedDelegateKey, weakDelegateContainer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
308+
}
309+
263310
- (NSMutableArray *)launchNotification
264311
{
265312
return objc_getAssociatedObject(self, &launchNotificationKey);

0 commit comments

Comments
 (0)