Add get scheduled local notifications

Summary:
_(This is a remake of #6907, which I botched pretty good with a rebase.)_

This returns an `Array` of Local Notifications that have been scheduled to be delivered.

Available attributes on return Notification object (if set in the local notification itself):

`alertAction`
`alertBody`
`applicationIconBadgeNumber`
`category`
`fireDate`
`soundName`
`userInfo`

More could be added to this but I just matched what was available in the `Object` passed to `presentLocalNotification` and `scheduleLocalNotification`.

**Motivation**

I needed to determine the number and other details about local notifications that were already scheduled. There is an API for this in iOS but one hadn't been built yet for React Native.

I created the Obj-C method and updated the documentation in `PushNotificationIOS.js` as well.

**Usage:**

```js
PushNotificationIOS.getScheduledLocalNotifications( (notifications) => {
  console.log(notifications);
});
```

**Sample Output:**

```js
[
  Object {
    aler
Closes https://github.com/facebook/react-native/pull/7937

Differential Revision: D3392476

Pulled By: javache

fbshipit-source-id: d83f419bfcfbdaf9b955724dcf5cfe26834895fb
This commit is contained in:
Joshua Pinter 2016-06-06 09:18:59 -07:00 committed by Facebook Github Bot 3
parent 4de616b4c1
commit 5b4b027477
2 changed files with 35 additions and 0 deletions

View File

@ -142,6 +142,13 @@ class PushNotificationIOS {
RCTPushNotificationManager.cancelLocalNotifications(userInfo);
}
/**
* Gets the local notifications that are currently scheduled.
*/
static getScheduledLocalNotifications(callback: Function) {
RCTPushNotificationManager.getScheduledLocalNotifications(callback);
}
/**
* Attaches a listener to remote or local notification events while the app is running
* in the foreground or the background.

View File

@ -57,6 +57,24 @@ NSString *const RCTErrorUnableToRequestPermissions = @"E_UNABLE_TO_REQUEST_PERMI
@implementation RCTPushNotificationManager
static NSDictionary *formatLocalNotification(UILocalNotification *notification)
{
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
if (notification.fireDate) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
NSString *fireDateString = [formatter stringFromDate:notification.fireDate];
formattedLocalNotification[@"fireDate"] = fireDateString;
}
formattedLocalNotification[@"alertAction"] = RCTNullIfNil(notification.alertAction);
formattedLocalNotification[@"alertBody"] = RCTNullIfNil(notification.alertBody);
formattedLocalNotification[@"applicationIconBadgeNumber"] = @(notification.applicationIconBadgeNumber);
formattedLocalNotification[@"category"] = RCTNullIfNil(notification.category);
formattedLocalNotification[@"soundName"] = RCTNullIfNil(notification.soundName);
formattedLocalNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(notification.userInfo));
return formattedLocalNotification;
}
RCT_EXPORT_MODULE()
- (void)startObserving
@ -305,4 +323,14 @@ RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve
resolve(RCTNullIfNil(initialNotification));
}
RCT_EXPORT_METHOD(getScheduledLocalNotifications:(RCTResponseSenderBlock)callback)
{
NSArray<UILocalNotification *> *scheduledLocalNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
NSMutableArray *formattedScheduledLocalNotifications = [[NSMutableArray alloc] init];
for (UILocalNotification *notification in scheduledLocalNotifications) {
[formattedScheduledLocalNotifications addObject:formatLocalNotification(notification)];
}
callback(@[formattedScheduledLocalNotifications]);
}
@end