Skip to content

Commit b86e52a

Browse files
Lucas Santosfacebook-github-bot
Lucas Santos
authored andcommitted
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
1 parent 24bfa46 commit b86e52a

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Libraries/PushNotificationIOS/NativePushNotificationManagerIOS.js

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export interface Spec extends TurboModule {
6565
+getDeliveredNotifications: (
6666
callback: (notification: Array<Notification>) => void,
6767
) => void;
68+
+getAuthorizationStatus: (
69+
callback: (authorizationStatus: number) => void,
70+
) => void;
6871
+addListener: (eventType: string) => void;
6972
+removeListeners: (count: number) => void;
7073
}

Libraries/PushNotificationIOS/PushNotificationIOS.js

+14
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,20 @@ class PushNotificationIOS {
399399
);
400400
}
401401

402+
/**
403+
* This method returns a promise that resolves to notification authorization status.
404+
*/
405+
static getAuthorizationStatus(
406+
callback: (authorizationStatus: number) => void,
407+
): void {
408+
invariant(
409+
NativePushNotificationManagerIOS,
410+
'PushNotificationManager is not available.',
411+
);
412+
413+
NativePushNotificationManagerIOS.getAuthorizationStatus(callback);
414+
}
415+
402416
/**
403417
* You will never need to instantiate `PushNotificationIOS` yourself.
404418
* Listening to the `notification` event and invoking

Libraries/PushNotificationIOS/RCTPushNotificationManager.mm

+13
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,14 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
471471
}];
472472
}
473473

474+
RCT_EXPORT_METHOD(getAuthorizationStatus:(RCTResponseSenderBlock)callback)
475+
{
476+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
477+
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *_Nonnull settings) {
478+
callback(@[@(settings.authorizationStatus)]);
479+
}];
480+
}
481+
474482
#else // TARGET_OS_UIKITFORMAC
475483

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

562+
RCT_EXPORT_METHOD(getAuthorizationStatus:(RCTResponseSenderBlock)callback)
563+
{
564+
RCTLogError(@"Not implemented: %@", NSStringFromSelector(_cmd));
565+
}
566+
554567
- (NSArray<NSString *> *)supportedEvents
555568
{
556569
return @[];

0 commit comments

Comments
 (0)