Skip to content

Commit

Permalink
Do not shadow UNUserNotificationCenter delegates
Browse files Browse the repository at this point in the history
With this change the plugin works together with
cordova-plugin-local-notifications. Based on
arnesson/cordova-plugin-firebase#680.
  • Loading branch information
jkronborg committed Apr 4, 2019
1 parent 92049f1 commit 9c5eae4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ios/AppDelegate+FCMPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,20 @@ @interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegat
#define NSFoundationVersionNumber_iOS_9_x_Max 1299
#endif

#define kDelegateKey @"delegate"

@implementation AppDelegate (MCPlugin)

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- (void)setDelegate:(id)delegate {
objc_setAssociatedObject(self, kDelegateKey, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)delegate {
return objc_getAssociatedObject(self, kDelegateKey);
}
#endif

static NSData *lastPush;
NSString *const kGCMMessageIDKey = @"gcm.message_id";

Expand Down Expand Up @@ -84,6 +96,7 @@ - (BOOL)application:(UIApplication *)application customDidFinishLaunchingWithOpt
}];

// For iOS 10 display notification (sent via APNS)
self.delegate = [UNUserNotificationCenter currentNotificationCenter].delegate;
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
// For iOS 10 data message (sent via FCM)
[FIRMessaging messaging].remoteMessageDelegate = self;
Expand Down Expand Up @@ -114,6 +127,14 @@ - (BOOL)application:(UIApplication *)application customDidFinishLaunchingWithOpt
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[self.delegate userNotificationCenter:center
willPresentNotification:notification
withCompletionHandler:completionHandler];

if (![notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class]) {
return;
}

// Print message ID.
NSDictionary *userInfo = notification.request.content.userInfo;
if (userInfo[kGCMMessageIDKey]) {
Expand All @@ -138,6 +159,14 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler {
[self.delegate userNotificationCenter:center
didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];

if (![response.notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class]) {
return;
}

NSDictionary *userInfo = response.notification.request.content.userInfo;
if (userInfo[kGCMMessageIDKey]) {
NSLog(@"Message ID 2: %@", userInfo[kGCMMessageIDKey]);
Expand Down

0 comments on commit 9c5eae4

Please sign in to comment.