Skip to content

Commit

Permalink
Add observers for local notifications
Browse files Browse the repository at this point in the history
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 #2084

Reviewed By: svcscm

Differential Revision: D2908096

Pulled By: nicklockwood

fb-gh-sync-id: 759d299ea35abea177e72934076297d666d3ea20
shipit-source-id: 759d299ea35abea177e72934076297d666d3ea20
  • Loading branch information
slycoder authored and facebook-github-bot-7 committed Feb 9, 2016
1 parent c6366e4 commit 758d9e8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 27 deletions.
35 changes: 29 additions & 6 deletions Libraries/PushNotificationIOS/PushNotificationIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var _initialNotification = RCTPushNotificationManager &&

var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
var DEVICE_LOCAL_NOTIF_EVENT = 'localNotificationReceived';

/**
* Handle push notifications for your app, including permission handling and
Expand Down Expand Up @@ -59,6 +60,11 @@ var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
* {
* [RCTPushNotificationManager didReceiveRemoteNotification:notification];
* }
* // Required for the localNotification event.
* - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
* {
* [RCTPushNotificationManager didReceiveLocalNotification:notification];
* }
* ```
*/
class PushNotificationIOS {
Expand All @@ -74,7 +80,6 @@ class PushNotificationIOS {
*
* - `alertBody` : The message displayed in the notification alert.
* - `soundName` : The sound played when the notification is fired (optional).
*
*/
static presentLocalNotification(details: Object) {
RCTPushNotificationManager.presentLocalNotification(details);
Expand All @@ -88,7 +93,7 @@ class PushNotificationIOS {
* - `fireDate` : The date and time when the system should deliver the notification.
* - `alertBody` : The message displayed in the notification alert.
* - `soundName` : The sound played when the notification is fired (optional).
*
* - `userInfo` : An optional object containing additional notification data.
*/
static scheduleLocalNotification(details: Object) {
RCTPushNotificationManager.scheduleLocalNotification(details);
Expand All @@ -115,6 +120,17 @@ class PushNotificationIOS {
RCTPushNotificationManager.getApplicationIconBadgeNumber(callback);
}

/**
* Cancel local notifications.
*
* Optionally restricts the set of canceled notifications to those
* notifications whose `userInfo` fields match the corresponding fields
* in the `userInfo` argument.
*/
static cancelLocalNotifications(userInfo: Object) {
RCTPushNotificationManager.cancelLocalNotifications(userInfo);
}

/**
* Attaches a listener to remote notification events while the app is running
* in the foreground or the background.
Expand All @@ -128,8 +144,8 @@ class PushNotificationIOS {
*/
static addEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
type === 'notification' || type === 'register' || type === 'localNotification',
'PushNotificationIOS only supports `notification`, `register` and `localNotification` events'
);
var listener;
if (type === 'notification') {
Expand All @@ -139,6 +155,13 @@ class PushNotificationIOS {
handler(new PushNotificationIOS(notifData));
}
);
} else if (type === 'localNotification') {
listener = RCTDeviceEventEmitter.addListener(
DEVICE_LOCAL_NOTIF_EVENT,
(notifData) => {
handler(new PushNotificationIOS(notifData));
}
);
} else if (type === 'register') {
listener = RCTDeviceEventEmitter.addListener(
NOTIF_REGISTER_EVENT,
Expand Down Expand Up @@ -220,8 +243,8 @@ class PushNotificationIOS {
*/
static removeEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
type === 'notification' || type === 'register' || type === 'localNotification',
'PushNotificationIOS only supports `notification`, `register` and `localNotification` events'
);
var listener = _notifHandlers.get(handler);
if (!listener) {
Expand Down
1 change: 1 addition & 0 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
+ (void)didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
+ (void)didReceiveRemoteNotification:(NSDictionary *)notification;
+ (void)didReceiveLocalNotification:(UILocalNotification *)notification;

@end
78 changes: 58 additions & 20 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#endif

NSString *const RCTLocalNotificationReceived = @"LocalNotificationReceived";
NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered";

Expand All @@ -36,6 +37,7 @@ + (UILocalNotification *)UILocalNotification:(id)json
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;
}

Expand All @@ -56,6 +58,10 @@ - (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleLocalNotificationReceived:)
name:RCTLocalNotificationReceived
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationReceived:)
name:RCTRemoteNotificationReceived
Expand All @@ -68,7 +74,8 @@ - (void)setBridge:(RCTBridge *)bridge

- (NSDictionary<NSString *, id> *)constantsToExport
{
NSDictionary<NSString *, id> *initialNotification = [_bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] copy];
NSDictionary<NSString *, id> *initialNotification =
[_bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] copy];
return @{@"initialNotification": RCTNullIfNil(initialNotification)};
}

Expand All @@ -87,12 +94,9 @@ + (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
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
Expand All @@ -102,6 +106,26 @@ + (void)didReceiveRemoteNotification:(NSDictionary *)notification
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"
Expand All @@ -127,9 +151,7 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
*/
RCT_EXPORT_METHOD(getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback)
{
callback(@[
@(RCTSharedApplication().applicationIconBadgeNumber)
]);
callback(@[@(RCTSharedApplication().applicationIconBadgeNumber)]);
}

RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions)
Expand All @@ -140,13 +162,13 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification

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 {
Expand All @@ -155,7 +177,8 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification

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 {
Expand All @@ -171,8 +194,7 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
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;
}

Expand All @@ -189,12 +211,11 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification

}

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)
Expand All @@ -212,4 +233,21 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
[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
2 changes: 1 addition & 1 deletion React/Base/RCTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RCT_EXTERN NSString *__nullable RCTJSONStringify(id __nullable jsonObject, NSErr
RCT_EXTERN id __nullable RCTJSONParse(NSString *__nullable jsonString, NSError **error);
RCT_EXTERN id __nullable RCTJSONParseMutable(NSString *__nullable jsonString, NSError **error);

// Santize a JSON string by stripping invalid objects and/or NaN values
// Sanitize a JSON object by stripping invalid types and/or NaN values
RCT_EXTERN id RCTJSONClean(id object);

// Get MD5 hash of a string
Expand Down

0 comments on commit 758d9e8

Please sign in to comment.