Skip to content

Commit

Permalink
userInfo + formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
holmesal committed Feb 22, 2016
1 parent 8dfd0e6 commit fd855cb
Showing 1 changed file with 84 additions and 90 deletions.
174 changes: 84 additions & 90 deletions RNNotificationActions/RNNotificationActions.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@
NSString *const RNNotificationActionReceived = @"NotificationActionReceived";

@implementation RCTConvert (UIUserNotificationActivationMode)
RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{
@"foreground": @(UIUserNotificationActivationModeForeground),
@"background": @(UIUserNotificationActivationModeBackground),
}), UIUserNotificationActivationModeForeground, integerValue)
RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (
@{
@"foreground": @(UIUserNotificationActivationModeForeground),
@"background": @(UIUserNotificationActivationModeBackground),
}), UIUserNotificationActivationModeForeground, integerValue)
@end

@implementation RCTConvert (UIUserNotificationActionBehavior)
RCT_ENUM_CONVERTER(UIUserNotificationActionBehavior, (@{
@"default": @(UIUserNotificationActionBehaviorDefault),
@"textInput": @(UIUserNotificationActionBehaviorTextInput),
}), UIUserNotificationActionBehaviorDefault, integerValue)
RCT_ENUM_CONVERTER(UIUserNotificationActionBehavior, (
@{
@"default": @(UIUserNotificationActionBehaviorDefault),
@"textInput": @(UIUserNotificationActionBehaviorTextInput),
}), UIUserNotificationActionBehaviorDefault, integerValue)
@end

@implementation RCTConvert (UIUserNotificationActionContext)
RCT_ENUM_CONVERTER(UIUserNotificationActionContext, (@{
@"default": @(UIUserNotificationActionContextDefault),
@"minimal": @(UIUserNotificationActionContextMinimal),
}), UIUserNotificationActionContextDefault, integerValue)
RCT_ENUM_CONVERTER(UIUserNotificationActionContext, (
@{
@"default": @(UIUserNotificationActionContextDefault),
@"minimal": @(UIUserNotificationActionContextMinimal),
}), UIUserNotificationActionContextDefault, integerValue)
@end

@implementation RNNotificationActions
Expand All @@ -37,7 +40,6 @@ @implementation RNNotificationActions

- (id)init
{
// NSLog(@"initializing!!!");
if (self = [super init]) {
self.completeCallbacks = [[NSMutableDictionary alloc] init];
return self;
Expand All @@ -48,114 +50,106 @@ - (id)init

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotificationActionReceived:)
name:RNNotificationActionReceived
object:nil];
_bridge = bridge;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotificationActionReceived:)
name:RNNotificationActionReceived
object:nil];
}

- (UIMutableUserNotificationAction *)actionFromJSON:(NSDictionary *)opts
{
//RCTLogInfo(@"hello from notificationactions: %@", opts);
UIMutableUserNotificationAction *action;
action = [[UIMutableUserNotificationAction alloc] init];
[action setActivationMode: [RCTConvert UIUserNotificationActivationMode:opts[@"activationMode"]]];
[action setBehavior: [RCTConvert UIUserNotificationActionBehavior:opts[@"behavior"]]];
[action setTitle:opts[@"title"]];
[action setIdentifier:opts[@"identifier"]];
[action setDestructive:[RCTConvert BOOL:opts[@"destructive"]]];
[action setAuthenticationRequired:[RCTConvert BOOL:opts[@"authenticationRequired"]]];
// NSLog(@"action: %@", action);
return action;
UIMutableUserNotificationAction *action;
action = [[UIMutableUserNotificationAction alloc] init];
[action setActivationMode: [RCTConvert UIUserNotificationActivationMode:opts[@"activationMode"]]];
[action setBehavior: [RCTConvert UIUserNotificationActionBehavior:opts[@"behavior"]]];
[action setTitle:opts[@"title"]];
[action setIdentifier:opts[@"identifier"]];
[action setDestructive:[RCTConvert BOOL:opts[@"destructive"]]];
[action setAuthenticationRequired:[RCTConvert BOOL:opts[@"authenticationRequired"]]];
return action;
}

- (UIUserNotificationCategory *)categoryFromJSON:(NSDictionary *)json
{
// RCTLogInfo(@"hello from category: %@", json);
UIMutableUserNotificationCategory *category;
category = [[UIMutableUserNotificationCategory alloc] init];
[category setIdentifier:[RCTConvert NSString:json[@"identifier"]]];

// // Get the context
// UIUserNotificationActionContext context;
// context = [RCTConvert UIUserNotificationActionContext:json[@"context"]];

// Get the actions from the category
NSMutableArray *actions;
actions = [[NSMutableArray alloc] init];
for (NSDictionary *actionJSON in [RCTConvert NSArray:json[@"actions"]]) {
[actions addObject:[self actionFromJSON:actionJSON]];
}

// Set these actions for this context
[category setActions:actions
forContext:[RCTConvert UIUserNotificationActionContext:json[@"context"]]];
// NSLog(@"got actions: %@", actions);
// NSLog(@"made category: %@", category);
return category;
UIMutableUserNotificationCategory *category;
category = [[UIMutableUserNotificationCategory alloc] init];
[category setIdentifier:[RCTConvert NSString:json[@"identifier"]]];

// Get the actions from the category
NSMutableArray *actions;
actions = [[NSMutableArray alloc] init];
for (NSDictionary *actionJSON in [RCTConvert NSArray:json[@"actions"]]) {
[actions addObject:[self actionFromJSON:actionJSON]];
}

// Set these actions for this context
[category setActions:actions
forContext:[RCTConvert UIUserNotificationActionContext:json[@"context"]]];
return category;
}

RCT_EXPORT_METHOD(updateCategories:(NSArray *)json)
{
NSMutableArray *categories = [[NSMutableArray alloc] init];
// Create the category
for (NSDictionary *categoryJSON in json) {
[categories addObject:[self categoryFromJSON:categoryJSON]];
}
// NSLog(@"got categories, %@", categories);

// Get the current types
UIUserNotificationSettings *settings;
UIUserNotificationType types = settings.types;

// Update the settings for these types
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:[NSSet setWithArray:categories]]];
NSMutableArray *categories = [[NSMutableArray alloc] init];
// Create the category
for (NSDictionary *categoryJSON in json) {
[categories addObject:[self categoryFromJSON:categoryJSON]];
}

// Get the current types
UIUserNotificationSettings *settings;
UIUserNotificationType types = settings.types;

// Update the settings for these types
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:[NSSet setWithArray:categories]]];
}

// Given a identifier and completion handler, store the associated block
// TODO


// Handle notifications received by the app delegate and passed to the following class methods

+ (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler;
{
[self emitNotificationActionForIdentifier:identifier source:@"local" responseInfo:responseInfo];
completionHandler();
[self emitNotificationActionForIdentifier:identifier source:@"local" 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 source:@"remote" responseInfo:responseInfo];
completionHandler();
[self emitNotificationActionForIdentifier:identifier source:@"remote" responseInfo:responseInfo userInfo:userInfo];
completionHandler();
}

+ (void)emitNotificationActionForIdentifier:(NSString *)identifier source:(NSString *)source responseInfo:(NSDictionary *)responseInfo
+ (void)emitNotificationActionForIdentifier:(NSString *)identifier source:(NSString *)source responseInfo:(NSDictionary *)responseInfo userInfo:(NSDictionary *)userInfo
{
NSMutableDictionary *info = [[NSMutableDictionary alloc] initWithDictionary:@{
@"identifier": identifier,
@"source": @"local"
}
];
NSString *text = [responseInfo objectForKey:UIUserNotificationActionResponseTypedTextKey];
if (text != NULL) {
info[@"text"] = text;
}
[[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationActionReceived
object:self
userInfo: info];
NSMutableDictionary *info = [[NSMutableDictionary alloc] initWithDictionary:@{
@"identifier": identifier,
@"source": @"local"
}
];
// Add text if present
NSString *text = [responseInfo objectForKey:UIUserNotificationActionResponseTypedTextKey];
if (text != NULL) {
info[@"text"] = text;
}
// Add userinfo if present
if (userInfo != NULL) {
info[@"userInfo"] = userInfo;
}
// Emit
[[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationActionReceived
object:self
userInfo:info];
}

- (void)handleNotificationActionReceived:(NSNotification *)notification
{
[_bridge.eventDispatcher sendAppEventWithName:@"notificationActionReceived"
body:notification.userInfo];
[_bridge.eventDispatcher sendAppEventWithName:@"notificationActionReceived"
body:notification.userInfo];
}

@end

0 comments on commit fd855cb

Please sign in to comment.