Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ios/AppDelegate+notification.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ extern NSString *const pushPluginApplicationDidBecomeActiveNotification;

@property (nonatomic, retain) NSDictionary *launchNotification;
@property (nonatomic, retain) NSNumber *coldstart;
@property (nonatomic, retain) id<UNUserNotificationCenterDelegate> clobberedDelegate;

@end
47 changes: 47 additions & 0 deletions src/ios/AppDelegate+notification.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,28 @@
#import "PushPlugin.h"
#import <objc/runtime.h>

static char clobberedDelegateKey;
static char launchNotificationKey;
static char coldstartKey;
NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginApplicationDidBecomeActiveNotification";

@interface WeakObjectContainer<T> : 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)

Expand Down Expand Up @@ -56,6 +74,7 @@ + (void)load
- (AppDelegate *)pushPluginSwizzledInit
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
self.clobberedDelegate = center.delegate;
center.delegate = self;

[[NSNotificationCenter defaultCenter]addObserver:self
Expand Down Expand Up @@ -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"];
Expand All @@ -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];
Expand Down Expand Up @@ -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<UNUserNotificationCenterDelegate>)clobberedDelegate
{
WeakObjectContainer<id<UNUserNotificationCenterDelegate>> *weakDelegateContainer = objc_getAssociatedObject(self, &clobberedDelegateKey);
return weakDelegateContainer.object;
}

- (void)setClobberedDelegate:(id<UNUserNotificationCenterDelegate>)clobberedDelegate
{
WeakObjectContainer<id<UNUserNotificationCenterDelegate>> *weakDelegateContainer = [[WeakObjectContainer alloc] initWithObject:clobberedDelegate];
objc_setAssociatedObject(self, &clobberedDelegateKey, weakDelegateContainer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMutableArray *)launchNotification
{
return objc_getAssociatedObject(self, &launchNotificationKey);
Expand Down