From 358c1ec56a53090ea183a8684b5cdb4eca85cc52 Mon Sep 17 00:00:00 2001 From: Ryan Eberhardt Date: Thu, 24 Aug 2017 12:23:34 -0700 Subject: [PATCH] Add silent option to iOS localNotification Add an option to suppress notification sound. Allows for in-app notification mute --- README.md | 4 +++- RNNotifications/RNNotifications.m | 6 ++++++ index.ios.js | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 641a329..341dc07 100644 --- a/README.md +++ b/README.md @@ -327,6 +327,7 @@ let localNotification = NotificationsIOS.localNotification({ alertBody: "Local notificiation!", alertTitle: "Local Notification Title", soundName: "chime.aiff", + silent: false, category: "SOME_CATEGORY", userInfo: { } }); @@ -338,7 +339,8 @@ Notification object contains: - `alertBody`- The message displayed in the notification alert. - `alertTitle`- The title of the notification, displayed in the notifications center. - `alertAction`- The "action" displayed beneath an actionable notification on the lockscreen (e.g. "Slide to **open**"). Note that Apple no longer shows this in iOS 10. -- `soundName`- The sound played when the notification is fired (optional). +- `soundName`- The sound played when the notification is fired (optional -- will play default sound if unspecified). This must be the filename of a sound included in the application bundle; the sound must be 30 seconds or less and should be encoded with linear PCM or IMA4. +- `silent`- Whether the notification sound should be suppressed (optional). - `category`- The category of this notification, required for [interactive notifications](#interactive--actionable-notifications-ios-only) (optional). - `userInfo`- An optional object containing additional notification data. diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index 3c12a9d..d50a7ce 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -103,6 +103,9 @@ RCT_ENUM_CONVERTER(UIUserNotificationActionBehavior, (@{ notification.alertTitle = [RCTConvert NSString:details[@"alertTitle"]]; notification.alertAction = [RCTConvert NSString:details[@"alertAction"]]; notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName; + if ([RCTConvert BOOL:details[@"silent"]]) { + notification.soundName = nil; + } notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]] ?: @{}; notification.category = [RCTConvert NSString:details[@"category"]]; @@ -121,6 +124,9 @@ RCT_ENUM_CONVERTER(UIUserNotificationActionBehavior, (@{ content.sound = [RCTConvert NSString:details[@"soundName"]] ? [UNNotificationSound soundNamed:[RCTConvert NSString:details[@"soundName"]]] : [UNNotificationSound defaultSound]; + if ([RCTConvert BOOL:details[@"silent"]]) { + content.sound = nil; + } content.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]] ?: @{}; content.categoryIdentifier = [RCTConvert NSString:details[@"category"]]; diff --git a/index.ios.js b/index.ios.js index d5ac856..3450e99 100644 --- a/index.ios.js +++ b/index.ios.js @@ -190,6 +190,7 @@ export default class NotificationsIOS { * - `alertTitle` : The message title displayed in the notification. * - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view"; * - `soundName` : The sound played when the notification is fired (optional). + * - `silent` : If true, the notification sound will be suppressed (optional). * - `category` : The category of this notification, required for actionable notifications (optional). * - `userInfo` : An optional object containing additional notification data. * - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately. -- 2.26.2