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

Add support for critical notifications #277

Merged
merged 1 commit into from
Aug 8, 2021
Merged
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
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ request is an object containing:
- `sound` : The sound played when the notification is fired.
- `category` : The category of this notification, required for actionable notifications.
- `isSilent` : If true, the notification will appear without sound.
- `isCritical` : If true, the notification sound be played even when the device is locked, muted, or has Do Not Disturb enabled.
- `criticalSoundVolume` : A number between 0 and 1 for volume of critical notification. Default volume will be used if not specified.
- `userInfo` : An object containing additional notification data.

---
Expand Down Expand Up @@ -548,16 +550,19 @@ Requests notification permissions from iOS, prompting the user's dialog box. By
- `alert`
- `badge`
- `sound`
- `critical`

`critical` requires special entitlement that could be requested here: https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/

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

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.

**Parameters:**

| Name | Type | Required | Description |
| ----------- | ----- | -------- | ---------------------- |
| permissions | array | No | alert, badge, or sound |
| Name | Type | Required | Description |
| ----------- | ----- | -------- | ------------------------------- |
| permissions | array | No | alert, badge, sound or critical |

---

Expand Down Expand Up @@ -592,6 +597,7 @@ See what push permissions are currently enabled.
- `alert` :boolean
- `badge` :boolean
- `sound` :boolean
- `critical` :boolean
- `lockScreen` :boolean
- `notificationCenter` :boolean
- `authorizationStatus` :AuthorizationStatus
Expand Down
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ export type NotificationRequest = {
* Sets notification to be silent
*/
isSilent?: boolean;
/**
* Sets notification to be critical
*/
isCritical?: boolean;
/**
* The volume for the critical alert’s sound. Set this to a value between 0.0 (silent) and 1.0 (full volume).
*/
criticalSoundVolume?: number;
/**
* Optional data to be added to the notification
*/
Expand Down Expand Up @@ -291,6 +299,7 @@ export interface PushNotificationPermissions {
alert?: boolean;
badge?: boolean;
sound?: boolean;
critical?: boolean;
lockScreen?: boolean;
notificationCenter?: boolean;
authorizationStatus?: AuthorizationStatus[keyof AuthorizationStatus];
Expand Down
12 changes: 11 additions & 1 deletion ios/RCTConvert+Notification.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ + (UNNotificationRequest *)UNNotificationRequest:(id)json
NSDictionary<NSString *, id> *details = [self NSDictionary:json];

BOOL isSilent = [RCTConvert BOOL:details[@"isSilent"]];
BOOL isCritical = [RCTConvert BOOL:details[@"isCritical"]];
float criticalSoundVolume = [RCTConvert float:details[@"criticalSoundVolume"]];
NSString* identifier = [RCTConvert NSString:details[@"id"]];


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

content.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]];
if (!isSilent) {
content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound soundNamed:[RCTConvert NSString:details[@"sound"]]] : [UNNotificationSound defaultSound];
if (isCritical) {
if (criticalSoundVolume) {
content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound criticalSoundNamed:[RCTConvert NSString:details[@"sound"]] withAudioVolume:criticalSoundVolume] : [UNNotificationSound defaultCriticalSoundWithAudioVolume:criticalSoundVolume];
} else {
content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound criticalSoundNamed:[RCTConvert NSString:details[@"sound"]]] : [UNNotificationSound defaultCriticalSound];
}
} else {
content.sound = [RCTConvert NSString:details[@"sound"]] ? [UNNotificationSound soundNamed:[RCTConvert NSString:details[@"sound"]]] : [UNNotificationSound defaultSound];
}
}

NSDate* fireDate = [RCTConvert NSDate:details[@"fireDate"]];
Expand Down
10 changes: 7 additions & 3 deletions ios/RNCPushNotificationIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
if ([RCTConvert BOOL:permissions[@"sound"]]) {
types |= UNAuthorizationOptionSound;
}
if ([RCTConvert BOOL:permissions[@"critical"]]) {
types |= UNAuthorizationOptionCriticalAlert;
}
} else {
types = UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound;
}
Expand Down Expand Up @@ -241,7 +244,7 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback)
{
if (RCTRunningInAppExtension()) {
callback(@[RCTSettingsDictForUNNotificationSettings(NO, NO, NO, NO, NO, UNAuthorizationStatusNotDetermined)]);
callback(@[RCTSettingsDictForUNNotificationSettings(NO, NO, NO, NO, NO, NO, UNAuthorizationStatusNotDetermined)]);
return;
}

Expand All @@ -254,13 +257,14 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
return RCTSettingsDictForUNNotificationSettings(settings.alertSetting == UNNotificationSettingEnabled,
settings.badgeSetting == UNNotificationSettingEnabled,
settings.soundSetting == UNNotificationSettingEnabled,
settings.criticalAlertSetting == UNNotificationSettingEnabled,
settings.lockScreenSetting == UNNotificationSettingEnabled,
settings.notificationCenterSetting == UNNotificationSettingEnabled,
settings.authorizationStatus);
}

static inline NSDictionary *RCTSettingsDictForUNNotificationSettings(BOOL alert, BOOL badge, BOOL sound, BOOL lockScreen, BOOL notificationCenter, UNAuthorizationStatus authorizationStatus) {
return @{@"alert": @(alert), @"badge": @(badge), @"sound": @(sound), @"lockScreen": @(lockScreen), @"notificationCenter": @(notificationCenter), @"authorizationStatus": @(authorizationStatus)};
static inline NSDictionary *RCTSettingsDictForUNNotificationSettings(BOOL alert, BOOL badge, BOOL sound, BOOL critical, BOOL lockScreen, BOOL notificationCenter, UNAuthorizationStatus authorizationStatus) {
return @{@"alert": @(alert), @"badge": @(badge), @"sound": @(sound), @"critical": @(critical), @"lockScreen": @(lockScreen), @"notificationCenter": @(notificationCenter), @"authorizationStatus": @(authorizationStatus)};
}

/**
Expand Down
3 changes: 3 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,12 @@ class PushNotificationIOS {
alert?: boolean,
badge?: boolean,
sound?: boolean,
critical?: boolean,
}): Promise<{
alert: boolean,
badge: boolean,
sound: boolean,
critical: boolean,
}> {
let requestedPermissions = {
alert: true,
Expand All @@ -394,6 +396,7 @@ class PushNotificationIOS {
alert: !!permissions.alert,
badge: !!permissions.badge,
sound: !!permissions.sound,
critical: !!permissions.critical,
};
}
invariant(
Expand Down