Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

co-existence with cordova-plugin-local-notification on iOS #680

Merged
merged 2 commits into from
May 10, 2018
Merged
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
48 changes: 48 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,14 @@ - (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];

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

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

[mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
Expand All @@ -130,6 +155,29 @@ - (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];

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

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

[mutableUserInfo setValue:@YES forKey:@"tap"];

// Print full message.
NSLog(@"Response %@", mutableUserInfo);

[FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];

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