From 74f574e8afa59fa198458728f4f57353d2404bcf Mon Sep 17 00:00:00 2001 From: Gary Tokman Date: Mon, 20 Mar 2023 09:26:39 -0400 Subject: [PATCH] feat: add ability to request the device token without showing permission --- index.d.ts | 6 ++++++ ios/RNCPushNotificationIOS.m | 20 ++++++++++++++++++++ js/index.js | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/index.d.ts b/index.d.ts index 06bdd536..f7b08fdd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -497,6 +497,12 @@ export interface PushNotificationIOSStatic { permissions?: PushNotificationPermissions[] | PushNotificationPermissions, ): Promise; + /** + * Requests the APNS token for the app. This is useful for integrating with + * other services, such as Amazon's Simple Notification Service. + */ + requestToken(): Promise; + /** * Unregister for all remote notifications received via Apple Push * Notification service. diff --git a/ios/RNCPushNotificationIOS.m b/ios/RNCPushNotificationIOS.m index bc6bb3d3..45869b43 100644 --- a/ios/RNCPushNotificationIOS.m +++ b/ios/RNCPushNotificationIOS.m @@ -243,6 +243,26 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification }]; } +RCT_EXPORT_METHOD(requestToken:(NSDictionary *)permissions + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) +{ + if (RCTRunningInAppExtension()) { + reject(kErrorUnableToRequestPermissions, nil, RCTErrorWithMessage(@"Requesting push notifications is currently unavailable in an app extension")); + return; + } + + // Add a listener to make sure that startObserving has been called + [self addListener:@"remoteNotificationsRegistered"]; + + dispatch_async(dispatch_get_main_queue(), ^(void){ + [RCTSharedApplication() registerForRemoteNotifications]; + }); + [UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { + resolve(RCTPromiseResolveValueForUNNotificationSettings(settings)); + }]; +} + RCT_EXPORT_METHOD(abandonPermissions) { [RCTSharedApplication() unregisterForRemoteNotifications]; diff --git a/js/index.js b/js/index.js index b2cd9502..0d9315d4 100644 --- a/js/index.js +++ b/js/index.js @@ -410,6 +410,27 @@ class PushNotificationIOS { return RNCPushNotificationIOS.requestPermissions(requestedPermissions); } + /** + * Requests notification permissions from iOS, prompting the user's + * dialog box. By default, it will request all notification permissions, but + * a subset of these can be requested by passing a map of requested + * permissions. + * + * See https://reactnative.dev/docs/pushnotificationios.html#requestpermissions + */ + static requestToken(): Promise<{ + alert: boolean, + badge: boolean, + sound: boolean, + critical: boolean, + }> { + invariant( + RNCPushNotificationIOS, + 'PushNotificationManager is not available.', + ); + return RNCPushNotificationIOS.requestToken({}); + } + /** * Unregister for all remote notifications received via Apple Push Notification service. *