Skip to content

Commit

Permalink
Add method to retrieve authorization status
Browse files Browse the repository at this point in the history
Summary:
Changelog:
[iOS][Added] - Adds an ability to retrieve the notifications authorization status from JavaScript side.

Differential Revision: D27426952

fbshipit-source-id: 84a1eae1ff8c8f9f7601c7479745002c21178850
  • Loading branch information
Lucas Santos authored and facebook-github-bot committed Mar 30, 2021
1 parent 24bfa46 commit b86e52a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export interface Spec extends TurboModule {
+getDeliveredNotifications: (
callback: (notification: Array<Notification>) => void,
) => void;
+getAuthorizationStatus: (
callback: (authorizationStatus: number) => void,
) => void;
+addListener: (eventType: string) => void;
+removeListeners: (count: number) => void;
}
Expand Down
14 changes: 14 additions & 0 deletions Libraries/PushNotificationIOS/PushNotificationIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,20 @@ class PushNotificationIOS {
);
}

/**
* This method returns a promise that resolves to notification authorization status.
*/
static getAuthorizationStatus(
callback: (authorizationStatus: number) => void,
): void {
invariant(
NativePushNotificationManagerIOS,
'PushNotificationManager is not available.',
);

NativePushNotificationManagerIOS.getAuthorizationStatus(callback);
}

/**
* You will never need to instantiate `PushNotificationIOS` yourself.
* Listening to the `notification` event and invoking
Expand Down
13 changes: 13 additions & 0 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,14 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
}];
}

RCT_EXPORT_METHOD(getAuthorizationStatus:(RCTResponseSenderBlock)callback)
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *_Nonnull settings) {
callback(@[@(settings.authorizationStatus)]);
}];
}

#else // TARGET_OS_UIKITFORMAC

RCT_EXPORT_METHOD(onFinishRemoteNotification:(NSString *)notificationId fetchResult:(NSString *)fetchResult)
Expand Down Expand Up @@ -551,6 +559,11 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
RCTLogError(@"Not implemented: %@", NSStringFromSelector(_cmd));
}

RCT_EXPORT_METHOD(getAuthorizationStatus:(RCTResponseSenderBlock)callback)
{
RCTLogError(@"Not implemented: %@", NSStringFromSelector(_cmd));
}

- (NSArray<NSString *> *)supportedEvents
{
return @[];
Expand Down

0 comments on commit b86e52a

Please sign in to comment.