From 9fae268e5b47a60734db9372d5b726fb2abd5470 Mon Sep 17 00:00:00 2001 From: Alex Ledak Date: Sun, 28 May 2017 15:26:01 -0700 Subject: [PATCH] Play sound in local notification only if soundName is specified Summary: Currently it's not possible to create a silent local notification (which is useful for badge updates) with PushNotificationIOS. This commit solves the problem - when **soundName** param is not specified in **PushNotificationIOS.scheduleLocalNotification** method, a silent notification will be created. Local notification without sound: ``` let fireDate = new Date(); fireDate = fireDate.setSeconds(fireDate.getSeconds() + 15); //fire in 15 seconds PushNotificationIOS.scheduleLocalNotification({ fireDate: fireDate, alertBody: "I'm silent!" }); ``` Local notification with sound: ``` let fireDate = new Date(); fireDate = fireDate.setSeconds(fireDate.getSeconds() + 15); //fire in 15 seconds PushNotificationIOS.scheduleLocalNotification({ fireDate: fireDate, alertBody: "I'm with sound!", soundName: "Any sound" }); ``` Closes https://github.com/facebook/react-native/pull/13734 Differential Revision: D5144848 Pulled By: shergin fbshipit-source-id: dc990b2673305a01cfd868fcdedcf27c461d0a34 --- Libraries/PushNotificationIOS/PushNotificationIOS.js | 2 ++ Libraries/PushNotificationIOS/RCTPushNotificationManager.m | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 83de728b5c..6a511d5f51 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -140,6 +140,7 @@ class PushNotificationIOS { * - `alertBody` : The message displayed in the notification alert. * - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view"; * - `soundName` : The sound played when the notification is fired (optional). + * - `isSilent` : If true, the notification will appear without sound (optional). * - `category` : The category of this notification, required for actionable notifications (optional). * - `userInfo` : An optional object containing additional notification data. * - `applicationIconBadgeNumber` (optional) : The number to display as the app's icon badge. The default value of this property is 0, which means that no badge is displayed. @@ -157,6 +158,7 @@ class PushNotificationIOS { * - `alertBody` : The message displayed in the notification alert. * - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view"; * - `soundName` : The sound played when the notification is fired (optional). + * - `isSilent` : If true, the notification will appear without sound (optional). * - `category` : The category of this notification, required for actionable notifications (optional). * - `userInfo` : An optional object containing additional notification data. * - `applicationIconBadgeNumber` (optional) : The number to display as the app's icon badge. Setting the number to 0 removes the icon badge. diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index ec7a487e19..cdb8bd5802 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -50,17 +50,20 @@ RCT_ENUM_CONVERTER(NSCalendarUnit, + (UILocalNotification *)UILocalNotification:(id)json { NSDictionary *details = [self NSDictionary:json]; + BOOL isSilent = [RCTConvert BOOL:details[@"isSilent"]]; UILocalNotification *notification = [UILocalNotification new]; notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]] ?: [NSDate date]; notification.alertBody = [RCTConvert NSString:details[@"alertBody"]]; notification.alertAction = [RCTConvert NSString:details[@"alertAction"]]; - notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName; notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]]; notification.category = [RCTConvert NSString:details[@"category"]]; notification.repeatInterval = [RCTConvert NSCalendarUnit:details[@"repeatInterval"]]; if (details[@"applicationIconBadgeNumber"]) { notification.applicationIconBadgeNumber = [RCTConvert NSInteger:details[@"applicationIconBadgeNumber"]]; } + if (!isSilent) { + notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName; + } return notification; }