Skip to content

Commit bc3b112

Browse files
authored
Add support for critical notifications (#277)
1 parent 080f9b6 commit bc3b112

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ request is an object containing:
324324
- `sound` : The sound played when the notification is fired.
325325
- `category` : The category of this notification, required for actionable notifications.
326326
- `isSilent` : If true, the notification will appear without sound.
327+
- `isCritical` : If true, the notification sound be played even when the device is locked, muted, or has Do Not Disturb enabled.
328+
- `criticalSoundVolume` : A number between 0 and 1 for volume of critical notification. Default volume will be used if not specified.
327329
- `userInfo` : An object containing additional notification data.
328330

329331
---
@@ -556,16 +558,19 @@ Requests notification permissions from iOS, prompting the user's dialog box. By
556558
- `alert`
557559
- `badge`
558560
- `sound`
561+
- `critical`
562+
563+
`critical` requires special entitlement that could be requested here: https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/
559564

560565
If a map is provided to the method, only the permissions with truthy values will be requested.
561566

562567
This method returns a promise that will resolve when the user accepts, rejects, or if the permissions were previously rejected. The promise resolves to the current state of the permission.
563568

564569
**Parameters:**
565570

566-
| Name | Type | Required | Description |
567-
| ----------- | ----- | -------- | ---------------------- |
568-
| permissions | array | No | alert, badge, or sound |
571+
| Name | Type | Required | Description |
572+
| ----------- | ----- | -------- | ------------------------------- |
573+
| permissions | array | No | alert, badge, sound or critical |
569574

570575
---
571576

@@ -600,6 +605,7 @@ See what push permissions are currently enabled.
600605
- `alert` :boolean
601606
- `badge` :boolean
602607
- `sound` :boolean
608+
- `critical` :boolean
603609
- `lockScreen` :boolean
604610
- `notificationCenter` :boolean
605611
- `authorizationStatus` :AuthorizationStatus

index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ export type NotificationRequest = {
178178
* Sets notification to be silent
179179
*/
180180
isSilent?: boolean;
181+
/**
182+
* Sets notification to be critical
183+
*/
184+
isCritical?: boolean;
185+
/**
186+
* The volume for the critical alert’s sound. Set this to a value between 0.0 (silent) and 1.0 (full volume).
187+
*/
188+
criticalSoundVolume?: number;
181189
/**
182190
* Optional data to be added to the notification
183191
*/
@@ -291,6 +299,7 @@ export interface PushNotificationPermissions {
291299
alert?: boolean;
292300
badge?: boolean;
293301
sound?: boolean;
302+
critical?: boolean;
294303
lockScreen?: boolean;
295304
notificationCenter?: boolean;
296305
authorizationStatus?: AuthorizationStatus[keyof AuthorizationStatus];

ios/RCTConvert+Notification.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ + (UNNotificationRequest *)UNNotificationRequest:(id)json
9595
NSDictionary<NSString *, id> *details = [self NSDictionary:json];
9696

9797
BOOL isSilent = [RCTConvert BOOL:details[@"isSilent"]];
98+
BOOL isCritical = [RCTConvert BOOL:details[@"isCritical"]];
99+
float criticalSoundVolume = [RCTConvert float:details[@"criticalSoundVolume"]];
98100
NSString* identifier = [RCTConvert NSString:details[@"id"]];
99101

100102

@@ -112,7 +114,15 @@ + (UNNotificationRequest *)UNNotificationRequest:(id)json
112114

113115
content.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]];
114116
if (!isSilent) {
115-
content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound soundNamed:[RCTConvert NSString:details[@"sound"]]] : [UNNotificationSound defaultSound];
117+
if (isCritical) {
118+
if (criticalSoundVolume) {
119+
content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound criticalSoundNamed:[RCTConvert NSString:details[@"sound"]] withAudioVolume:criticalSoundVolume] : [UNNotificationSound defaultCriticalSoundWithAudioVolume:criticalSoundVolume];
120+
} else {
121+
content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound criticalSoundNamed:[RCTConvert NSString:details[@"sound"]]] : [UNNotificationSound defaultCriticalSound];
122+
}
123+
} else {
124+
content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound soundNamed:[RCTConvert NSString:details[@"sound"]]] : [UNNotificationSound defaultSound];
125+
}
116126
}
117127

118128
NSDate* fireDate = [RCTConvert NSDate:details[@"fireDate"]];

ios/RNCPushNotificationIOS.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
212212
if ([RCTConvert BOOL:permissions[@"sound"]]) {
213213
types |= UNAuthorizationOptionSound;
214214
}
215+
if ([RCTConvert BOOL:permissions[@"critical"]]) {
216+
types |= UNAuthorizationOptionCriticalAlert;
217+
}
215218
} else {
216219
types = UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound;
217220
}
@@ -241,7 +244,7 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
241244
RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback)
242245
{
243246
if (RCTRunningInAppExtension()) {
244-
callback(@[RCTSettingsDictForUNNotificationSettings(NO, NO, NO, NO, NO, UNAuthorizationStatusNotDetermined)]);
247+
callback(@[RCTSettingsDictForUNNotificationSettings(NO, NO, NO, NO, NO, NO, UNAuthorizationStatusNotDetermined)]);
245248
return;
246249
}
247250

@@ -254,13 +257,14 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
254257
return RCTSettingsDictForUNNotificationSettings(settings.alertSetting == UNNotificationSettingEnabled,
255258
settings.badgeSetting == UNNotificationSettingEnabled,
256259
settings.soundSetting == UNNotificationSettingEnabled,
260+
settings.criticalAlertSetting == UNNotificationSettingEnabled,
257261
settings.lockScreenSetting == UNNotificationSettingEnabled,
258262
settings.notificationCenterSetting == UNNotificationSettingEnabled,
259263
settings.authorizationStatus);
260264
}
261265

262-
static inline NSDictionary *RCTSettingsDictForUNNotificationSettings(BOOL alert, BOOL badge, BOOL sound, BOOL lockScreen, BOOL notificationCenter, UNAuthorizationStatus authorizationStatus) {
263-
return @{@"alert": @(alert), @"badge": @(badge), @"sound": @(sound), @"lockScreen": @(lockScreen), @"notificationCenter": @(notificationCenter), @"authorizationStatus": @(authorizationStatus)};
266+
static inline NSDictionary *RCTSettingsDictForUNNotificationSettings(BOOL alert, BOOL badge, BOOL sound, BOOL critical, BOOL lockScreen, BOOL notificationCenter, UNAuthorizationStatus authorizationStatus) {
267+
return @{@"alert": @(alert), @"badge": @(badge), @"sound": @(sound), @"critical": @(critical), @"lockScreen": @(lockScreen), @"notificationCenter": @(notificationCenter), @"authorizationStatus": @(authorizationStatus)};
264268
}
265269

266270
/**

js/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,12 @@ class PushNotificationIOS {
379379
alert?: boolean,
380380
badge?: boolean,
381381
sound?: boolean,
382+
critical?: boolean,
382383
}): Promise<{
383384
alert: boolean,
384385
badge: boolean,
385386
sound: boolean,
387+
critical: boolean,
386388
}> {
387389
let requestedPermissions = {
388390
alert: true,
@@ -394,6 +396,7 @@ class PushNotificationIOS {
394396
alert: !!permissions.alert,
395397
badge: !!permissions.badge,
396398
sound: !!permissions.sound,
399+
critical: !!permissions.critical,
397400
};
398401
}
399402
invariant(

0 commit comments

Comments
 (0)