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

fix(messaging,ios): keep original UNUserNotificationCenter delegate #3427

Merged
merged 9 commits into from
Apr 22, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface RNFBMessagingUNUserNotificationCenter : NSObject <UNUserNotificationCenterDelegate>

@property NSDictionary* initialNotification;
@property NSDictionary *initialNotification;
@property(nonatomic, nullable, weak) id <UNUserNotificationCenterDelegate> originalDelegate;

+ (_Nonnull instancetype)sharedInstance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#import "RNFBMessaging+UNUserNotificationCenter.h"

@implementation RNFBMessagingUNUserNotificationCenter
struct {
unsigned int willPresentNotification:1;
unsigned int didReceiveNotificationResponse:1;
unsigned int openSettingsForNotification:1;
} originalDelegateRespondsTo;

+ (instancetype)sharedInstance {
static dispatch_once_t once;
Expand All @@ -38,6 +43,12 @@ - (void)observe {
dispatch_once(&once, ^{
RNFBMessagingUNUserNotificationCenter *strongSelf = weakSelf;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
if (center.delegate != nil) {
_originalDelegate = center.delegate;
originalDelegateRespondsTo.openSettingsForNotification = (unsigned int) [_originalDelegate respondsToSelector:@selector(userNotificationCenter:openSettingsForNotification:)];
originalDelegateRespondsTo.willPresentNotification = (unsigned int) [_originalDelegate respondsToSelector:@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:)];
originalDelegateRespondsTo.didReceiveNotificationResponse = (unsigned int) [_originalDelegate respondsToSelector:@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:)];
}
center.delegate = strongSelf;
});
}
Expand All @@ -64,6 +75,10 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNot

// TODO in a later version allow customising completion options in JS code
completionHandler(UNNotificationPresentationOptionNone);
} else if (_originalDelegate != nil && originalDelegateRespondsTo.willPresentNotification) {
[_originalDelegate userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
} else {
completionHandler(UNNotificationPresentationOptionNone);
}
}

Expand All @@ -74,6 +89,16 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti
[[RNFBRCTEventEmitter shared] sendEventWithName:@"messaging_notification_opened" body:notificationDict];
_initialNotification = notificationDict;
completionHandler();
} else if (_originalDelegate != nil && originalDelegateRespondsTo.didReceiveNotificationResponse) {
[_originalDelegate userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
} else {
completionHandler();
}
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(nullable UNNotification *)notification {
if (_originalDelegate != nil && originalDelegateRespondsTo.openSettingsForNotification) {
[_originalDelegate userNotificationCenter:center openSettingsForNotification:notification];
}
}

Expand Down