Add observers for local notifications

Summary:
This commit adds the delegate hooks so that local notifications get
passed onto the JS and adds a new event listener type for local
notifications.

Also add functions to clear local notifications
Closes https://github.com/facebook/react-native/pull/2084

Reviewed By: svcscm

Differential Revision: D2908096

Pulled By: nicklockwood

fb-gh-sync-id: 759d299ea35abea177e72934076297d666d3ea20
shipit-source-id: 759d299ea35abea177e72934076297d666d3ea20
This commit is contained in:
slycoder
2016-02-09 05:45:23 -08:00
committed by facebook-github-bot-7
parent c6366e4dd8
commit 758d9e8304
4 changed files with 89 additions and 27 deletions

View File

@@ -24,6 +24,7 @@
#endif
NSString *const RCTLocalNotificationReceived = @"LocalNotificationReceived";
NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered";
@@ -36,6 +37,7 @@ NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegister
notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]] ?: [NSDate date];
notification.alertBody = [RCTConvert NSString:details[@"alertBody"]];
notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName;
notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]];
return notification;
}
@@ -56,6 +58,10 @@ RCT_EXPORT_MODULE()
{
_bridge = bridge;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleLocalNotificationReceived:)
name:RCTLocalNotificationReceived
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationReceived:)
name:RCTRemoteNotificationReceived
@@ -68,7 +74,8 @@ RCT_EXPORT_MODULE()
- (NSDictionary<NSString *, id> *)constantsToExport
{
NSDictionary<NSString *, id> *initialNotification = [_bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] copy];
NSDictionary<NSString *, id> *initialNotification =
[_bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] copy];
return @{@"initialNotification": RCTNullIfNil(initialNotification)};
}
@@ -87,12 +94,9 @@ RCT_EXPORT_MODULE()
for (NSUInteger i = 0; i < deviceTokenLength; i++) {
[hexString appendFormat:@"%02x", bytes[i]];
}
NSDictionary<NSString *, id> *userInfo = @{
@"deviceToken" : [hexString copy]
};
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationsRegistered
object:self
userInfo:userInfo];
userInfo:@{@"deviceToken" : [hexString copy]}];
}
+ (void)didReceiveRemoteNotification:(NSDictionary *)notification
@@ -102,6 +106,26 @@ RCT_EXPORT_MODULE()
userInfo:notification];
}
+ (void)didReceiveLocalNotification:(UILocalNotification *)notification
{
NSMutableDictionary *details = [NSMutableDictionary new];
if (notification.alertBody) {
details[@"alertBody"] = notification.alertBody;
}
if (notification.userInfo) {
details[@"userInfo"] = RCTJSONClean(notification.userInfo);
}
[[NSNotificationCenter defaultCenter] postNotificationName:RCTLocalNotificationReceived
object:self
userInfo:details];
}
- (void)handleLocalNotificationReceived:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"localNotificationReceived"
body:notification.userInfo];
}
- (void)handleRemoteNotificationReceived:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceived"
@@ -127,9 +151,7 @@ RCT_EXPORT_METHOD(setApplicationIconBadgeNumber:(NSInteger)number)
*/
RCT_EXPORT_METHOD(getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback)
{
callback(@[
@(RCTSharedApplication().applicationIconBadgeNumber)
]);
callback(@[@(RCTSharedApplication().applicationIconBadgeNumber)]);
}
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions)
@@ -140,13 +162,13 @@ RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions)
UIUserNotificationType types = UIUserNotificationTypeNone;
if (permissions) {
if ([permissions[@"alert"] boolValue]) {
if ([RCTConvert BOOL:permissions[@"alert"]]) {
types |= UIUserNotificationTypeAlert;
}
if ([permissions[@"badge"] boolValue]) {
if ([RCTConvert BOOL:permissions[@"badge"]]) {
types |= UIUserNotificationTypeBadge;
}
if ([permissions[@"sound"] boolValue]) {
if ([RCTConvert BOOL:permissions[@"sound"]]) {
types |= UIUserNotificationTypeSound;
}
} else {
@@ -155,7 +177,8 @@ RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions)
UIApplication *app = RCTSharedApplication();
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:(NSUInteger)types categories:nil];
UIUserNotificationSettings *notificationSettings =
[UIUserNotificationSettings settingsForTypes:(NSUInteger)types categories:nil];
[app registerUserNotificationSettings:notificationSettings];
[app registerForRemoteNotifications];
} else {
@@ -171,8 +194,7 @@ RCT_EXPORT_METHOD(abandonPermissions)
RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback)
{
if (RCTRunningInAppExtension()) {
NSDictionary<NSString *, NSNumber *> *permissions = @{@"alert": @NO, @"badge": @NO, @"sound": @NO};
callback(@[permissions]);
callback(@[@{@"alert": @NO, @"badge": @NO, @"sound": @NO}]);
return;
}
@@ -189,12 +211,11 @@ RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback)
}
NSMutableDictionary<NSString *, NSNumber *> *permissions = [NSMutableDictionary new];
permissions[@"alert"] = @((types & UIUserNotificationTypeAlert) > 0);
permissions[@"badge"] = @((types & UIUserNotificationTypeBadge) > 0);
permissions[@"sound"] = @((types & UIUserNotificationTypeSound) > 0);
callback(@[permissions]);
callback(@[@{
@"alert": @((types & UIUserNotificationTypeAlert) > 0),
@"badge": @((types & UIUserNotificationTypeBadge) > 0),
@"sound": @((types & UIUserNotificationTypeSound) > 0),
}]);
}
RCT_EXPORT_METHOD(presentLocalNotification:(UILocalNotification *)notification)
@@ -212,4 +233,21 @@ RCT_EXPORT_METHOD(cancelAllLocalNotifications)
[RCTSharedApplication() cancelAllLocalNotifications];
}
RCT_EXPORT_METHOD(cancelLocalNotifications:(NSDictionary *)userInfo)
{
for (UILocalNotification *notification in [UIApplication sharedApplication].scheduledLocalNotifications) {
__block BOOL matchesAll = YES;
NSDictionary *notificationInfo = notification.userInfo;
[userInfo enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
if (![notificationInfo[key] isEqual:obj]) {
matchesAll = NO;
*stop = YES;
}
}];
if (matchesAll) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
@end