Skip to content

Commit

Permalink
store initial notification in static instead of ivar
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philIip authored and facebook-github-bot committed Feb 14, 2024
1 parent 50d55f1 commit daed538
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
static NSString *const kRemoteNotificationRegistrationFailed = @"RemoteNotificationRegistrationFailed";

static NSString *const kErrorUnableToRequestPermissions = @"E_UNABLE_TO_REQUEST_PERMISSIONS";
static UNNotification *kInitialNotification = nil;

@interface RCTPushNotificationManager () <NativePushNotificationManagerIOSSpec>
@property (nonatomic, strong) NSMutableDictionary *remoteNotificationCallbacks;
Expand Down Expand Up @@ -261,6 +262,11 @@ + (void)didReceiveRemoteNotification:(NSDictionary *)notification
userInfo:userInfo];
}

+ (void)setInitialNotification:(UNNotification *)notification
{
kInitialNotification = notification;
}

// Deprecated
+ (void)didReceiveLocalNotification:(UILocalNotification *)notification
{
Expand All @@ -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];
Expand Down Expand Up @@ -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<NSString *, id> *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<NSString *, id> *userInfoCopy = [notificationDict[@"userInfo"] mutableCopy];
userInfoCopy[@"remote"] = @YES;
Expand Down

0 comments on commit daed538

Please sign in to comment.