Skip to content
Permalink
Browse files

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 #7937

Differential Revision: D3392476

Pulled By: javache

fbshipit-source-id: d83f419bfcfbdaf9b955724dcf5cfe26834895fb
  • Loading branch information
joshuapinter authored and Facebook Github Bot 3 committed Jun 6, 2016
1 parent 4de616b commit 5b4b02747792c77b51c64449bc18b9a0ba81f867
@@ -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.
@@ -57,6 +57,24 @@ + (UILocalNotification *)UILocalNotification:(id)json

@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 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
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

0 comments on commit 5b4b027

Please sign in to comment.
You can’t perform that action at this time.