From daed538c3c60cb5927585ef6393ef93db6e86fd8 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Tue, 13 Feb 2024 19:36:51 -0800 Subject: [PATCH] store initial notification in static instead of ivar Summary: Changelog: [Internal] I'm updating this API to match the semantics of the other new APIs, namely that they're static methods that don't depend on the instance of the native module. This should help streamline our documentation. Instead of capturing the launch notification as an ivar, we capture it in a static variable that we clear out when the object gets cleared or is invalidated, which is similar to an ivar. Differential Revision: D53743761 --- .../RCTPushNotificationManager.h | 8 +++++-- .../RCTPushNotificationManager.mm | 24 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) 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;