From b86e52a9ec9ec828388eb4a717a3782a54c7b3d9 Mon Sep 17 00:00:00 2001 From: Lucas Santos Date: Tue, 30 Mar 2021 16:04:56 -0700 Subject: [PATCH] Add method to retrieve authorization status Summary: Changelog: [iOS][Added] - Adds an ability to retrieve the notifications authorization status from JavaScript side. Differential Revision: D27426952 fbshipit-source-id: 84a1eae1ff8c8f9f7601c7479745002c21178850 --- .../NativePushNotificationManagerIOS.js | 3 +++ .../PushNotificationIOS/PushNotificationIOS.js | 14 ++++++++++++++ .../RCTPushNotificationManager.mm | 13 +++++++++++++ 3 files changed, 30 insertions(+) diff --git a/Libraries/PushNotificationIOS/NativePushNotificationManagerIOS.js b/Libraries/PushNotificationIOS/NativePushNotificationManagerIOS.js index 1eec7bde81481b..84d59b030a9df1 100644 --- a/Libraries/PushNotificationIOS/NativePushNotificationManagerIOS.js +++ b/Libraries/PushNotificationIOS/NativePushNotificationManagerIOS.js @@ -65,6 +65,9 @@ export interface Spec extends TurboModule { +getDeliveredNotifications: ( callback: (notification: Array) => void, ) => void; + +getAuthorizationStatus: ( + callback: (authorizationStatus: number) => void, + ) => void; +addListener: (eventType: string) => void; +removeListeners: (count: number) => void; } diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index c8c8e535ea444f..efe9b6132e716d 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -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 diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm b/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm index aa075bb6bfa1e6..3924fd64e2387b 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm @@ -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) @@ -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 *)supportedEvents { return @[];