/
RCTOneSignal.m
229 lines (180 loc) · 7.58 KB
/
RCTOneSignal.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#if __has_include(<React/RCTConvert.h>)
#import <React/RCTConvert.h>
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTUtils.h>
#else
#import "RCTConvert.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "RCTUtils.h"
#endif
#import "RCTOneSignal.h"
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
#define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert
#define UIUserNotificationTypeBadge UIRemoteNotificationTypeBadge
#define UIUserNotificationTypeSound UIRemoteNotificationTypeSound
#define UIUserNotificationTypeNone UIRemoteNotificationTypeNone
#define UIUserNotificationType UIRemoteNotificationType
#endif
@interface RCTOneSignal ()
@end
@implementation RCTOneSignal
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE(RCTOneSignal)
static RCTBridge *curRCTBridge;
OSNotificationOpenedResult* coldStartOSNotificationOpenedResult;
- (void)setBridge:(RCTBridge *)receivedBridge {
_bridge = receivedBridge;
curRCTBridge = receivedBridge;
if (coldStartOSNotificationOpenedResult) {
[self handleRemoteNotificationOpened:[coldStartOSNotificationOpenedResult stringify]];
coldStartOSNotificationOpenedResult = nil;
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id)initWithLaunchOptions:(NSDictionary *)launchOptions appId:(NSString *)appId {
return [self initWithLaunchOptions:launchOptions appId:appId settings:nil];
}
- (id)initWithLaunchOptions:(NSDictionary *)launchOptions appId:(NSString *)appId settings:(NSDictionary*)settings {
[OneSignal setValue:@"react" forKey:@"mSDKType"];
[OneSignal initWithLaunchOptions:launchOptions
appId:appId
handleNotificationReceived:^(OSNotification* notification) {
[self handleRemoteNotificationReceived:[notification stringify]];
}
handleNotificationAction:^(OSNotificationOpenedResult *result) {
if (!curRCTBridge)
coldStartOSNotificationOpenedResult = result;
else
[self handleRemoteNotificationOpened:[result stringify]];
}
settings:settings];
return self;
}
// This isn't required, the iOS native SDK already hooks into this event.
+ (void)didReceiveRemoteNotification:(NSDictionary *)dictionary {
// Keeping empty method around so developers do not get compile errors when updating versions.
}
- (void)handleRemoteNotificationReceived:(NSString *)notification {
NSError *jsonError;
NSData *objectData = [notification dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
[curRCTBridge.eventDispatcher sendAppEventWithName:@"remoteNotificationReceived" body:json];
}
- (void)handleRemoteNotificationOpened:(NSString *)result {
NSError *jsonError;
NSData *objectData = [result dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
[curRCTBridge.eventDispatcher sendAppEventWithName:@"remoteNotificationOpened" body:json];
}
- (void)handleRemoteNotificationsRegistered:(NSNotification *)notification {
[self.bridge.eventDispatcher sendAppEventWithName:@"remoteNotificationsRegistered" body:notification.userInfo];
}
RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback)
{
if (RCTRunningInAppExtension()) {
callback(@[@{@"alert": @NO, @"badge": @NO, @"sound": @NO}]);
return;
}
NSUInteger types = 0;
if ([UIApplication instancesRespondToSelector:@selector(currentUserNotificationSettings)]) {
types = [RCTSharedApplication() currentUserNotificationSettings].types;
} else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
types = [RCTSharedApplication() enabledRemoteNotificationTypes];
#endif
}
callback(@[@{
@"alert": @((types & UIUserNotificationTypeAlert) > 0),
@"badge": @((types & UIUserNotificationTypeBadge) > 0),
@"sound": @((types & UIUserNotificationTypeSound) > 0),
}]);
}
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions) {
if (RCTRunningInAppExtension()) {
return;
}
UIUserNotificationType types = UIUserNotificationTypeNone;
if (permissions) {
if ([RCTConvert BOOL:permissions[@"alert"]]) {
types |= UIUserNotificationTypeAlert;
}
if ([RCTConvert BOOL:permissions[@"badge"]]) {
types |= UIUserNotificationTypeBadge;
}
if ([RCTConvert BOOL:permissions[@"sound"]]) {
types |= UIUserNotificationTypeSound;
}
} else {
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
}
UIApplication *app = RCTSharedApplication();
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *notificationSettings =
[UIUserNotificationSettings settingsForTypes:(NSUInteger)types categories:nil];
[app registerUserNotificationSettings:notificationSettings];
[app registerForRemoteNotifications];
} else {
[app registerForRemoteNotificationTypes:(NSUInteger)types];
}
}
RCT_EXPORT_METHOD(registerForPushNotifications) {
[OneSignal registerForPushNotifications];
}
RCT_EXPORT_METHOD(sendTag:(NSString *)key value:(NSString*)value) {
[OneSignal sendTag:key value:value];
}
RCT_EXPORT_METHOD(configure) {
[OneSignal IdsAvailable:^(NSString* userId, NSString* pushToken) {
NSDictionary *params = @{
@"pushToken": pushToken ?: [NSNull null],
@"userId" : userId ?: [NSNull null]
};
[self.bridge.eventDispatcher sendAppEventWithName:@"idsAvailable" body:params];
}];
}
RCT_EXPORT_METHOD(sendTags:(NSDictionary *)properties) {
[OneSignal sendTags:properties onSuccess:^(NSDictionary *sucess) {
NSLog(@"Send Tags Success");
} onFailure:^(NSError *error) {
NSLog(@"Send Tags Failure");
}];}
RCT_EXPORT_METHOD(getTags:(RCTResponseSenderBlock)callback) {
[OneSignal getTags:^(NSDictionary *tags) {
NSLog(@"Get Tags Success");
callback(@[tags]);
} onFailure:^(NSError *error) {
NSLog(@"Get Tags Failure");
callback(@[error]);
}];
}
RCT_EXPORT_METHOD(deleteTag:(NSString *)key) {
[OneSignal deleteTag:key];
}
RCT_EXPORT_METHOD(setSubscription:(BOOL)enable) {
[OneSignal setSubscription:enable];
}
RCT_EXPORT_METHOD(promptLocation) {
[OneSignal promptLocation];
}
RCT_EXPORT_METHOD(postNotification:(NSDictionary *)contents data:(NSDictionary *)data player_id:(NSString*)player_id) {
[OneSignal postNotification:@{
@"contents" : contents,
@"data" : @{@"p2p_notification": data},
@"include_player_ids": @[player_id]
}];
}
RCT_EXPORT_METHOD(syncHashedEmail:(NSString*)email) {
[OneSignal syncHashedEmail:email];
}
RCT_EXPORT_METHOD(setLogLevel:(ONE_S_LOG_LEVEL)logLevel visualLogLevel:(ONE_S_LOG_LEVEL)visualLogLevel) {
[OneSignal setLogLevel:logLevel visualLevel:visualLogLevel];
}
@end