cordova-plugin-appice 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,529 +1,529 @@
1
- #import "AppDelegate+FirebasePlugin.h"
2
- #import "FirebasePlugin.h"
3
- #import "Firebase.h"
4
- #import "AppICEPlugin.h"
5
- #import <objc/runtime.h>
6
-
7
-
8
- @import UserNotifications;
9
- @import FirebaseFirestore;
10
-
11
- // Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices running iOS 10 and above.
12
- // Implement FIRMessagingDelegate to receive data message via FCM for devices running iOS 10 and above.
13
- @interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate>
14
- @end
15
-
16
- #define kApplicationInBackgroundKey @"applicationInBackground"
17
-
18
- @implementation AppDelegate (FirebasePlugin)
19
-
20
- static AppDelegate* instance;
21
- static id <UNUserNotificationCenterDelegate> _previousDelegate;
22
-
23
- + (AppDelegate*) instance {
24
- return instance;
25
- }
26
-
27
- static NSDictionary* mutableUserInfo;
28
- static FIRAuthStateDidChangeListenerHandle authStateChangeListener;
29
- static bool authStateChangeListenerInitialized = false;
30
-
31
- + (void)load {
32
- Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
33
- Method swizzled = class_getInstanceMethod(self, @selector(application:swizzledDidFinishLaunchingWithOptions:));
34
- method_exchangeImplementations(original, swizzled);
35
- }
36
-
37
- - (void)setApplicationInBackground:(NSNumber *)applicationInBackground {
38
- objc_setAssociatedObject(self, kApplicationInBackgroundKey, applicationInBackground, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
39
- }
40
-
41
- - (NSNumber *)applicationInBackground {
42
- return objc_getAssociatedObject(self, kApplicationInBackgroundKey);
43
- }
44
-
45
- - (BOOL)application:(UIApplication *)application swizzledDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
46
- [self application:application swizzledDidFinishLaunchingWithOptions:launchOptions];
47
-
48
- @try{
49
- instance = self;
50
-
51
- bool isFirebaseInitializedWithPlist = false;
52
- if(![FIRApp defaultApp]) {
53
- // get GoogleService-Info.plist file path
54
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];
55
-
56
- // if file is successfully found, use it
57
- if(filePath){
58
- [FirebasePlugin.firebasePlugin _logMessage:@"GoogleService-Info.plist found, setup: [FIRApp configureWithOptions]"];
59
- // create firebase configure options passing .plist as content
60
- FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
61
-
62
- // configure FIRApp with options
63
- [FIRApp configureWithOptions:options];
64
-
65
- isFirebaseInitializedWithPlist = true;
66
- }else{
67
- // no .plist found, try default App
68
- [FirebasePlugin.firebasePlugin _logError:@"GoogleService-Info.plist NOT FOUND, setup: [FIRApp defaultApp]"];
69
- [FIRApp configure];
70
- }
71
- }else{
72
- // Firebase SDK has already been initialised:
73
- // Assume that another call (probably from another plugin) did so with the plist
74
- isFirebaseInitializedWithPlist = true;
75
- }
76
-
77
- // Set UNUserNotificationCenter delegate
78
- if ([UNUserNotificationCenter currentNotificationCenter].delegate != nil) {
79
- _previousDelegate = [UNUserNotificationCenter currentNotificationCenter].delegate;
80
- }
81
- [UNUserNotificationCenter currentNotificationCenter].delegate = self;
82
-
83
- // Set FCM messaging delegate
84
- [FIRMessaging messaging].delegate = self;
85
-
86
- // Setup Firestore
87
- [FirebasePlugin setFirestore:[FIRFirestore firestore]];
88
-
89
- // Setup Google SignIn
90
- [GIDSignIn sharedInstance].clientID = [FIRApp defaultApp].options.clientID;
91
- [GIDSignIn sharedInstance].delegate = self;
92
-
93
- authStateChangeListener = [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
94
- @try {
95
- if(!authStateChangeListenerInitialized){
96
- authStateChangeListenerInitialized = true;
97
- }else{
98
- [FirebasePlugin.firebasePlugin executeGlobalJavascript:[NSString stringWithFormat:@"FirebasePlugin._onAuthStateChange(%@)", (user != nil ? @"true": @"false")]];
99
- }
100
- }@catch (NSException *exception) {
101
- [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
102
- }
103
- }];
104
-
105
-
106
- self.applicationInBackground = @(YES);
107
-
108
- }@catch (NSException *exception) {
109
- [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
110
- }
111
-
112
- return YES;
113
- }
114
-
115
- - (void)applicationDidBecomeActive:(UIApplication *)application {
116
- self.applicationInBackground = @(NO);
117
- [FirebasePlugin.firebasePlugin _logMessage:@"Enter foreground"];
118
- }
119
-
120
- - (void)applicationDidEnterBackground:(UIApplication *)application {
121
- self.applicationInBackground = @(YES);
122
- [FirebasePlugin.firebasePlugin _logMessage:@"Enter background"];
123
- }
124
-
125
- # pragma mark - Google SignIn
126
- - (void)signIn:(GIDSignIn *)signIn
127
- didSignInForUser:(GIDGoogleUser *)user
128
- withError:(NSError *)error {
129
- @try{
130
- CDVPluginResult* pluginResult;
131
- if (error == nil) {
132
- GIDAuthentication *authentication = user.authentication;
133
- FIRAuthCredential *credential =
134
- [FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken
135
- accessToken:authentication.accessToken];
136
-
137
- NSNumber* key = [[FirebasePlugin firebasePlugin] saveAuthCredential:credential];
138
- NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
139
- [result setValue:@"true" forKey:@"instantVerification"];
140
- [result setValue:key forKey:@"id"];
141
- pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
142
- } else {
143
- pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description];
144
- }
145
- if ([FirebasePlugin firebasePlugin].googleSignInCallbackId != nil) {
146
- [[FirebasePlugin firebasePlugin].commandDelegate sendPluginResult:pluginResult callbackId:[FirebasePlugin firebasePlugin].googleSignInCallbackId];
147
- }
148
- }@catch (NSException *exception) {
149
- [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
150
- }
151
- }
152
-
153
- - (void)signIn:(GIDSignIn *)signIn
154
- didDisconnectWithUser:(GIDGoogleUser *)user
155
- withError:(NSError *)error {
156
- NSString* msg = @"Google SignIn delegate: didDisconnectWithUser";
157
- if(error != nil){
158
- [FirebasePlugin.firebasePlugin _logError:[NSString stringWithFormat:@"%@: %@", msg, error]];
159
- }else{
160
- [FirebasePlugin.firebasePlugin _logMessage:msg];
161
- }
162
- }
163
-
164
- # pragma mark - FIRMessagingDelegate
165
- - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
166
- @try{
167
- [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"didReceiveRegistrationToken: %@", fcmToken]];
168
- [FirebasePlugin.firebasePlugin sendToken:fcmToken];
169
- }@catch (NSException *exception) {
170
- [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
171
- }
172
- }
173
-
174
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
175
- [FIRMessaging messaging].APNSToken = deviceToken;
176
- [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken]];
177
- [FirebasePlugin.firebasePlugin sendApnsToken:[FirebasePlugin.firebasePlugin hexadecimalStringFromData:deviceToken]];
178
- [[AppICEPlugin appice] handleToken:deviceToken];
179
-
180
- }
181
-
182
- //Tells the app that a remote notification arrived that indicates there is data to be fetched.
183
- // Called when a message arrives in the foreground and remote notifications permission has been granted
184
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
185
- fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
186
-
187
- @try{
188
- [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
189
- mutableUserInfo = [userInfo mutableCopy];
190
- NSDictionary* aps = [mutableUserInfo objectForKey:@"aps"];
191
- bool isContentAvailable = false;
192
- if([aps objectForKey:@"alert"] != nil){
193
- isContentAvailable = [[aps objectForKey:@"content-available"] isEqualToNumber:[NSNumber numberWithInt:1]];
194
- [mutableUserInfo setValue:@"notification" forKey:@"messageType"];
195
- NSString* tap;
196
- if([self.applicationInBackground isEqual:[NSNumber numberWithBool:YES]] && !isContentAvailable){
197
- tap = @"background";
198
- }
199
- [mutableUserInfo setValue:tap forKey:@"tap"];
200
- }else{
201
- [mutableUserInfo setValue:@"data" forKey:@"messageType"];
202
- }
203
-
204
- [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"didReceiveRemoteNotification: %@", mutableUserInfo]];
205
-
206
- completionHandler(UIBackgroundFetchResultNewData);
207
- if([self.applicationInBackground isEqual:[NSNumber numberWithBool:YES]] && isContentAvailable){
208
- [FirebasePlugin.firebasePlugin _logError:@"didReceiveRemoteNotification: omitting foreground notification as content-available:1 so system notification will be shown"];
209
- }else{
210
- [self processMessageForForegroundNotification:mutableUserInfo];
211
- }
212
- if([self.applicationInBackground isEqual:[NSNumber numberWithBool:YES]] || !isContentAvailable){
213
- [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
214
- }
215
- }@catch (NSException *exception) {
216
- [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
217
- }
218
- [[AppICEPlugin appice] onHandleRemoteUNotification:userInfo];
219
-
220
- }
221
-
222
- // Scans a message for keys which indicate a notification should be shown.
223
- // If found, extracts relevant keys and uses then to display a local notification
224
- -(void)processMessageForForegroundNotification:(NSDictionary*)messageData {
225
- bool showForegroundNotification = [messageData objectForKey:@"notification_foreground"];
226
- if(!showForegroundNotification){
227
- return;
228
- }
229
-
230
- NSString* title = nil;
231
- NSString* body = nil;
232
- NSString* sound = nil;
233
- NSNumber* badge = nil;
234
-
235
- // Extract APNS notification keys
236
- NSDictionary* aps = [messageData objectForKey:@"aps"];
237
- if([aps objectForKey:@"alert"] != nil){
238
- NSDictionary* alert = [aps objectForKey:@"alert"];
239
- if([alert objectForKey:@"title"] != nil){
240
- title = [alert objectForKey:@"title"];
241
- }
242
- if([alert objectForKey:@"body"] != nil){
243
- body = [alert objectForKey:@"body"];
244
- }
245
- if([aps objectForKey:@"sound"] != nil){
246
- sound = [aps objectForKey:@"sound"];
247
- }
248
- if([aps objectForKey:@"badge"] != nil){
249
- badge = [aps objectForKey:@"badge"];
250
- }
251
- }
252
-
253
- // Extract data notification keys
254
- if([messageData objectForKey:@"notification_title"] != nil){
255
- title = [messageData objectForKey:@"notification_title"];
256
- }
257
- if([messageData objectForKey:@"notification_body"] != nil){
258
- body = [messageData objectForKey:@"notification_body"];
259
- }
260
- if([messageData objectForKey:@"notification_ios_sound"] != nil){
261
- sound = [messageData objectForKey:@"notification_ios_sound"];
262
- }
263
- if([messageData objectForKey:@"notification_ios_badge"] != nil){
264
- badge = [messageData objectForKey:@"notification_ios_badge"];
265
- }
266
-
267
- if(title == nil || body == nil){
268
- return;
269
- }
270
-
271
- [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
272
- @try{
273
- if (settings.alertSetting == UNNotificationSettingEnabled) {
274
- UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
275
- objNotificationContent.title = [NSString localizedUserNotificationStringForKey:title arguments:nil];
276
- objNotificationContent.body = [NSString localizedUserNotificationStringForKey:body arguments:nil];
277
-
278
- NSDictionary* alert = [[NSDictionary alloc] initWithObjectsAndKeys:
279
- title, @"title",
280
- body, @"body"
281
- , nil];
282
- NSMutableDictionary* aps = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
283
- alert, @"alert",
284
- nil];
285
-
286
- if(![sound isKindOfClass:[NSString class]] || [sound isEqualToString:@"default"]){
287
- objNotificationContent.sound = [UNNotificationSound defaultSound];
288
- [aps setValue:sound forKey:@"sound"];
289
- }else if(sound != nil){
290
- objNotificationContent.sound = [UNNotificationSound soundNamed:sound];
291
- [aps setValue:sound forKey:@"sound"];
292
- }
293
-
294
- if(badge != nil){
295
- [aps setValue:badge forKey:@"badge"];
296
- }
297
-
298
- NSString* messageType = @"data";
299
- if([mutableUserInfo objectForKey:@"messageType"] != nil){
300
- messageType = [mutableUserInfo objectForKey:@"messageType"];
301
- }
302
-
303
- NSDictionary* userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:
304
- @"true", @"notification_foreground",
305
- messageType, @"messageType",
306
- aps, @"aps"
307
- , nil];
308
-
309
- objNotificationContent.userInfo = userInfo;
310
-
311
- UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1f repeats:NO];
312
- UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"local_notification" content:objNotificationContent trigger:trigger];
313
- [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
314
- if (!error) {
315
- [FirebasePlugin.firebasePlugin _logMessage:@"Local Notification succeeded"];
316
- } else {
317
- [FirebasePlugin.firebasePlugin _logError:[NSString stringWithFormat:@"Local Notification failed: %@", error.description]];
318
- }
319
- }];
320
- }else{
321
- [FirebasePlugin.firebasePlugin _logError:@"processMessageForForegroundNotification: cannot show notification as permission denied"];
322
- }
323
- }@catch (NSException *exception) {
324
- [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
325
- }
326
- }];
327
- }
328
-
329
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
330
- [FirebasePlugin.firebasePlugin _logError:[NSString stringWithFormat:@"didFailToRegisterForRemoteNotificationsWithError: %@", error.description]];
331
- }
332
-
333
- // Asks the delegate how to handle a notification that arrived while the app was running in the foreground
334
- // Called when an APS notification arrives when app is in foreground
335
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center
336
- willPresentNotification:(UNNotification *)notification
337
- withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
338
-
339
- @try{
340
-
341
- if (![notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class] && ![notification.request.trigger isKindOfClass:UNTimeIntervalNotificationTrigger.class]){
342
- if (_previousDelegate) {
343
- // bubbling notification
344
- [_previousDelegate userNotificationCenter:center
345
- willPresentNotification:notification
346
- withCompletionHandler:completionHandler];
347
- return;
348
- } else {
349
- [FirebasePlugin.firebasePlugin _logError:@"willPresentNotification: aborting as not a supported UNNotificationTrigger"];
350
- return;
351
- }
352
- }
353
-
354
- [[FIRMessaging messaging] appDidReceiveMessage:notification.request.content.userInfo];
355
-
356
- mutableUserInfo = [notification.request.content.userInfo mutableCopy];
357
-
358
- NSString* messageType = [mutableUserInfo objectForKey:@"messageType"];
359
- if(![messageType isEqualToString:@"data"]){
360
- [mutableUserInfo setValue:@"notification" forKey:@"messageType"];
361
- }
362
-
363
- // Print full message.
364
- [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"willPresentNotification: %@", mutableUserInfo]];
365
-
366
-
367
- NSDictionary* aps = [mutableUserInfo objectForKey:@"aps"];
368
- bool isContentAvailable = [[aps objectForKey:@"content-available"] isEqualToNumber:[NSNumber numberWithInt:1]];
369
- if(isContentAvailable){
370
- [FirebasePlugin.firebasePlugin _logError:@"willPresentNotification: aborting as content-available:1 so system notification will be shown"];
371
- return;
372
- }
373
-
374
- bool showForegroundNotification = [mutableUserInfo objectForKey:@"notification_foreground"];
375
- bool hasAlert = [aps objectForKey:@"alert"] != nil;
376
- bool hasBadge = [aps objectForKey:@"badge"] != nil;
377
- bool hasSound = [aps objectForKey:@"sound"] != nil;
378
-
379
- if(showForegroundNotification){
380
- [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"willPresentNotification: foreground notification alert=%@, badge=%@, sound=%@", hasAlert ? @"YES" : @"NO", hasBadge ? @"YES" : @"NO", hasSound ? @"YES" : @"NO"]];
381
- if(hasAlert && hasBadge && hasSound){
382
- completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound);
383
- }else if(hasAlert && hasBadge){
384
- completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionBadge);
385
- }else if(hasAlert && hasSound){
386
- completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);
387
- }else if(hasBadge && hasSound){
388
- completionHandler(UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound);
389
- }else if(hasAlert){
390
- completionHandler(UNNotificationPresentationOptionAlert);
391
- }else if(hasBadge){
392
- completionHandler(UNNotificationPresentationOptionBadge);
393
- }else if(hasSound){
394
- completionHandler(UNNotificationPresentationOptionSound);
395
- }
396
- }else{
397
- [FirebasePlugin.firebasePlugin _logMessage:@"willPresentNotification: foreground notification not set"];
398
- }
399
-
400
- if(![messageType isEqualToString:@"data"]){
401
- [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
402
- }
403
-
404
- }@catch (NSException *exception) {
405
- [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
406
- }
407
- [[AppICEPlugin appice] onHandleRemoteUNotification:mutableUserInfo];
408
-
409
- }
410
-
411
- // Asks the delegate to process the user's response to a delivered notification.
412
- // Called when user taps on system notification
413
- - (void) userNotificationCenter:(UNUserNotificationCenter *)center
414
- didReceiveNotificationResponse:(UNNotificationResponse *)response
415
- withCompletionHandler:(void (^)(void))completionHandler
416
- {
417
- @try{
418
-
419
- if (![response.notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class] && ![response.notification.request.trigger isKindOfClass:UNTimeIntervalNotificationTrigger.class]){
420
- if (_previousDelegate) {
421
- // bubbling event
422
- [_previousDelegate userNotificationCenter:center
423
- didReceiveNotificationResponse:response
424
- withCompletionHandler:completionHandler];
425
- return;
426
- } else {
427
- [FirebasePlugin.firebasePlugin _logMessage:@"didReceiveNotificationResponse: aborting as not a supported UNNotificationTrigger"];
428
- return;
429
- }
430
- }
431
-
432
- [[FIRMessaging messaging] appDidReceiveMessage:response.notification.request.content.userInfo];
433
-
434
- mutableUserInfo = [response.notification.request.content.userInfo mutableCopy];
435
-
436
- NSString* tap;
437
- if([self.applicationInBackground isEqual:[NSNumber numberWithBool:YES]]){
438
- tap = @"background";
439
- }else{
440
- tap = @"foreground";
441
-
442
- }
443
- [mutableUserInfo setValue:tap forKey:@"tap"];
444
- if([mutableUserInfo objectForKey:@"messageType"] == nil){
445
- [mutableUserInfo setValue:@"notification" forKey:@"messageType"];
446
- }
447
-
448
- // Dynamic Actions
449
- if (response.actionIdentifier && ![response.actionIdentifier isEqual:UNNotificationDefaultActionIdentifier]) {
450
- [mutableUserInfo setValue:response.actionIdentifier forKey:@"action"];
451
- }
452
-
453
- // Print full message.
454
- [FirebasePlugin.firebasePlugin _logInfo:[NSString stringWithFormat:@"didReceiveNotificationResponse: %@", mutableUserInfo]];
455
-
456
- [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
457
-
458
- [[AppICEPlugin appice] onHandleNotificationUResponse:mutableUserInfo];
459
-
460
-
461
- completionHandler();
462
-
463
- }@catch (NSException *exception) {
464
- [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
465
- }
466
- }
467
-
468
-
469
- // Apple Sign In
470
- - (void)authorizationController:(ASAuthorizationController *)controller
471
- didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
472
- @try{
473
- CDVPluginResult* pluginResult;
474
- NSString* errorMessage = nil;
475
- FIROAuthCredential *credential;
476
-
477
- if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
478
- ASAuthorizationAppleIDCredential *appleIDCredential = authorization.credential;
479
- NSString *rawNonce = [FirebasePlugin appleSignInNonce];
480
- if(rawNonce == nil){
481
- errorMessage = @"Invalid state: A login callback was received, but no login request was sent.";
482
- }else if (appleIDCredential.identityToken == nil) {
483
- errorMessage = @"Unable to fetch identity token.";
484
- }else{
485
- NSString *idToken = [[NSString alloc] initWithData:appleIDCredential.identityToken
486
- encoding:NSUTF8StringEncoding];
487
- if (idToken == nil) {
488
- errorMessage = [NSString stringWithFormat:@"Unable to serialize id token from data: %@", appleIDCredential.identityToken];
489
- }else{
490
- // Initialize a Firebase credential.
491
- credential = [FIROAuthProvider credentialWithProviderID:@"apple.com"
492
- IDToken:idToken
493
- rawNonce:rawNonce];
494
-
495
- NSNumber* key = [[FirebasePlugin firebasePlugin] saveAuthCredential:credential];
496
- NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
497
- [result setValue:@"true" forKey:@"instantVerification"];
498
- [result setValue:key forKey:@"id"];
499
- pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
500
- }
501
- }
502
- if(errorMessage != nil){
503
- [FirebasePlugin.firebasePlugin _logError:errorMessage];
504
- pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
505
- }
506
- if ([FirebasePlugin firebasePlugin].appleSignInCallbackId != nil) {
507
- [[FirebasePlugin firebasePlugin].commandDelegate sendPluginResult:pluginResult callbackId:[FirebasePlugin firebasePlugin].appleSignInCallbackId];
508
- }
509
- }
510
- }@catch (NSException *exception) {
511
- [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
512
- }
513
- }
514
-
515
- - (void)authorizationController:(ASAuthorizationController *)controller
516
- didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
517
- NSString* errorMessage = [NSString stringWithFormat:@"Sign in with Apple errored: %@", error];
518
- [FirebasePlugin.firebasePlugin _logError:errorMessage];
519
- if ([FirebasePlugin firebasePlugin].appleSignInCallbackId != nil) {
520
- CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
521
- [[FirebasePlugin firebasePlugin].commandDelegate sendPluginResult:pluginResult callbackId:[FirebasePlugin firebasePlugin].appleSignInCallbackId];
522
- }
523
- }
524
-
525
- - (nonnull ASPresentationAnchor)presentationAnchorForAuthorizationController:(nonnull ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
526
- return self.viewController.view.window;
527
- }
528
-
529
- @end
1
+ #import "AppDelegate+FirebasePlugin.h"
2
+ #import "FirebasePlugin.h"
3
+ #import "Firebase.h"
4
+ #import "AppICEPlugin.h"
5
+ #import <objc/runtime.h>
6
+
7
+
8
+ @import UserNotifications;
9
+ @import FirebaseFirestore;
10
+
11
+ // Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices running iOS 10 and above.
12
+ // Implement FIRMessagingDelegate to receive data message via FCM for devices running iOS 10 and above.
13
+ @interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate>
14
+ @end
15
+
16
+ #define kApplicationInBackgroundKey @"applicationInBackground"
17
+
18
+ @implementation AppDelegate (FirebasePlugin)
19
+
20
+ static AppDelegate* instance;
21
+ static id <UNUserNotificationCenterDelegate> _previousDelegate;
22
+
23
+ + (AppDelegate*) instance {
24
+ return instance;
25
+ }
26
+
27
+ static NSDictionary* mutableUserInfo;
28
+ static FIRAuthStateDidChangeListenerHandle authStateChangeListener;
29
+ static bool authStateChangeListenerInitialized = false;
30
+
31
+ + (void)load {
32
+ Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
33
+ Method swizzled = class_getInstanceMethod(self, @selector(application:swizzledDidFinishLaunchingWithOptions:));
34
+ method_exchangeImplementations(original, swizzled);
35
+ }
36
+
37
+ - (void)setApplicationInBackground:(NSNumber *)applicationInBackground {
38
+ objc_setAssociatedObject(self, kApplicationInBackgroundKey, applicationInBackground, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
39
+ }
40
+
41
+ - (NSNumber *)applicationInBackground {
42
+ return objc_getAssociatedObject(self, kApplicationInBackgroundKey);
43
+ }
44
+
45
+ - (BOOL)application:(UIApplication *)application swizzledDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
46
+ [self application:application swizzledDidFinishLaunchingWithOptions:launchOptions];
47
+
48
+ @try{
49
+ instance = self;
50
+
51
+ bool isFirebaseInitializedWithPlist = false;
52
+ if(![FIRApp defaultApp]) {
53
+ // get GoogleService-Info.plist file path
54
+ NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];
55
+
56
+ // if file is successfully found, use it
57
+ if(filePath){
58
+ [FirebasePlugin.firebasePlugin _logMessage:@"GoogleService-Info.plist found, setup: [FIRApp configureWithOptions]"];
59
+ // create firebase configure options passing .plist as content
60
+ FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
61
+
62
+ // configure FIRApp with options
63
+ [FIRApp configureWithOptions:options];
64
+
65
+ isFirebaseInitializedWithPlist = true;
66
+ }else{
67
+ // no .plist found, try default App
68
+ [FirebasePlugin.firebasePlugin _logError:@"GoogleService-Info.plist NOT FOUND, setup: [FIRApp defaultApp]"];
69
+ [FIRApp configure];
70
+ }
71
+ }else{
72
+ // Firebase SDK has already been initialised:
73
+ // Assume that another call (probably from another plugin) did so with the plist
74
+ isFirebaseInitializedWithPlist = true;
75
+ }
76
+
77
+ // Set UNUserNotificationCenter delegate
78
+ if ([UNUserNotificationCenter currentNotificationCenter].delegate != nil) {
79
+ _previousDelegate = [UNUserNotificationCenter currentNotificationCenter].delegate;
80
+ }
81
+ [UNUserNotificationCenter currentNotificationCenter].delegate = self;
82
+
83
+ // Set FCM messaging delegate
84
+ [FIRMessaging messaging].delegate = self;
85
+
86
+ // Setup Firestore
87
+ [FirebasePlugin setFirestore:[FIRFirestore firestore]];
88
+
89
+ // Setup Google SignIn
90
+ [GIDSignIn sharedInstance].clientID = [FIRApp defaultApp].options.clientID;
91
+ [GIDSignIn sharedInstance].delegate = self;
92
+
93
+ authStateChangeListener = [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
94
+ @try {
95
+ if(!authStateChangeListenerInitialized){
96
+ authStateChangeListenerInitialized = true;
97
+ }else{
98
+ [FirebasePlugin.firebasePlugin executeGlobalJavascript:[NSString stringWithFormat:@"FirebasePlugin._onAuthStateChange(%@)", (user != nil ? @"true": @"false")]];
99
+ }
100
+ }@catch (NSException *exception) {
101
+ [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
102
+ }
103
+ }];
104
+
105
+
106
+ self.applicationInBackground = @(YES);
107
+
108
+ }@catch (NSException *exception) {
109
+ [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
110
+ }
111
+
112
+ return YES;
113
+ }
114
+
115
+ - (void)applicationDidBecomeActive:(UIApplication *)application {
116
+ self.applicationInBackground = @(NO);
117
+ [FirebasePlugin.firebasePlugin _logMessage:@"Enter foreground"];
118
+ }
119
+
120
+ - (void)applicationDidEnterBackground:(UIApplication *)application {
121
+ self.applicationInBackground = @(YES);
122
+ [FirebasePlugin.firebasePlugin _logMessage:@"Enter background"];
123
+ }
124
+
125
+ # pragma mark - Google SignIn
126
+ - (void)signIn:(GIDSignIn *)signIn
127
+ didSignInForUser:(GIDGoogleUser *)user
128
+ withError:(NSError *)error {
129
+ @try{
130
+ CDVPluginResult* pluginResult;
131
+ if (error == nil) {
132
+ GIDAuthentication *authentication = user.authentication;
133
+ FIRAuthCredential *credential =
134
+ [FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken
135
+ accessToken:authentication.accessToken];
136
+
137
+ NSNumber* key = [[FirebasePlugin firebasePlugin] saveAuthCredential:credential];
138
+ NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
139
+ [result setValue:@"true" forKey:@"instantVerification"];
140
+ [result setValue:key forKey:@"id"];
141
+ pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
142
+ } else {
143
+ pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description];
144
+ }
145
+ if ([FirebasePlugin firebasePlugin].googleSignInCallbackId != nil) {
146
+ [[FirebasePlugin firebasePlugin].commandDelegate sendPluginResult:pluginResult callbackId:[FirebasePlugin firebasePlugin].googleSignInCallbackId];
147
+ }
148
+ }@catch (NSException *exception) {
149
+ [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
150
+ }
151
+ }
152
+
153
+ - (void)signIn:(GIDSignIn *)signIn
154
+ didDisconnectWithUser:(GIDGoogleUser *)user
155
+ withError:(NSError *)error {
156
+ NSString* msg = @"Google SignIn delegate: didDisconnectWithUser";
157
+ if(error != nil){
158
+ [FirebasePlugin.firebasePlugin _logError:[NSString stringWithFormat:@"%@: %@", msg, error]];
159
+ }else{
160
+ [FirebasePlugin.firebasePlugin _logMessage:msg];
161
+ }
162
+ }
163
+
164
+ # pragma mark - FIRMessagingDelegate
165
+ - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
166
+ @try{
167
+ [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"didReceiveRegistrationToken: %@", fcmToken]];
168
+ [FirebasePlugin.firebasePlugin sendToken:fcmToken];
169
+ }@catch (NSException *exception) {
170
+ [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
171
+ }
172
+ }
173
+
174
+ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
175
+ [FIRMessaging messaging].APNSToken = deviceToken;
176
+ [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken]];
177
+ [FirebasePlugin.firebasePlugin sendApnsToken:[FirebasePlugin.firebasePlugin hexadecimalStringFromData:deviceToken]];
178
+ [[AppICEPlugin appice] handleToken:deviceToken];
179
+
180
+ }
181
+
182
+ //Tells the app that a remote notification arrived that indicates there is data to be fetched.
183
+ // Called when a message arrives in the foreground and remote notifications permission has been granted
184
+ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
185
+ fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
186
+
187
+ @try{
188
+ [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
189
+ mutableUserInfo = [userInfo mutableCopy];
190
+ NSDictionary* aps = [mutableUserInfo objectForKey:@"aps"];
191
+ bool isContentAvailable = false;
192
+ if([aps objectForKey:@"alert"] != nil){
193
+ isContentAvailable = [[aps objectForKey:@"content-available"] isEqualToNumber:[NSNumber numberWithInt:1]];
194
+ [mutableUserInfo setValue:@"notification" forKey:@"messageType"];
195
+ NSString* tap;
196
+ if([self.applicationInBackground isEqual:[NSNumber numberWithBool:YES]] && !isContentAvailable){
197
+ tap = @"background";
198
+ }
199
+ [mutableUserInfo setValue:tap forKey:@"tap"];
200
+ }else{
201
+ [mutableUserInfo setValue:@"data" forKey:@"messageType"];
202
+ }
203
+
204
+ [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"didReceiveRemoteNotification: %@", mutableUserInfo]];
205
+
206
+ completionHandler(UIBackgroundFetchResultNewData);
207
+ if([self.applicationInBackground isEqual:[NSNumber numberWithBool:YES]] && isContentAvailable){
208
+ [FirebasePlugin.firebasePlugin _logError:@"didReceiveRemoteNotification: omitting foreground notification as content-available:1 so system notification will be shown"];
209
+ }else{
210
+ [self processMessageForForegroundNotification:mutableUserInfo];
211
+ }
212
+ if([self.applicationInBackground isEqual:[NSNumber numberWithBool:YES]] || !isContentAvailable){
213
+ [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
214
+ }
215
+ }@catch (NSException *exception) {
216
+ [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
217
+ }
218
+ [[AppICEPlugin appice] onHandleRemoteUNotification:userInfo];
219
+
220
+ }
221
+
222
+ // Scans a message for keys which indicate a notification should be shown.
223
+ // If found, extracts relevant keys and uses then to display a local notification
224
+ -(void)processMessageForForegroundNotification:(NSDictionary*)messageData {
225
+ bool showForegroundNotification = [messageData objectForKey:@"notification_foreground"];
226
+ if(!showForegroundNotification){
227
+ return;
228
+ }
229
+
230
+ NSString* title = nil;
231
+ NSString* body = nil;
232
+ NSString* sound = nil;
233
+ NSNumber* badge = nil;
234
+
235
+ // Extract APNS notification keys
236
+ NSDictionary* aps = [messageData objectForKey:@"aps"];
237
+ if([aps objectForKey:@"alert"] != nil){
238
+ NSDictionary* alert = [aps objectForKey:@"alert"];
239
+ if([alert objectForKey:@"title"] != nil){
240
+ title = [alert objectForKey:@"title"];
241
+ }
242
+ if([alert objectForKey:@"body"] != nil){
243
+ body = [alert objectForKey:@"body"];
244
+ }
245
+ if([aps objectForKey:@"sound"] != nil){
246
+ sound = [aps objectForKey:@"sound"];
247
+ }
248
+ if([aps objectForKey:@"badge"] != nil){
249
+ badge = [aps objectForKey:@"badge"];
250
+ }
251
+ }
252
+
253
+ // Extract data notification keys
254
+ if([messageData objectForKey:@"notification_title"] != nil){
255
+ title = [messageData objectForKey:@"notification_title"];
256
+ }
257
+ if([messageData objectForKey:@"notification_body"] != nil){
258
+ body = [messageData objectForKey:@"notification_body"];
259
+ }
260
+ if([messageData objectForKey:@"notification_ios_sound"] != nil){
261
+ sound = [messageData objectForKey:@"notification_ios_sound"];
262
+ }
263
+ if([messageData objectForKey:@"notification_ios_badge"] != nil){
264
+ badge = [messageData objectForKey:@"notification_ios_badge"];
265
+ }
266
+
267
+ if(title == nil || body == nil){
268
+ return;
269
+ }
270
+
271
+ [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
272
+ @try{
273
+ if (settings.alertSetting == UNNotificationSettingEnabled) {
274
+ UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
275
+ objNotificationContent.title = [NSString localizedUserNotificationStringForKey:title arguments:nil];
276
+ objNotificationContent.body = [NSString localizedUserNotificationStringForKey:body arguments:nil];
277
+
278
+ NSDictionary* alert = [[NSDictionary alloc] initWithObjectsAndKeys:
279
+ title, @"title",
280
+ body, @"body"
281
+ , nil];
282
+ NSMutableDictionary* aps = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
283
+ alert, @"alert",
284
+ nil];
285
+
286
+ if(![sound isKindOfClass:[NSString class]] || [sound isEqualToString:@"default"]){
287
+ objNotificationContent.sound = [UNNotificationSound defaultSound];
288
+ [aps setValue:sound forKey:@"sound"];
289
+ }else if(sound != nil){
290
+ objNotificationContent.sound = [UNNotificationSound soundNamed:sound];
291
+ [aps setValue:sound forKey:@"sound"];
292
+ }
293
+
294
+ if(badge != nil){
295
+ [aps setValue:badge forKey:@"badge"];
296
+ }
297
+
298
+ NSString* messageType = @"data";
299
+ if([mutableUserInfo objectForKey:@"messageType"] != nil){
300
+ messageType = [mutableUserInfo objectForKey:@"messageType"];
301
+ }
302
+
303
+ NSDictionary* userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:
304
+ @"true", @"notification_foreground",
305
+ messageType, @"messageType",
306
+ aps, @"aps"
307
+ , nil];
308
+
309
+ objNotificationContent.userInfo = userInfo;
310
+
311
+ UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1f repeats:NO];
312
+ UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"local_notification" content:objNotificationContent trigger:trigger];
313
+ [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
314
+ if (!error) {
315
+ [FirebasePlugin.firebasePlugin _logMessage:@"Local Notification succeeded"];
316
+ } else {
317
+ [FirebasePlugin.firebasePlugin _logError:[NSString stringWithFormat:@"Local Notification failed: %@", error.description]];
318
+ }
319
+ }];
320
+ }else{
321
+ [FirebasePlugin.firebasePlugin _logError:@"processMessageForForegroundNotification: cannot show notification as permission denied"];
322
+ }
323
+ }@catch (NSException *exception) {
324
+ [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
325
+ }
326
+ }];
327
+ }
328
+
329
+ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
330
+ [FirebasePlugin.firebasePlugin _logError:[NSString stringWithFormat:@"didFailToRegisterForRemoteNotificationsWithError: %@", error.description]];
331
+ }
332
+
333
+ // Asks the delegate how to handle a notification that arrived while the app was running in the foreground
334
+ // Called when an APS notification arrives when app is in foreground
335
+ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
336
+ willPresentNotification:(UNNotification *)notification
337
+ withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
338
+
339
+ @try{
340
+
341
+ if (![notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class] && ![notification.request.trigger isKindOfClass:UNTimeIntervalNotificationTrigger.class]){
342
+ if (_previousDelegate) {
343
+ // bubbling notification
344
+ [_previousDelegate userNotificationCenter:center
345
+ willPresentNotification:notification
346
+ withCompletionHandler:completionHandler];
347
+ return;
348
+ } else {
349
+ [FirebasePlugin.firebasePlugin _logError:@"willPresentNotification: aborting as not a supported UNNotificationTrigger"];
350
+ return;
351
+ }
352
+ }
353
+
354
+ [[FIRMessaging messaging] appDidReceiveMessage:notification.request.content.userInfo];
355
+
356
+ mutableUserInfo = [notification.request.content.userInfo mutableCopy];
357
+
358
+ NSString* messageType = [mutableUserInfo objectForKey:@"messageType"];
359
+ if(![messageType isEqualToString:@"data"]){
360
+ [mutableUserInfo setValue:@"notification" forKey:@"messageType"];
361
+ }
362
+
363
+ // Print full message.
364
+ [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"willPresentNotification: %@", mutableUserInfo]];
365
+
366
+
367
+ NSDictionary* aps = [mutableUserInfo objectForKey:@"aps"];
368
+ bool isContentAvailable = [[aps objectForKey:@"content-available"] isEqualToNumber:[NSNumber numberWithInt:1]];
369
+ if(isContentAvailable){
370
+ [FirebasePlugin.firebasePlugin _logError:@"willPresentNotification: aborting as content-available:1 so system notification will be shown"];
371
+ return;
372
+ }
373
+
374
+ bool showForegroundNotification = [mutableUserInfo objectForKey:@"notification_foreground"];
375
+ bool hasAlert = [aps objectForKey:@"alert"] != nil;
376
+ bool hasBadge = [aps objectForKey:@"badge"] != nil;
377
+ bool hasSound = [aps objectForKey:@"sound"] != nil;
378
+
379
+ if(showForegroundNotification){
380
+ [FirebasePlugin.firebasePlugin _logMessage:[NSString stringWithFormat:@"willPresentNotification: foreground notification alert=%@, badge=%@, sound=%@", hasAlert ? @"YES" : @"NO", hasBadge ? @"YES" : @"NO", hasSound ? @"YES" : @"NO"]];
381
+ if(hasAlert && hasBadge && hasSound){
382
+ completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound);
383
+ }else if(hasAlert && hasBadge){
384
+ completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionBadge);
385
+ }else if(hasAlert && hasSound){
386
+ completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);
387
+ }else if(hasBadge && hasSound){
388
+ completionHandler(UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound);
389
+ }else if(hasAlert){
390
+ completionHandler(UNNotificationPresentationOptionAlert);
391
+ }else if(hasBadge){
392
+ completionHandler(UNNotificationPresentationOptionBadge);
393
+ }else if(hasSound){
394
+ completionHandler(UNNotificationPresentationOptionSound);
395
+ }
396
+ }else{
397
+ [FirebasePlugin.firebasePlugin _logMessage:@"willPresentNotification: foreground notification not set"];
398
+ }
399
+
400
+ if(![messageType isEqualToString:@"data"]){
401
+ [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
402
+ }
403
+
404
+ }@catch (NSException *exception) {
405
+ [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
406
+ }
407
+ [[AppICEPlugin appice] onHandleRemoteUNotification:mutableUserInfo];
408
+
409
+ }
410
+
411
+ // Asks the delegate to process the user's response to a delivered notification.
412
+ // Called when user taps on system notification
413
+ - (void) userNotificationCenter:(UNUserNotificationCenter *)center
414
+ didReceiveNotificationResponse:(UNNotificationResponse *)response
415
+ withCompletionHandler:(void (^)(void))completionHandler
416
+ {
417
+ @try{
418
+
419
+ if (![response.notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class] && ![response.notification.request.trigger isKindOfClass:UNTimeIntervalNotificationTrigger.class]){
420
+ if (_previousDelegate) {
421
+ // bubbling event
422
+ [_previousDelegate userNotificationCenter:center
423
+ didReceiveNotificationResponse:response
424
+ withCompletionHandler:completionHandler];
425
+ return;
426
+ } else {
427
+ [FirebasePlugin.firebasePlugin _logMessage:@"didReceiveNotificationResponse: aborting as not a supported UNNotificationTrigger"];
428
+ return;
429
+ }
430
+ }
431
+
432
+ [[FIRMessaging messaging] appDidReceiveMessage:response.notification.request.content.userInfo];
433
+
434
+ mutableUserInfo = [response.notification.request.content.userInfo mutableCopy];
435
+
436
+ NSString* tap;
437
+ if([self.applicationInBackground isEqual:[NSNumber numberWithBool:YES]]){
438
+ tap = @"background";
439
+ }else{
440
+ tap = @"foreground";
441
+
442
+ }
443
+ [mutableUserInfo setValue:tap forKey:@"tap"];
444
+ if([mutableUserInfo objectForKey:@"messageType"] == nil){
445
+ [mutableUserInfo setValue:@"notification" forKey:@"messageType"];
446
+ }
447
+
448
+ // Dynamic Actions
449
+ if (response.actionIdentifier && ![response.actionIdentifier isEqual:UNNotificationDefaultActionIdentifier]) {
450
+ [mutableUserInfo setValue:response.actionIdentifier forKey:@"action"];
451
+ }
452
+
453
+ // Print full message.
454
+ [FirebasePlugin.firebasePlugin _logInfo:[NSString stringWithFormat:@"didReceiveNotificationResponse: %@", mutableUserInfo]];
455
+
456
+ [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
457
+
458
+ [[AppICEPlugin appice] onHandleNotificationUResponse:mutableUserInfo];
459
+
460
+
461
+ completionHandler();
462
+
463
+ }@catch (NSException *exception) {
464
+ [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
465
+ }
466
+ }
467
+
468
+
469
+ // Apple Sign In
470
+ - (void)authorizationController:(ASAuthorizationController *)controller
471
+ didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
472
+ @try{
473
+ CDVPluginResult* pluginResult;
474
+ NSString* errorMessage = nil;
475
+ FIROAuthCredential *credential;
476
+
477
+ if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
478
+ ASAuthorizationAppleIDCredential *appleIDCredential = authorization.credential;
479
+ NSString *rawNonce = [FirebasePlugin appleSignInNonce];
480
+ if(rawNonce == nil){
481
+ errorMessage = @"Invalid state: A login callback was received, but no login request was sent.";
482
+ }else if (appleIDCredential.identityToken == nil) {
483
+ errorMessage = @"Unable to fetch identity token.";
484
+ }else{
485
+ NSString *idToken = [[NSString alloc] initWithData:appleIDCredential.identityToken
486
+ encoding:NSUTF8StringEncoding];
487
+ if (idToken == nil) {
488
+ errorMessage = [NSString stringWithFormat:@"Unable to serialize id token from data: %@", appleIDCredential.identityToken];
489
+ }else{
490
+ // Initialize a Firebase credential.
491
+ credential = [FIROAuthProvider credentialWithProviderID:@"apple.com"
492
+ IDToken:idToken
493
+ rawNonce:rawNonce];
494
+
495
+ NSNumber* key = [[FirebasePlugin firebasePlugin] saveAuthCredential:credential];
496
+ NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
497
+ [result setValue:@"true" forKey:@"instantVerification"];
498
+ [result setValue:key forKey:@"id"];
499
+ pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
500
+ }
501
+ }
502
+ if(errorMessage != nil){
503
+ [FirebasePlugin.firebasePlugin _logError:errorMessage];
504
+ pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
505
+ }
506
+ if ([FirebasePlugin firebasePlugin].appleSignInCallbackId != nil) {
507
+ [[FirebasePlugin firebasePlugin].commandDelegate sendPluginResult:pluginResult callbackId:[FirebasePlugin firebasePlugin].appleSignInCallbackId];
508
+ }
509
+ }
510
+ }@catch (NSException *exception) {
511
+ [FirebasePlugin.firebasePlugin handlePluginExceptionWithoutContext:exception];
512
+ }
513
+ }
514
+
515
+ - (void)authorizationController:(ASAuthorizationController *)controller
516
+ didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
517
+ NSString* errorMessage = [NSString stringWithFormat:@"Sign in with Apple errored: %@", error];
518
+ [FirebasePlugin.firebasePlugin _logError:errorMessage];
519
+ if ([FirebasePlugin firebasePlugin].appleSignInCallbackId != nil) {
520
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
521
+ [[FirebasePlugin firebasePlugin].commandDelegate sendPluginResult:pluginResult callbackId:[FirebasePlugin firebasePlugin].appleSignInCallbackId];
522
+ }
523
+ }
524
+
525
+ - (nonnull ASPresentationAnchor)presentationAnchorForAuthorizationController:(nonnull ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
526
+ return self.viewController.view.window;
527
+ }
528
+
529
+ @end