Skip to content

Commit

Permalink
co-existence with cordova-plugin-local-notification on iOS
Browse files Browse the repository at this point in the history
Local notifications on iOS via cordova-plugin-local-notification are working again.
Click-callback for remote notifications not working right now.

Details:
UNUserNotificationCenter accepts a single delegate that handles the display of notifications. cordova-plugin-firebase now stores a reference to the existing delegate if any and proxies the delegate-calls (cordova-plugin-local-notification is already doing that in the other direction when applicable).
  • Loading branch information
FWink committed May 6, 2018
1 parent 16a0aa8 commit 4e9a0f4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/ios/AppDelegate+FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ @interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegat
#endif

#define kApplicationInBackgroundKey @"applicationInBackground"
#define kDelegateKey @"delegate"

@implementation AppDelegate (FirebasePlugin)

#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

+ (void)load {
Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
Method swizzled = class_getInstanceMethod(self, @selector(application:swizzledDidFinishLaunchingWithOptions:));
Expand All @@ -41,6 +54,10 @@ - (BOOL)application:(UIApplication *)application swizzledDidFinishLaunchingWithO
// [START set_messaging_delegate]
[FIRMessaging messaging].delegate = self;
// [END set_messaging_delegate]
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
self.delegate = [UNUserNotificationCenter currentNotificationCenter].delegate;
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
#endif

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
Expand Down Expand Up @@ -120,6 +137,15 @@ - (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemot
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

[self.delegate userNotificationCenter:center
willPresentNotification:notification
withCompletionHandler:completionHandler];

UNNotificationRequest* toast = notification.request;
if (![toast.trigger isKindOfClass:UNPushNotificationTrigger.class])
return;

NSDictionary *mutableUserInfo = [notification.request.content.userInfo mutableCopy];

[mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
Expand All @@ -130,6 +156,15 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
[FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
}

- (void) userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
{
[self.delegate userNotificationCenter:center
didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];
}

// Receive data message on iOS 10 devices.
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
Expand Down
2 changes: 0 additions & 2 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,13 @@ - (void)grantPermission:(CDVInvokedUrlCommand *)command {

if (![NSThread isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
[[FIRMessaging messaging] setDelegate:self];
[[UIApplication sharedApplication] registerForRemoteNotifications];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: granted ? CDVCommandStatus_OK : CDVCommandStatus_ERROR];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
});
} else {
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
[[FIRMessaging messaging] setDelegate:self];
[[UIApplication sharedApplication] registerForRemoteNotifications];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
Expand Down

0 comments on commit 4e9a0f4

Please sign in to comment.