-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathFirestackCloudMessaging.m
More file actions
331 lines (283 loc) · 10.3 KB
/
FirestackCloudMessaging.m
File metadata and controls
331 lines (283 loc) · 10.3 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// FirestackMessaging.m
// Firestack
//
// Created by Ari Lerner on 8/23/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <NotificationCenter/NotificationCenter.h>
#import "FirestackCloudMessaging.h"
#import "FirestackEvents.h"
#import "RCTConvert.h"
@import FirebaseInstanceID;
@import FirebaseMessaging;
// https://github.com/facebook/react-native/blob/master/Libraries/PushNotificationIOS/RCTPushNotificationManager.m
@implementation RCTConvert (UILocalNotification)
+ (UILocalNotification *)UILocalNotification:(id)json
{
NSDictionary<NSString *, id> *details = [self NSDictionary:json];
UILocalNotification *notification = [UILocalNotification new];
notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]] ?: [NSDate date];
notification.alertBody = [RCTConvert NSString:details[@"alertBody"]];
notification.alertAction = [RCTConvert NSString:details[@"alertAction"]];
notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName;
notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]];
notification.category = [RCTConvert NSString:details[@"category"]];
if (details[@"applicationIconBadgeNumber"]) {
notification.applicationIconBadgeNumber = [RCTConvert NSInteger:details[@"applicationIconBadgeNumber"]];
}
return notification;
}
@end
@implementation FirestackCloudMessaging
// https://github.com/facebook/react-native/blob/master/Libraries/PushNotificationIOS/RCTPushNotificationManager.m
static NSDictionary *RCTFormatLocalNotification(UILocalNotification *notification)
{
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
if (notification.fireDate) {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
NSString *fireDateString = [formatter stringFromDate:notification.fireDate];
formattedLocalNotification[@"fireDate"] = fireDateString;
}
formattedLocalNotification[@"alertAction"] = RCTNullIfNil(notification.alertAction);
formattedLocalNotification[@"alertBody"] = RCTNullIfNil(notification.alertBody);
formattedLocalNotification[@"applicationIconBadgeNumber"] = @(notification.applicationIconBadgeNumber);
formattedLocalNotification[@"category"] = RCTNullIfNil(notification.category);
formattedLocalNotification[@"soundName"] = RCTNullIfNil(notification.soundName);
formattedLocalNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(notification.userInfo));
formattedLocalNotification[@"remote"] = @NO;
return formattedLocalNotification;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
}
+ (void) setup:(UIApplication *) application
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(connectToFirebase)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(disconnectFromFirebase)
name: UIApplicationDidBecomeActiveNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationReceived:)
name:MESSAGING_MESSAGE_RECEIVED_REMOTE
object: nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleLocalNotificationReceived:)
name:MESSAGING_MESSAGE_RECEIVED_LOCAL
object: nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleTokenRefresh)
name:kFIRInstanceIDTokenRefreshNotification
object: nil];
if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"9.0")) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}
];
// For iOS 10 display notification (sent via APNS)
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
// For iOS 10 data message (sent via FCM)
[[FIRMessaging messaging] setRemoteMessageDelegate:self];
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
#pragma mark callbacks
- (void) connectToFirebase
{
[[FIRMessaging messaging] connectWithCompletion:^(NSError *error) {
NSDictionary *evt;
NSString *evtName;
if (error != nil) {
NSLog(@"Error connecting: %@", [error debugDescription]);
evtName = MESSAGING_SUBSYSTEM_ERROR;
evt = @{
@"eventName": MESSAGING_SUBSYSTEM_ERROR,
@"msg": [error debugDescription]
};
} else {
NSLog(@"Connected to Firebase messaging");
evtName = MESSAGING_SUBSYSTEM_EVENT;
evt = @{
@"result": @"connected"
};
[self
sendJSEvent:evtName
props: evt];
}
}];
}
- (void) disconnectFromFirebase
{
[[FIRMessaging messaging] disconnect];
NSLog(@"Disconnect from Firebase");
[self
sendJSEvent:MESSAGING_SUBSYSTEM_EVENT
props: @{
@"status": @"disconnected"
}];
}
- (void) handleRemoteNotificationReceived:(NSNotification *) n
{
NSMutableDictionary *props = [[NSMutableDictionary alloc] initWithDictionary: n.userInfo];
[self sendJSEvent:MESSAGING_MESSAGE_RECEIVED_REMOTE props: props];
}
- (void) handleLocalNotificationReceived:(NSNotification *) n
{
NSMutableDictionary *props = [[NSMutableDictionary alloc] initWithDictionary: n.userInfo];
[self sendJSEvent:MESSAGING_MESSAGE_RECEIVED_LOCAL props: props];
}
- (void) handleTokenRefresh
{
NSDictionary *props = @{
@"status": @"token_refreshed",
@"token": [[FIRInstanceID instanceID] token]
};
[self sendJSEvent:MESSAGING_TOKEN_REFRESH props: props];
}
RCT_EXPORT_MODULE(FirestackCloudMessaging);
RCT_EXPORT_METHOD(getToken:(RCTResponseSenderBlock)callback)
{
NSString *token = [[FIRInstanceID instanceID] token];
callback(@[[NSNull null], @{
@"status": @"success",
@"token": token
}]);
}
RCT_EXPORT_METHOD(sendLocal:(UILocalNotification *)notification
callback:(RCTResponseSenderBlock) callback)
{
NSLog(@"sendLocal called with notification: %@", notification);
[RCTSharedApplication() presentLocalNotificationNow:notification];
}
RCT_EXPORT_METHOD(scheduleLocal:(UILocalNotification *)notification
callback:(RCTResponseSenderBlock) callback)
{
[RCTSharedApplication() scheduleLocalNotification:notification];
}
RCT_EXPORT_METHOD(cancelAllLocalNotifications)
{
[RCTSharedApplication() cancelAllLocalNotifications];
}
RCT_EXPORT_METHOD(cancelLocalNotifications:(NSDictionary<NSString *, id> *)userInfo)
{
for (UILocalNotification *notification in [UIApplication sharedApplication].scheduledLocalNotifications) {
__block BOOL matchesAll = YES;
NSDictionary<NSString *, id> *notificationInfo = notification.userInfo;
// Note: we do this with a loop instead of just `isEqualToDictionary:`
// because we only require that all specified userInfo values match the
// notificationInfo values - notificationInfo may contain additional values
// which we don't care about.
[userInfo enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
if (![notificationInfo[key] isEqual:obj]) {
matchesAll = NO;
*stop = YES;
}
}];
if (matchesAll) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
RCT_EXPORT_METHOD(sendRemote:(UILocalNotification *)notification
callback:(RCTResponseSenderBlock) callback)
{
}
RCT_EXPORT_METHOD(send:(NSString *) senderId
messageId:(NSString *) messageId
messageType:(NSString *) messageType
msg: (NSString *) msg
callback:(RCTResponseSenderBlock)callback)
{
}
RCT_EXPORT_METHOD(listenForTokenRefresh:(RCTResponseSenderBlock)callback)
{}
RCT_EXPORT_METHOD(unlistenForTokenRefresh:(RCTResponseSenderBlock)callback)
{}
RCT_EXPORT_METHOD(subscribeToTopic:(NSString *) topic
callback:(RCTResponseSenderBlock)callback)
{
[[FIRMessaging messaging] subscribeToTopic:topic];
callback(@[[NSNull null], @{
@"result": @"success",
@"topic": topic
}]);
}
RCT_EXPORT_METHOD(unsubscribeFromTopic:(NSString *) topic
callback: (RCTResponseSenderBlock)callback)
{
[[FIRMessaging messaging] unsubscribeFromTopic:topic];
callback(@[[NSNull null], @{
@"result": @"success",
@"topic": topic
}]);
}
RCT_EXPORT_METHOD(setBadge:(NSInteger) number
callback:(RCTResponseSenderBlock) callback)
{
RCTSharedApplication().applicationIconBadgeNumber = number;
callback(@[[NSNull null], @{
@"result": @"success",
@"number": @(number)
}]);
}
RCT_EXPORT_METHOD(getBadge:(RCTResponseSenderBlock) callback)
{
callback(@[[NSNull null], @{
@"result": @"success",
@"number": @(RCTSharedApplication().applicationIconBadgeNumber)
}]);
}
RCT_EXPORT_METHOD(listenForReceiveNotification:(RCTResponseSenderBlock)callback)
{}
RCT_EXPORT_METHOD(unlistenForReceiveNotification:(RCTResponseSenderBlock)callback)
{}
RCT_EXPORT_METHOD(listenForReceiveUpstreamSend:(RCTResponseSenderBlock)callback)
{}
RCT_EXPORT_METHOD(unlistenForReceiveUpstreamSend:(RCTResponseSenderBlock)callback)
{}
// Not sure how to get away from this... yet
- (NSArray<NSString *> *)supportedEvents {
return @[
MESSAGING_SUBSYSTEM_EVENT,
MESSAGING_SUBSYSTEM_ERROR,
MESSAGING_TOKEN_REFRESH,
MESSAGING_MESSAGE_RECEIVED_LOCAL,
MESSAGING_MESSAGE_RECEIVED_REMOTE];
}
- (void) sendJSEvent:(NSString *)title
props:(NSDictionary *)props
{
@try {
[self sendEventWithName:title
body:@{
@"eventName": title,
@"body": props
}];
}
@catch (NSException *err) {
NSLog(@"An error occurred in sendJSEvent: %@", [err debugDescription]);
NSLog(@"Tried to send: %@ with %@", title, props);
}
}
@end