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

Commit d14be79

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

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
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

17-
1818
@implementation AppDelegate (notification)
1919

2020
- (id) getCommandInstance:(NSString*)className
@@ -56,6 +56,7 @@ + (void)load
5656
- (AppDelegate *)pushPluginSwizzledInit
5757
{
5858
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
59+
self.clobberedDelegate = center.delegate;
5960
center.delegate = self;
6061

6162
[[NSNotificationCenter defaultCenter]addObserver:self
@@ -190,6 +191,14 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
190191
willPresentNotification:(UNNotification *)notification
191192
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
192193
{
194+
if (![notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
195+
{
196+
[self.clobberedDelegate userNotificationCenter:center
197+
willPresentNotification:notification
198+
withCompletionHandler:completionHandler];
199+
return;
200+
}
201+
193202
NSLog( @"NotificationCenter Handle push from foreground" );
194203
// custom code to handle push while app is in the foreground
195204
PushPlugin *pushHandler = [self getCommandInstance:@"PushNotification"];
@@ -204,6 +213,14 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
204213
didReceiveNotificationResponse:(UNNotificationResponse *)response
205214
withCompletionHandler:(void(^)(void))completionHandler
206215
{
216+
if (![response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
217+
{
218+
[self.clobberedDelegate userNotificationCenter:center
219+
didReceiveNotificationResponse:response
220+
withCompletionHandler:completionHandler];
221+
return;
222+
}
223+
207224
NSLog(@"Push Plugin didReceiveNotificationResponse: actionIdentifier %@, notification: %@", response.actionIdentifier,
208225
response.notification.request.content.userInfo);
209226
NSMutableDictionary *userInfo = [response.notification.request.content.userInfo mutableCopy];
@@ -260,6 +277,16 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
260277

261278
// The accessors use an Associative Reference since you can't define a iVar in a category
262279
// http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocAssociativeReferences.html
280+
- (id<UNUserNotificationCenterDelegate>)clobberedDelegate
281+
{
282+
return objc_getAssociatedObject(self, &clobberedDelegateKey);
283+
}
284+
285+
- (void)setClobberedDelegate:(id<UNUserNotificationCenterDelegate>)clobberedDelegate
286+
{
287+
objc_setAssociatedObject(self, &clobberedDelegateKey, clobberedDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
288+
}
289+
263290
- (NSMutableArray *)launchNotification
264291
{
265292
return objc_getAssociatedObject(self, &launchNotificationKey);

0 commit comments

Comments
 (0)