diff --git a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.h b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.h index 15bdd59e86e132..214e0c81fc678c 100644 --- a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.h +++ b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.h @@ -11,8 +11,6 @@ extern NSString *const RCTRemoteNotificationReceived; @interface RCTPushNotificationManager : RCTEventEmitter -@property (nonatomic, nullable, readwrite) UNNotification *initialNotification; - typedef void (^RCTRemoteNotificationCallback)(UIBackgroundFetchResult result); + (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; @@ -41,6 +39,12 @@ typedef void (^RCTRemoteNotificationCallback)(UIBackgroundFetchResult result); + (void)didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(RCTRemoteNotificationCallback)completionHandler; +/** + * Call this in `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:` + * to get the correct value from .getInitialNotification in JS. + */ ++ (void)setInitialNotification:(UNNotification *)notification; + /** DEPRECATED. Use didReceiveNotification instead. */ + (void)didReceiveLocalNotification:(UILocalNotification *)notification RCT_DEPRECATED; /** DEPRECATED. Use didReceiveNotification instead. */ diff --git a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm index e366d196aad8bd..31b7dcb7a89296 100644 --- a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm +++ b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm @@ -23,6 +23,7 @@ static NSString *const kRemoteNotificationRegistrationFailed = @"RemoteNotificationRegistrationFailed"; static NSString *const kErrorUnableToRequestPermissions = @"E_UNABLE_TO_REQUEST_PERMISSIONS"; +static UNNotification *kInitialNotification = nil; @interface RCTPushNotificationManager () @property (nonatomic, strong) NSMutableDictionary *remoteNotificationCallbacks; @@ -261,6 +262,11 @@ + (void)didReceiveRemoteNotification:(NSDictionary *)notification userInfo:userInfo]; } ++ (void)setInitialNotification:(UNNotification *)notification +{ + kInitialNotification = notification; +} + // Deprecated + (void)didReceiveLocalNotification:(UILocalNotification *)notification { @@ -278,6 +284,18 @@ + (void)didReceiveRemoteNotification:(NSDictionary *)notification userInfo:userInfo]; } +- (void)invalidate +{ + [super invalidate]; + + kInitialNotification = nil; +} + +- (void)dealloc +{ + kInitialNotification = nil; +} + - (void)handleLocalNotificationReceived:(NSNotification *)notification { [self sendEventWithName:@"localNotificationReceived" body:notification.userInfo]; @@ -532,10 +550,10 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification // The user actioned a local or remote notification to launch the app. Notification is represented by UNNotification. // Set this property in the implementation of // userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler. - if (self.initialNotification) { + if (kInitialNotification) { NSDictionary *notificationDict = - RCTFormatUNNotificationContent(self.initialNotification.request.content); - if (IsNotificationRemote(self.initialNotification)) { + RCTFormatUNNotificationContent(kInitialNotification.request.content); + if (IsNotificationRemote(kInitialNotification)) { // For backwards compatibility, remote notifications only returns a userInfo dict. NSMutableDictionary *userInfoCopy = [notificationDict[@"userInfo"] mutableCopy]; userInfoCopy[@"remote"] = @YES;