Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to request the device token without showing permission #397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,12 @@ export interface PushNotificationIOSStatic {
permissions?: PushNotificationPermissions[] | PushNotificationPermissions,
): Promise<PushNotificationPermissions>;

/**
* Requests the APNS token for the app. This is useful for integrating with
* other services, such as Amazon's Simple Notification Service.
*/
requestToken(): Promise<PushNotificationPermissions>;

/**
* Unregister for all remote notifications received via Apple Push
* Notification service.
Expand Down
20 changes: 20 additions & 0 deletions ios/RNCPushNotificationIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
21 changes: 21 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down