forked from arnesson/cordova-plugin-firebase
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFirebasePlugin.m
692 lines (559 loc) · 28.8 KB
/
FirebasePlugin.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#import "FirebasePlugin.h"
#import <Cordova/CDV.h>
#import "AppDelegate.h"
#import "Firebase.h"
// #import <Fabric/Fabric.h>
// #import <Crashlytics/Crashlytics.h>
// @import FirebaseInstanceID;
// @import FirebaseMessaging;
@import FirebaseAnalytics;
@import FirebaseRemoteConfig;
@import FirebasePerformance;
@import FirebaseAuth;
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@import UserNotifications;
#endif
#ifndef NSFoundationVersionNumber_iOS_9_x_Max
#define NSFoundationVersionNumber_iOS_9_x_Max 1299
#endif
@implementation FirebasePlugin
@synthesize notificationCallbackId;
@synthesize tokenRefreshCallbackId;
@synthesize notificationStack;
@synthesize traces;
static NSInteger const kNotificationStackSize = 10;
static FirebasePlugin *firebasePlugin;
+ (FirebasePlugin *) firebasePlugin {
return firebasePlugin;
}
- (void)pluginInitialize {
NSLog(@"Starting Firebase plugin");
firebasePlugin = self;
// For Crashlytics: this enables the browser to correctly detect the error stack
// Ref: https://bugs.webkit.org/show_bug.cgi?id=154916
// Ref: https://stackoverflow.com/questions/36013645/setting-disable-web-security-and-allow-file-access-from-files-in-ios-wkwebvi
NSString* allowFileAccess = [self.commandDelegate.settings objectForKey:[@"FirebasePluginAllowFileAccess" lowercaseString]];
if ([self.webView isKindOfClass:WKWebView.class] && [allowFileAccess isEqualToString:@"true"]){
WKWebView *wkWebView = (WKWebView *) self.webView;
[wkWebView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
}
}
/*
- (void)getId:(CDVInvokedUrlCommand *)command {
__block CDVPluginResult *pluginResult;
FIRInstanceIDHandler handler = ^(NSString *_Nullable instID, NSError *_Nullable error) {
if (error) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:instID];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
};
[[FIRInstanceID instanceID] getIDWithHandler:handler];
}
*/
// DEPRECATED - alias of getToken
/*
- (void)getInstanceId:(CDVInvokedUrlCommand *)command {
[self getToken:command];
}
- (void)getToken:(CDVInvokedUrlCommand *)command {
[[FIRInstanceID instanceID] instanceIDWithHandler:^
(FIRInstanceIDResult * _Nullable result, NSError * _Nullable error) {
CDVPluginResult *pluginResult = nil;
if (error != nil) {
// NSLog(@"Error fetching remote instance ID: %@", error);
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: error.localizedDescription];
} else {
// NSLog(@"Remote instance ID token: %@", result.token);
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result.token];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
*/
- (void)hasPermission:(CDVInvokedUrlCommand *)command {
BOOL enabled = NO;
UIApplication *application = [UIApplication sharedApplication];
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
enabled = application.currentUserNotificationSettings.types != UIUserNotificationTypeNone;
} else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
enabled = application.enabledRemoteNotificationTypes != UIRemoteNotificationTypeNone;
#pragma GCC diagnostic pop
}
NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:1];
[message setObject:[NSNumber numberWithBool:enabled] forKey:@"isEnabled"];
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message];
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
}
- (void)grantPermission:(CDVInvokedUrlCommand *)command {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType notificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
#pragma GCC diagnostic pop
}
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
/*
#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) {
if (![NSThread isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
[[FIRMessaging messaging] setDelegate:self];
[[UIApplication sharedApplication] registerForRemoteNotifications];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: granted ? CDVCommandStatus_OK : CDVCommandStatus_ERROR];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
});
} else {
[[FIRMessaging messaging] setDelegate:self];
[[UIApplication sharedApplication] registerForRemoteNotifications];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}
];
#else
[[UIApplication sharedApplication] registerForRemoteNotifications];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
#endif
*/
return;
}
- (void)verifyPhoneNumber:(CDVInvokedUrlCommand *)command {
[self getVerificationID:command];
}
- (void)getVerificationID:(CDVInvokedUrlCommand *)command {
NSString* number = [command.arguments objectAtIndex:0];
[[FIRPhoneAuthProvider provider]
verifyPhoneNumber:number
UIDelegate:nil
completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
NSDictionary *message;
if (error) {
// Verification code not sent.
message = @{
@"code": [NSNumber numberWithInteger:error.code],
@"description": error.description == nil ? [NSNull null] : error.description
};
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:message];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
// Successful.
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:verificationID];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}
- (void)setBadgeNumber:(CDVInvokedUrlCommand *)command {
int number = [[command.arguments objectAtIndex:0] intValue];
[self.commandDelegate runInBackground:^{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:number];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)getBadgeNumber:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
long badge = [[UIApplication sharedApplication] applicationIconBadgeNumber];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:badge];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
/*
- (void)subscribe:(CDVInvokedUrlCommand *)command {
NSString* topic = [NSString stringWithFormat:@"/topics/%@", [command.arguments objectAtIndex:0]];
[[FIRMessaging messaging] subscribeToTopic: topic];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)unsubscribe:(CDVInvokedUrlCommand *)command {
NSString* topic = [NSString stringWithFormat:@"/topics/%@", [command.arguments objectAtIndex:0]];
[[FIRMessaging messaging] unsubscribeFromTopic: topic];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)unregister:(CDVInvokedUrlCommand *)command {
[[FIRInstanceID instanceID] deleteIDWithHandler:^void(NSError *_Nullable error) {
if (error) {
NSLog(@"Unable to delete instance");
} else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}
- (void)onNotificationOpen:(CDVInvokedUrlCommand *)command {
self.notificationCallbackId = command.callbackId;
if (self.notificationStack != nil && [self.notificationStack count]) {
for (NSDictionary *userInfo in self.notificationStack) {
[self sendNotification:userInfo];
}
[self.notificationStack removeAllObjects];
}
}
- (void)onTokenRefresh:(CDVInvokedUrlCommand *)command {
self.tokenRefreshCallbackId = command.callbackId;
[[FIRInstanceID instanceID] instanceIDWithHandler:^
(FIRInstanceIDResult * _Nullable result, NSError * _Nullable error) {
if (error == nil) {
NSString* currentToken = result.token;
if (currentToken != nil) {
[self sendToken:currentToken];
}
}
}];
}
- (void)sendNotification:(NSDictionary *)userInfo {
if (self.notificationCallbackId != nil) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:userInfo];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.notificationCallbackId];
} else {
if (!self.notificationStack) {
self.notificationStack = [[NSMutableArray alloc] init];
}
// stack notifications until a callback has been registered
[self.notificationStack addObject:userInfo];
if ([self.notificationStack count] >= kNotificationStackSize) {
[self.notificationStack removeLastObject];
}
}
}
*/
- (void)sendToken:(NSString *)token {
if (self.tokenRefreshCallbackId != nil) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:token];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.tokenRefreshCallbackId];
}
}
- (void)logEvent:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* name = [command.arguments objectAtIndex:0];
NSDictionary* parameters = [command.arguments objectAtIndex:1];
[FIRAnalytics logEventWithName:name parameters:parameters];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)logError:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* errorMessage = [command.arguments objectAtIndex:0];
// CLSNSLog(@"%@", errorMessage);
[[FIRCrashlytics crashlytics] log:errorMessage];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)logMessage:(CDVInvokedUrlCommand*)command{
[self.commandDelegate runInBackground:^{
NSString* message = [command argumentAtIndex:0 withDefault:@""];
if(message)
{
// CLSNSLog(@"%@",message);
[[FIRCrashlytics crashlytics] log:message];
}
}];
}
-(void)sendNonFatalCrash:(CDVInvokedUrlCommand*)command{
[self.commandDelegate runInBackground:^{
// Ref: https://firebase.google.com/docs/reference/swift/crashlytics/api/reference/Classes#/c:objc(cs)CLSStackFrame
NSString *message = NSLocalizedString([command argumentAtIndex:0 withDefault:@"Unknown error"], nil);
NSArray *lines = [command argumentAtIndex:1 withDefault:[[NSArray alloc] init] ];
NSString *domain = NSLocalizedString([command argumentAtIndex:2 withDefault:[[NSBundle mainBundle] bundleIdentifier]], nil);
NSMutableArray *stack = [[NSMutableArray alloc] init];
NSMutableArray<FIRStackFrame *> *stackTrace = [[NSMutableArray<FIRStackFrame *> alloc] init];
for (NSDictionary *data in lines) {
NSString *filename = [data valueForKey:@"fileName"];
NSInteger lineNumber = [[data valueForKey:@"lineNumber"] integerValue];
NSInteger offset = [[data valueForKey:@"columnNumber"] integerValue];
NSString *symbol = [data valueForKey:@"source"];
NSString *library = [data valueForKey:@"functionName"];
// int *address = [data valueForKey:@"fileName"];
NSArray *arrayOfSymbol = [symbol componentsSeparatedByString:@"@"];
if (arrayOfSymbol.count > 0 ){
symbol = arrayOfSymbol[0];
}
FIRStackFrame *line = [FIRStackFrame stackFrameWithSymbol:symbol file:filename line:lineNumber];
[stackTrace addObject:line];
}
FIRExceptionModel *model = [FIRExceptionModel exceptionModelWithName:@"JavascriptException" reason:message];
model.stackTrace = [stackTrace copy];// stackTrace;
[[FIRCrashlytics crashlytics] recordExceptionModel:model];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)recordError:(CDVInvokedUrlCommand*)command{
[self.commandDelegate runInBackground:^{
// NSString *description = NSLocalizedString([command argumentAtIndex:0 withDefault:@"No Message Provided"], nil);
// NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: description };
NSDictionary* userInfo = [command.arguments objectAtIndex:0];
NSNumber *defaultCode = [NSNumber numberWithInt:-1];
int code = [[command argumentAtIndex:1 withDefault:defaultCode] intValue];
NSString *domain = NSLocalizedString([command argumentAtIndex:2 withDefault:[[NSBundle mainBundle] bundleIdentifier]], nil); // [[NSBundle mainBundle] bundleIdentifier];
NSError *error = [NSError errorWithDomain: domain code: code userInfo: userInfo];
[[FIRCrashlytics crashlytics] recordError:error];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)sendCrash:(CDVInvokedUrlCommand*)command{
// [[Crashlytics sharedInstance] crash];
@[][1];
}
- (void)setCrashlyticsUserId:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* userId = [command.arguments objectAtIndex:0];
[[FIRCrashlytics crashlytics] setUserID:userId];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setScreenName:(CDVInvokedUrlCommand *)command {
NSString* name = [command.arguments objectAtIndex:0];
[FIRAnalytics setScreenName:name screenClass:NULL];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setUserId:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* id = [command.arguments objectAtIndex:0];
[FIRAnalytics setUserID:id];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setUserProperty:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* name = [command.arguments objectAtIndex:0];
NSString* value = [command.arguments objectAtIndex:1];
[FIRAnalytics setUserPropertyString:value forName:name];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)fetch:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
FIRRemoteConfig* remoteConfig = [FIRRemoteConfig remoteConfig];
if ([command.arguments count] > 0) {
int expirationDuration = [[command.arguments objectAtIndex:0] intValue];
[remoteConfig fetchWithExpirationDuration:expirationDuration completionHandler:^(FIRRemoteConfigFetchStatus status, NSError * _Nullable error) {
CDVPluginResult *pluginResult;
if (status == FIRRemoteConfigFetchStatusSuccess) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
} else {
[remoteConfig fetchWithCompletionHandler:^(FIRRemoteConfigFetchStatus status, NSError * _Nullable error) {
CDVPluginResult *pluginResult;
if (status == FIRRemoteConfigFetchStatusSuccess) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
}];
}
- (void)activateFetched:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
FIRRemoteConfig* remoteConfig = [FIRRemoteConfig remoteConfig];
BOOL activated = [remoteConfig activateFetched];
CDVPluginResult *pluginResult;
if (activated) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)getValue:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* key = [command.arguments objectAtIndex:0];
FIRRemoteConfig* remoteConfig = [FIRRemoteConfig remoteConfig];
NSString* value = remoteConfig[key].stringValue;
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
//
// Performace
//
- (void)isPerformanceEnabled:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool: [FIRPerformance sharedInstance].isInstrumentationEnabled && [FIRPerformance sharedInstance].isDataCollectionEnabled ];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)enabeldPerformance:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSNumber *enabled = [command.arguments objectAtIndex:0];
if([enabled isEqual: @(YES)]){
[FIRPerformance sharedInstance].instrumentationEnabled = YES;
[FIRPerformance sharedInstance].dataCollectionEnabled = YES;
}else{
[FIRPerformance sharedInstance].instrumentationEnabled = NO;
[FIRPerformance sharedInstance].dataCollectionEnabled = NO;
}
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)startTrace:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* traceName = [command.arguments objectAtIndex:0];
FIRTrace *trace = [self.traces objectForKey:traceName];
if ( self.traces == nil) {
self.traces = [NSMutableDictionary new];
}
if (trace == nil) {
trace = [FIRPerformance startTraceWithName:traceName];
[self.traces setObject:trace forKey:traceName ];
}
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)incrementMetric:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* traceName = [command.arguments objectAtIndex:0];
NSString* counterNamed = [command.arguments objectAtIndex:1];
int64_t* increment = (int64_t)[command.arguments objectAtIndex:2];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
FIRTrace *trace = (FIRTrace*)[self.traces objectForKey:traceName];
if (trace != nil) {
[trace incrementMetric:counterNamed byInt:increment];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Trace not found"];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)stopTrace:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* traceName = [command.arguments objectAtIndex:0];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
FIRTrace *trace = [self.traces objectForKey:traceName];
if (trace != nil) {
[trace stop];
[self.traces removeObjectForKey:traceName];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Trace not found"];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
-(void)startTraceHTTP:(CDVInvokedUrlCommand *)command{
[self.commandDelegate runInBackground:^{
if ( self.httpTraces == nil) {
self.httpTraces = [NSMutableDictionary new];
}
NSString *url = [command.arguments objectAtIndex:0];
NSString *method = [command.arguments objectAtIndex:1];
long requestPayloadSize = [[command.arguments objectAtIndex:2] longValue];
method = [method uppercaseString];
FIRHTTPMethod aMethod = false;
if ( [method isEqualToString:@"GET"] ){
aMethod = FIRHTTPMethodGET;
}else if ( [method isEqualToString:@"PUT"] ){
aMethod = FIRHTTPMethodPUT;
}else if ( [method isEqualToString:@"POST"] ){
aMethod = FIRHTTPMethodPOST;
}else if ( [method isEqualToString:@"DELETE"] ){
aMethod = FIRHTTPMethodDELETE;
}else if ( [method isEqualToString:@"HEAD"] ){
aMethod = FIRHTTPMethodHEAD;
}else if ( [method isEqualToString:@"PATCH"] ){
aMethod = FIRHTTPMethodPATCH;
}else if ( [method isEqualToString:@"OPTIONS"] ){
aMethod = FIRHTTPMethodOPTIONS;
}else if ( [method isEqualToString:@"TRACE"] ){
aMethod = FIRHTTPMethodTRACE;
}else if ( [method isEqualToString:@"CONNECT"] ){
aMethod = FIRHTTPMethodCONNECT;
}
@try {
FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:[NSURL URLWithString:url] HTTPMethod:aMethod];
if ( requestPayloadSize > 0 )
[metric setRequestPayloadSize:requestPayloadSize];
[metric start];
NSString *traceName = [metric description];
[self.httpTraces setObject:metric forKey:traceName ];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:traceName];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@catch (NSException *exception) {
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"The HTTP method is not compatible"] callbackId:command.callbackId];
}
}];
}
-(void)stopTraceHTTP:(CDVInvokedUrlCommand *)command{
[self.commandDelegate runInBackground:^{
NSString *traceId = [command.arguments objectAtIndex:0];
NSInteger statusCode = [[command.arguments objectAtIndex:1] integerValue];
NSString *contentType = [command.arguments objectAtIndex:2];
long responsePayloadSize = [[command.arguments objectAtIndex:3] longValue];
FIRHTTPMetric *metric = [self.httpTraces objectForKey:traceId];
if ( metric != nil ){
[metric setResponseCode:statusCode];
[metric setResponseContentType:contentType];
if ( responsePayloadSize > 0 )
[metric setResponsePayloadSize:responsePayloadSize];
[metric stop];
[self.httpTraces removeObjectForKey:traceId];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}else{
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Trace not found"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}
- (void)setAnalyticsCollectionEnabled:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
BOOL enabled = [[command argumentAtIndex:0] boolValue];
// [[FIRAnalyticsConfiguration sharedInstance] setAnalyticsCollectionEnabled:enabled];
[FIRAnalytics setAnalyticsCollectionEnabled:enabled];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setPerformanceCollectionEnabled:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
BOOL enabled = [[command argumentAtIndex:0] boolValue];
[[FIRPerformance sharedInstance] setDataCollectionEnabled:enabled];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)clearAllNotifications:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
@end