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
This commit is contained in:
Alex Ledak 2017-05-28 15:26:01 -07:00 коммит произвёл Facebook Github Bot
Родитель daf3bd1b88
Коммит 9fae268e5b
2 изменённых файлов: 6 добавлений и 1 удалений

Просмотреть файл

@ -140,6 +140,7 @@ class PushNotificationIOS {
* - `alertBody` : The message displayed in the notification alert. * - `alertBody` : The message displayed in the notification alert.
* - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view"; * - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view";
* - `soundName` : The sound played when the notification is fired (optional). * - `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). * - `category` : The category of this notification, required for actionable notifications (optional).
* - `userInfo` : An optional object containing additional notification data. * - `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. * - `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. * - `alertBody` : The message displayed in the notification alert.
* - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view"; * - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view";
* - `soundName` : The sound played when the notification is fired (optional). * - `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). * - `category` : The category of this notification, required for actionable notifications (optional).
* - `userInfo` : An optional object containing additional notification data. * - `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. * - `applicationIconBadgeNumber` (optional) : The number to display as the app's icon badge. Setting the number to 0 removes the icon badge.

Просмотреть файл

@ -50,17 +50,20 @@ RCT_ENUM_CONVERTER(NSCalendarUnit,
+ (UILocalNotification *)UILocalNotification:(id)json + (UILocalNotification *)UILocalNotification:(id)json
{ {
NSDictionary<NSString *, id> *details = [self NSDictionary:json]; NSDictionary<NSString *, id> *details = [self NSDictionary:json];
BOOL isSilent = [RCTConvert BOOL:details[@"isSilent"]];
UILocalNotification *notification = [UILocalNotification new]; UILocalNotification *notification = [UILocalNotification new];
notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]] ?: [NSDate date]; notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]] ?: [NSDate date];
notification.alertBody = [RCTConvert NSString:details[@"alertBody"]]; notification.alertBody = [RCTConvert NSString:details[@"alertBody"]];
notification.alertAction = [RCTConvert NSString:details[@"alertAction"]]; notification.alertAction = [RCTConvert NSString:details[@"alertAction"]];
notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName;
notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]]; notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]];
notification.category = [RCTConvert NSString:details[@"category"]]; notification.category = [RCTConvert NSString:details[@"category"]];
notification.repeatInterval = [RCTConvert NSCalendarUnit:details[@"repeatInterval"]]; notification.repeatInterval = [RCTConvert NSCalendarUnit:details[@"repeatInterval"]];
if (details[@"applicationIconBadgeNumber"]) { if (details[@"applicationIconBadgeNumber"]) {
notification.applicationIconBadgeNumber = [RCTConvert NSInteger:details[@"applicationIconBadgeNumber"]]; notification.applicationIconBadgeNumber = [RCTConvert NSInteger:details[@"applicationIconBadgeNumber"]];
} }
if (!isSilent) {
notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName;
}
return notification; return notification;
} }