From 5b4b02747792c77b51c64449bc18b9a0ba81f867 Mon Sep 17 00:00:00 2001 From: Joshua Pinter Date: Mon, 6 Jun 2016 09:18:59 -0700 Subject: [PATCH] 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 --- .../PushNotificationIOS.js | 7 +++++ .../RCTPushNotificationManager.m | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 0fd8260625..4c89295ed7 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -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. diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index 329f7644d3..98f991825a 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -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 *scheduledLocalNotifications = [UIApplication sharedApplication].scheduledLocalNotifications; + NSMutableArray *formattedScheduledLocalNotifications = [[NSMutableArray alloc] init]; + for (UILocalNotification *notification in scheduledLocalNotifications) { + [formattedScheduledLocalNotifications addObject:formatLocalNotification(notification)]; + } + callback(@[formattedScheduledLocalNotifications]); +} + @end