Skip to content

Commit 00469f3

Browse files
committed
feat: Add support for critical notifications
1 parent 9fa4ff7 commit 00469f3

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

README.md

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

321323
---
@@ -548,16 +550,19 @@ Requests notification permissions from iOS, prompting the user's dialog box. By
548550
- `alert`
549551
- `badge`
550552
- `sound`
553+
- `critical`
554+
555+
`critical` requires special entitlement that could be requested here: https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/
551556

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

554559
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.
555560

556561
**Parameters:**
557562

558-
| Name | Type | Required | Description |
559-
| ----------- | ----- | -------- | ---------------------- |
560-
| permissions | array | No | alert, badge, or sound |
563+
| Name | Type | Required | Description |
564+
| ----------- | ----- | -------- | ------------------------------- |
565+
| permissions | array | No | alert, badge, sound or critical |
561566

562567
---
563568

@@ -592,6 +597,7 @@ See what push permissions are currently enabled.
592597
- `alert` :boolean
593598
- `badge` :boolean
594599
- `sound` :boolean
600+
- `critical` :boolean
595601
- `lockScreen` :boolean
596602
- `notificationCenter` :boolean
597603
- `authorizationStatus` :AuthorizationStatus

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ export type NotificationRequest = {
178178
* Sets notification to be silent
179179
*/
180180
isSilent?: boolean;
181+
isCritical?: boolean;
182+
criticalSoundVolume: number;
181183
/**
182184
* Optional data to be added to the notification
183185
*/
@@ -291,6 +293,7 @@ export interface PushNotificationPermissions {
291293
alert?: boolean;
292294
badge?: boolean;
293295
sound?: boolean;
296+
critical?: boolean;
294297
lockScreen?: boolean;
295298
notificationCenter?: boolean;
296299
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)