Skip to content

Commit 70164b3

Browse files
authored
Provisional option in iOS registeration (#744)
Adding `Provisional` option to `ios.registerRemoteNotifications`. See apple [docs](https://developer.apple.com/documentation/usernotifications/asking_permission_to_use_notifications) for reference.
1 parent f69c985 commit 70164b3

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

example/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ class NotificationsExampleApp extends Component {
143143
return (
144144
<View style={styles.container}>
145145
<Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'} />
146-
{Platform.OS === 'ios' && <Button title={'Request permissions with app notification settings'} onPress={() => this.requestPermissionsIos(['ProvidesAppNotificationSettings'])} testID={'requestPermissionsWithAppSettings'} />}
146+
{Platform.OS === 'ios' && Platform.Version > '12.0' && (<>
147+
<Button title={'Request permissions with app notification settings'} onPress={() => this.requestPermissionsIos(['ProvidesAppNotificationSettings'])} testID={'requestPermissionsWithAppSettings'} />
148+
<Button title={'Request permissions with provisional'} onPress={() => this.requestPermissionsIos(['Provisional'])} testID={'requestPermissionsWithAppSettings'} />
149+
<Button title={'Request permissions with app notification settings and provisional'} onPress={() => this.requestPermissionsIos(['ProvidesAppNotificationSettings', 'Provisional'])} testID={'requestPermissionsWithAppSettings'} />
150+
</>)}
147151
<Button title={'Send local notification'} onPress={this.sendLocalNotification} testID={'sendLocalNotification'} />
148152
<Button title={'Remove all delivered notifications'} onPress={this.removeAllDeliveredNotifications} />
149153
{notifications}

lib/ios/RNNotificationCenter.m

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ - (void)requestPermissions:(NSArray *)options {
88
if ([options count] > 0) {
99
for (NSString* option in options) {
1010
if ([option isEqualToString:@"ProvidesAppNotificationSettings"]) {
11-
authOptions = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionProvidesAppNotificationSettings);
11+
if (@available(iOS 12.0, *)) {
12+
authOptions = authOptions | UNAuthorizationOptionProvidesAppNotificationSettings;
13+
}
14+
}
15+
if ([option isEqualToString:@"Provisional"]) {
16+
if (@available(iOS 12.0, *)) {
17+
authOptions = authOptions | UNAuthorizationOptionProvisional;
18+
}
1219
}
1320
}
1421
}

lib/ios/RNNotificationsTests/Integration/RNCommandsHandlerIntegrationTest.m

+13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ - (void)testRequestPermissions_withProvidesAppNotificationSettings {
5151
[_notificationCenter verify];
5252
}
5353

54+
- (void)testRequestPermissions_withProvisional {
55+
UNAuthorizationOptions authOptions = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionProvisional);
56+
UNNotificationSettings* settings = [UNNotificationSettings new];
57+
[settings setValue:@(UNAuthorizationStatusAuthorized) forKey:@"authorizationStatus"];
58+
59+
[[_notificationCenter expect] requestAuthorizationWithOptions:authOptions completionHandler:[OCMArg invokeBlockWithArgs:@(YES), [NSNull null], nil]];
60+
[[_notificationCenter expect] getNotificationSettingsWithCompletionHandler:[OCMArg invokeBlockWithArgs:settings, nil]];
61+
[[(id)[UIApplication sharedApplication] expect] registerForRemoteNotifications];
62+
63+
[_uut requestPermissions:@[@"Provisional"]];
64+
[_notificationCenter verify];
65+
}
66+
5467
- (void)testRequestPermissions_userDeniedPermissions {
5568
UNAuthorizationOptions authOptions = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert);
5669
UNNotificationSettings* settings = [UNNotificationSettings new];

lib/src/adapters/NativeCommandsSender.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface NativeCommandsModule {
2828
finishHandlingBackgroundAction(notificationId: string, backgroundFetchResult: string): void;
2929
}
3030

31-
export type RequestPermissionsOptions = 'ProvidesAppNotificationSettings';
31+
export type RequestPermissionsOptions = 'ProvidesAppNotificationSettings' | 'Provisional';
3232

3333
export class NativeCommandsSender {
3434
private readonly nativeCommandsModule: NativeCommandsModule;

website/docs/api/ios-api.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ sidebar_label: iOS specific
55
---
66

77
## registerRemoteNotifications(options?: string[])
8-
Requests notification permissions from iOS, prompting the user's dialog box.
8+
Requests notification permissions from iOS, prompting the user's dialog box if needed.
9+
10+
Available options:
11+
- **`ProvidesAppNotificationSettings`** - An option indicating the iOS notification settings to display a button for in-app notification settings and to be [informed in the app on this event](ios-events.md#appNotificationSettingsLinked).
12+
- **`Provisional`** - Use provisional authorization to send notifications on a trial basis. Users can then evaluate the notifications and decide whether to authorize them.
13+
914

1015
```js
11-
Notifications.ios.registerRemoteNotifications(['ProvidesAppNotificationSettings']);
16+
Notifications.ios.registerRemoteNotifications([
17+
'ProvidesAppNotificationSettings',
18+
'Provisional',
19+
]);
1220
```
1321

1422
## checkPermissions()

0 commit comments

Comments
 (0)