Commit c09641c1 authored by Lidan Hifi's avatar Lidan Hifi

fix native interactive notifications support

parent 2ade57c9
...@@ -6,15 +6,16 @@ ...@@ -6,15 +6,16 @@
#import "RCTConvert.h" #import "RCTConvert.h"
#import "RCTUtils.h" #import "RCTUtils.h"
NSString *const RNNotificationCreateAction = @"CREATE"; NSString* const RNNotificationCreateAction = @"CREATE";
NSString *const RNNotificationClearAction = @"CLEAR"; NSString* const RNNotificationClearAction = @"CLEAR";
NSString *const RNNotificationReceivedForeground = @"RNNotificationReceivedForeground"; NSString* const RNNotificationReceivedForeground = @"RNNotificationReceivedForeground";
NSString *const RNNotificationReceivedBackground = @"RNNotificationReceivedBackground"; NSString* const RNNotificationReceivedBackground = @"RNNotificationReceivedBackground";
NSString *const RNNotificationOpened = @"RNNotificationOpened"; NSString* const RNNotificationOpened = @"RNNotificationOpened";
NSString* const RNNotificationActionTriggered = @"RNNotificationActionTriggered";
/* /*
* Enum Converters for Interactive Notifications * Converters for Interactive Notifications
*/ */
@implementation RCTConvert (UIUserNotificationActivationMode) @implementation RCTConvert (UIUserNotificationActivationMode)
RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{ RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{
...@@ -38,23 +39,48 @@ RCT_ENUM_CONVERTER(UIUserNotificationActionBehavior, (@{ ...@@ -38,23 +39,48 @@ RCT_ENUM_CONVERTER(UIUserNotificationActionBehavior, (@{
}), UIUserNotificationActionBehaviorDefault, integerValue) }), UIUserNotificationActionBehaviorDefault, integerValue)
@end @end
@implementation RNNotifications @implementation RCTConvert (UIMutableUserNotificationAction)
+ (UIMutableUserNotificationAction *)UIMutableUserNotificationAction:(id)json
RCT_EXPORT_MODULE() {
NSDictionary<NSString *, id> *details = [self NSDictionary:json];
@synthesize bridge = _bridge; UIMutableUserNotificationAction* action =[[UIMutableUserNotificationAction alloc] init];
action.activationMode = [RCTConvert UIUserNotificationActivationMode:details[@"activationMode"]];
action.behavior = [RCTConvert UIUserNotificationActionBehavior:details[@"behavior"]];
action.authenticationRequired = [RCTConvert BOOL:details[@"authenticationRequired"]];
action.destructive = [RCTConvert BOOL:details[@"destructive"]];
action.title = [RCTConvert NSString:details[@"title"]];
action.identifier = [RCTConvert NSString:details[@"identifier"]];
NSMutableDictionary *actionCallbacks; return action;
}
@end
- (id)init @implementation RCTConvert (UIMutableUserNotificationCategory)
+ (UIMutableUserNotificationCategory *)UIMutableUserNotificationCategory:(id)json
{ {
if (self = [super init]) { NSDictionary<NSString *, id> *details = [self NSDictionary:json];
actionCallbacks = [[NSMutableDictionary alloc] init];
return self; UIMutableUserNotificationCategory* category = [[UIMutableUserNotificationCategory alloc] init];
} else { category.identifier = details[@"identifier"];
return nil;
// category actions
NSMutableArray* actions = [[NSMutableArray alloc] init];
for (NSDictionary* actionJson in [RCTConvert NSArray:details[@"actions"]]) {
[actions addObject:[RCTConvert UIMutableUserNotificationAction:actionJson]];
} }
[category setActions:actions forContext:[RCTConvert UIUserNotificationActionContext:details[@"context"]]];
return category;
} }
@end
@implementation RNNotifications
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
- (void)dealloc - (void)dealloc
{ {
...@@ -79,6 +105,11 @@ NSMutableDictionary *actionCallbacks; ...@@ -79,6 +105,11 @@ NSMutableDictionary *actionCallbacks;
selector:@selector(handleNotificationOpened:) selector:@selector(handleNotificationOpened:)
name:RNNotificationOpened name:RNNotificationOpened
object:nil]; object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotificationActionTriggered:)
name:RNNotificationActionTriggered
object:nil];
} }
/* /*
...@@ -113,6 +144,18 @@ NSMutableDictionary *actionCallbacks; ...@@ -113,6 +144,18 @@ NSMutableDictionary *actionCallbacks;
} }
} }
+ (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
[self emitNotificationActionForIdentifier:identifier responseInfo:responseInfo userInfo:notification.userInfo];
completionHandler();
}
+ (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
[self emitNotificationActionForIdentifier:identifier responseInfo:responseInfo userInfo:userInfo];
completionHandler();
}
/* /*
* Notification handlers * Notification handlers
*/ */
...@@ -157,7 +200,6 @@ NSMutableDictionary *actionCallbacks; ...@@ -157,7 +200,6 @@ NSMutableDictionary *actionCallbacks;
/* /*
* Helper methods * Helper methods
*/ */
+ (void)dispatchLocalNotificationFromNotification:(NSDictionary *)notification + (void)dispatchLocalNotificationFromNotification:(NSDictionary *)notification
{ {
NSDictionary* managedAps = [notification objectForKey:@"managedAps"]; NSDictionary* managedAps = [notification objectForKey:@"managedAps"];
...@@ -211,48 +253,47 @@ NSMutableDictionary *actionCallbacks; ...@@ -211,48 +253,47 @@ NSMutableDictionary *actionCallbacks;
return [NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] bundleIdentifier], notificationId]; return [NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] bundleIdentifier], notificationId];
} }
+ (UIMutableUserNotificationAction *)parseAction:(NSDictionary *)json + (void)updateNotificationCategories:(NSArray *)json
{
UIMutableUserNotificationAction* action =[[UIMutableUserNotificationAction alloc] init];
action.activationMode = [RCTConvert UIUserNotificationActivationMode:json[@"activationMode"]];
action.behavior = [RCTConvert UIUserNotificationActionBehavior:json[@"behavior"]];
action.authenticationRequired = [RCTConvert BOOL:json[@"authenticationRequired"]];
action.destructive = [RCTConvert BOOL:json[@"destructive"]];
action.title = json[@"title"];
action.identifier = json[@"identifier"];
return action;
}
+ (UIMutableUserNotificationCategory *)parseCategory:(NSDictionary *)json
{ {
UIMutableUserNotificationCategory* category = [[UIMutableUserNotificationCategory alloc] init]; NSMutableSet* categories = nil;
category.identifier = json[@"identifier"];
// category actions if ([json count] > 0) {
NSMutableArray* actions = [[NSMutableArray alloc] init]; categories = [[NSMutableSet alloc] init];
for (NSDictionary* actionJson in [RCTConvert NSArray:json[@"actions"]]) { for (NSDictionary* categoryJson in json) {
[actions addObject:[self parseAction:actionJson]]; [categories addObject:[RCTConvert UIMutableUserNotificationCategory:categoryJson]];
}
} }
[category setActions:actions forContext:[RCTConvert UIUserNotificationActionContext:json[@"context"]]]; UIUserNotificationType types = (UIUserNotificationType) (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
return category; [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} }
+ (void)updateNotificationCategories:(NSArray *)json + (void)emitNotificationActionForIdentifier:(NSString *)identifier responseInfo:(NSDictionary *)responseInfo userInfo:(NSDictionary *)userInfo
{ {
NSMutableSet* categories = [[NSMutableSet alloc] init]; NSMutableDictionary* info = [[NSMutableDictionary alloc] initWithDictionary:@{ @"identifier": identifier }];
for (NSDictionary* categoryJson in json) {
[categories addObject:[self parseCategory:categoryJson]]; // add text
NSString* text = [responseInfo objectForKey:UIUserNotificationActionResponseTypedTextKey];
if (text != NULL) {
info[@"text"] = text;
} }
UIUserNotificationType types = (UIUserNotificationType) (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert); // add notification custom data
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; if (userInfo != NULL) {
info[@"data"] = userInfo;
}
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; // Emit event
[[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationActionTriggered
object:self
userInfo:info];
} }
/*
* Javascript events
*/
- (void)handleNotificationReceivedForeground:(NSNotification *)notification - (void)handleNotificationReceivedForeground:(NSNotification *)notification
{ {
[_bridge.eventDispatcher sendDeviceEventWithName:@"notificationReceivedForeground" body:notification.userInfo]; [_bridge.eventDispatcher sendDeviceEventWithName:@"notificationReceivedForeground" body:notification.userInfo];
...@@ -268,6 +309,11 @@ NSMutableDictionary *actionCallbacks; ...@@ -268,6 +309,11 @@ NSMutableDictionary *actionCallbacks;
[_bridge.eventDispatcher sendDeviceEventWithName:@"notificationOpened" body:notification.userInfo]; [_bridge.eventDispatcher sendDeviceEventWithName:@"notificationOpened" body:notification.userInfo];
} }
- (void)handleNotificationActionTriggered:(NSNotification *)notification
{
[_bridge.eventDispatcher sendAppEventWithName:@"notificationActionReceived" body:notification.userInfo];
}
/* /*
* React Native exported methods * React Native exported methods
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment