This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
RNNotificationActions.m
179 lines (152 loc) · 7.06 KB
/
RNNotificationActions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
@import UIKit;
#import "RNNotificationActions.h"
#import "RNNotificationActionsManager.h"
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTUtils.h>
NSString *const RNNotificationActionReceived = @"NotificationActionReceived";
@implementation RCTConvert (UIUserNotificationActivationMode)
RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (
@{
@"foreground": @(UIUserNotificationActivationModeForeground),
@"background": @(UIUserNotificationActivationModeBackground),
}), UIUserNotificationActivationModeForeground, integerValue)
@end
@implementation RCTConvert (UIUserNotificationActionBehavior)
RCT_ENUM_CONVERTER(UIUserNotificationActionBehavior, (
@{
@"default": @(UIUserNotificationActionBehaviorDefault),
@"textInput": @(UIUserNotificationActionBehaviorTextInput),
}), UIUserNotificationActionBehaviorDefault, integerValue)
@end
@implementation RCTConvert (UIUserNotificationActionContext)
RCT_ENUM_CONVERTER(UIUserNotificationActionContext, (
@{
@"default": @(UIUserNotificationActionContextDefault),
@"minimal": @(UIUserNotificationActionContextMinimal),
}), UIUserNotificationActionContextDefault, integerValue)
@end
@implementation RNNotificationActions
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
- (id)init
{
if (self = [super init]) {
return self;
} else {
return nil;
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotificationActionReceived:)
name:RNNotificationActionReceived
object:nil];
NSDictionary *lastActionInfo = [RNNotificationActionsManager sharedInstance].lastActionInfo;
if(lastActionInfo != nil) {
[[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationActionReceived
object:self
userInfo:lastActionInfo];
[RNNotificationActionsManager sharedInstance].lastActionInfo = nil;
}
}
- (UIMutableUserNotificationAction *)actionFromJSON:(NSDictionary *)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"]]];
return action;
}
- (UIUserNotificationCategory *)categoryFromJSON:(NSDictionary *)json
{
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]];
}
// Get the current types
UIUserNotificationSettings *settings;
UIUserNotificationType types = settings.types;
// Update the settings for these types
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:[NSSet setWithArray:categories]]];
});
}
RCT_EXPORT_METHOD(callCompletionHandler)
{
void (^completionHandler)() = [RNNotificationActionsManager sharedInstance].lastCompletionHandler;
if(completionHandler != nil) {
completionHandler();
[RNNotificationActionsManager sharedInstance].lastCompletionHandler = nil;
}
}
// 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 userInfo:notification.userInfo completionHandler:completionHandler];
}
+ (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
[self emitNotificationActionForIdentifier:identifier source:@"remote" responseInfo:responseInfo userInfo:userInfo completionHandler:completionHandler];
}
+ (void)emitNotificationActionForIdentifier:(NSString *)identifier source:(NSString *)source responseInfo:(NSDictionary *)responseInfo userInfo:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
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];
[RNNotificationActionsManager sharedInstance].lastActionInfo = info;
[RNNotificationActionsManager sharedInstance].lastCompletionHandler = completionHandler;
}
- (void)handleNotificationActionReceived:(NSNotification *)notification
{
[_bridge.eventDispatcher sendAppEventWithName:@"notificationActionReceived"
body:notification.userInfo];
}
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
@end