-
Notifications
You must be signed in to change notification settings - Fork 29
/
braze_plugin.dart
1009 lines (876 loc) · 30.6 KB
/
braze_plugin.dart
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import 'dart:async';
import 'dart:convert' as json;
import 'package:flutter/services.dart';
/* Custom configuration keys */
const String replayCallbacksConfigKey = 'ReplayCallbacksKey';
class BrazePlugin {
static const MethodChannel _channel = const MethodChannel('braze_plugin');
Map<String, bool>? _brazeCustomConfigs;
Function(BrazeSdkAuthenticationError)? _brazeSdkAuthenticationErrorHandler;
Function(BrazeInAppMessage)? _brazeInAppMessageHandler;
final List<BrazeInAppMessage> _queuedInAppMessages = [];
Function(List<BrazeContentCard>)? _brazeContentCardHandler;
final List<BrazeContentCard> _queuedContentCards = [];
/// Broadcast stream to listen for in-app messages.
StreamController<BrazeInAppMessage> inAppMessageStreamController =
StreamController<BrazeInAppMessage>.broadcast();
/// Broadcast stream to listen for content cards.
StreamController<List<BrazeContentCard>> contentCardsStreamController =
StreamController<List<BrazeContentCard>>.broadcast();
BrazePlugin(
{Function(BrazeInAppMessage)? inAppMessageHandler,
Function(BrazeSdkAuthenticationError)? brazeSdkAuthenticationErrorHandler,
Function(List<BrazeContentCard>)? contentCardsHandler,
Map<String, bool>? customConfigs}) {
_brazeInAppMessageHandler = inAppMessageHandler;
_brazeSdkAuthenticationErrorHandler = brazeSdkAuthenticationErrorHandler;
_brazeContentCardHandler = contentCardsHandler;
_brazeCustomConfigs = customConfigs;
// Called after setting up plugin settings
_channel.setMethodCallHandler(_handleBrazeData);
}
/// Subscribes to the stream of in-app messages and calls [onEvent] when it
/// receives an in-app message.
StreamSubscription subscribeToInAppMessages(
void Function(BrazeInAppMessage) onEvent) {
if (_replayCallbacksConfigEnabled() && _queuedInAppMessages.isNotEmpty) {
print(
"Replaying stream onEvent for previously queued Braze in-app messages.");
_queuedInAppMessages.forEach((message) => onEvent(message));
_queuedInAppMessages.clear();
}
StreamSubscription subscription =
inAppMessageStreamController.stream.listen(onEvent);
return subscription;
}
/// Subscribes to the stream of content cards and calls [onEvent] when it
/// receives the list of content cards.
StreamSubscription subscribeToContentCards(
void Function(List<BrazeContentCard>) onEvent) {
if (_replayCallbacksConfigEnabled() && _queuedContentCards.isNotEmpty) {
print(
"Replaying stream onEvent for previously queued Braze content cards.");
onEvent(_queuedContentCards);
_queuedContentCards.clear();
}
StreamSubscription subscription =
contentCardsStreamController.stream.listen(onEvent);
return subscription;
}
/// Sets a callback to receive in-app message data from Braze.
@Deprecated(
'Use subscribeToInAppMessages(void onEvent(List<BrazeContentCard> contentCard)) instead.')
void setBrazeInAppMessageCallback(Function(BrazeInAppMessage) callback) {
_brazeInAppMessageHandler = callback;
if (_replayCallbacksConfigEnabled() && _queuedInAppMessages.isNotEmpty) {
print("Replaying callback on previously queued Braze in-app messages.");
_queuedInAppMessages.forEach((message) => callback(message));
_queuedInAppMessages.clear();
}
}
/// Sets a callback to receive in-app message data from Braze.
void setBrazeSdkAuthenticationErrorCallback(
Function(BrazeSdkAuthenticationError) callback) {
_channel.invokeMethod('setSdkAuthenticationDelegate');
_brazeSdkAuthenticationErrorHandler = callback;
}
/// Sets a callback to receive Content Card data from Braze.
@Deprecated(
'Use subscribeToContentCards(void onEvent(List<BrazeContentCard> contentCard)) instead.')
void setBrazeContentCardsCallback(Function(List<BrazeContentCard>) callback) {
_brazeContentCardHandler = callback;
if (_replayCallbacksConfigEnabled() && _queuedContentCards.isNotEmpty) {
print("Replaying callback on previously queued Braze content cards.");
callback(_queuedContentCards);
_queuedContentCards.clear();
}
}
/// Changes the current Braze userId.
/// If [sdkAuthSignature] is present, passes that token to the native layer.
///
/// See the Braze public docs for more info around the SDK Authentication feature.
void changeUser(String userId, {String? sdkAuthSignature}) {
final Map<String, dynamic> params = <String, dynamic>{
"userId": userId,
};
if (sdkAuthSignature != null) {
params["sdkAuthSignature"] = sdkAuthSignature;
}
_channel.invokeMethod('changeUser', params);
}
void setSdkAuthenticationSignature(String? sdkAuthSignature) {
final Map<String, dynamic> params = <String, dynamic>{
"sdkAuthSignature": sdkAuthSignature
};
_channel.invokeMethod('setSdkAuthenticationSignature', params);
}
/// Logs a click for the provided Content Card data.
void logContentCardClicked(BrazeContentCard contentCard) {
final Map<String, dynamic> params = <String, dynamic>{
"contentCardString": contentCard.contentCardJsonString
};
_channel.invokeMethod('logContentCardClicked', params);
}
/// Logs an impression for the provided Content Card data.
void logContentCardImpression(BrazeContentCard contentCard) {
final Map<String, dynamic> params = <String, dynamic>{
"contentCardString": contentCard.contentCardJsonString
};
_channel.invokeMethod('logContentCardImpression', params);
}
/// Logs dismissal for the provided Content Card data.
void logContentCardDismissed(BrazeContentCard contentCard) {
final Map<String, dynamic> params = <String, dynamic>{
"contentCardString": contentCard.contentCardJsonString
};
_channel.invokeMethod('logContentCardDismissed', params);
}
/// Logs a click for the provided in-app message data.
void logInAppMessageClicked(BrazeInAppMessage inAppMessage) {
final Map<String, dynamic> params = <String, dynamic>{
"inAppMessageString": inAppMessage.inAppMessageJsonString
};
_channel.invokeMethod('logInAppMessageClicked', params);
}
/// Logs an impression for the provided in-app message data.
void logInAppMessageImpression(BrazeInAppMessage inAppMessage) {
final Map<String, dynamic> params = <String, dynamic>{
"inAppMessageString": inAppMessage.inAppMessageJsonString
};
_channel.invokeMethod('logInAppMessageImpression', params);
}
/// Logs a button click for the provided in-app message button data.
void logInAppMessageButtonClicked(
BrazeInAppMessage inAppMessage, int buttonId) {
final Map<String, dynamic> params = <String, dynamic>{
"inAppMessageString": inAppMessage.inAppMessageJsonString,
"buttonId": buttonId
};
_channel.invokeMethod('logInAppMessageButtonClicked', params);
}
/// Add alias for current user.
void addAlias(String aliasName, String aliasLabel) {
final Map<String, dynamic> params = <String, dynamic>{
'aliasName': aliasName,
'aliasLabel': aliasLabel
};
_channel.invokeMethod('addAlias', params);
}
/// Logs a custom event to Braze.
void logCustomEvent(String eventName, {Map<String, dynamic>? properties}) {
final Map<String, dynamic> params = <String, dynamic>{
"eventName": eventName,
};
if (properties != null) {
// Omits entry when properties is null
params["properties"] = properties;
}
_channel.invokeMethod('logCustomEvent', params);
}
/// Logs a custom event to Braze.
@Deprecated('Use logCustomEvent(eventName, properties: properties) instead.')
void logCustomEventWithProperties(
String eventName, Map<String, dynamic> properties) {
final Map<String, dynamic> params = <String, dynamic>{
"eventName": eventName,
"properties": properties
};
_channel.invokeMethod('logCustomEvent', params);
}
/// Logs a purchase event to Braze.
void logPurchase(
String productId, String currencyCode, double price, int quantity,
{Map<String, dynamic>? properties}) {
final Map<String, dynamic> params = <String, dynamic>{
"productId": productId,
"currencyCode": currencyCode,
"price": price,
"quantity": quantity,
};
if (properties != null) {
// Omits entry when properties is null
params["properties"] = properties;
}
_channel.invokeMethod('logPurchase', params);
}
/// Logs a purchase event to Braze.
@Deprecated(
'Use logPurchase(productId, currencyCode, price, quantity, properties: properties) instead.')
void logPurchaseWithProperties(String productId, String currencyCode,
double price, int quantity, Map<String, dynamic> properties) {
final Map<String, dynamic> params = <String, dynamic>{
"productId": productId,
"currencyCode": currencyCode,
"price": price,
"quantity": quantity,
"properties": properties
};
_channel.invokeMethod('logPurchase', params);
}
/// Adds an element to a custom attribute array.
void addToCustomAttributeArray(String key, String value) {
final Map<String, dynamic> params = <String, dynamic>{
'key': key,
'value': value
};
_channel.invokeMethod('addToCustomAttributeArray', params);
}
/// Removes an element from a custom attribute array.
void removeFromCustomAttributeArray(String key, String value) {
final Map<String, dynamic> params = <String, dynamic>{
'key': key,
'value': value
};
_channel.invokeMethod('removeFromCustomAttributeArray', params);
}
/// Sets a string typed custom attribute.
void setStringCustomUserAttribute(String key, String value) {
final Map<String, dynamic> params = <String, dynamic>{
'key': key,
'value': value
};
_channel.invokeMethod('setStringCustomUserAttribute', params);
}
/// Sets a double typed custom attribute.
void setDoubleCustomUserAttribute(String key, double value) {
final Map<String, dynamic> params = <String, dynamic>{
'key': key,
'value': value
};
_channel.invokeMethod('setDoubleCustomUserAttribute', params);
}
/// Sets a boolean typed custom attribute.
void setBoolCustomUserAttribute(String key, bool value) {
final Map<String, dynamic> params = <String, dynamic>{
'key': key,
'value': value
};
_channel.invokeMethod('setBoolCustomUserAttribute', params);
}
/// Sets a integer typed custom attribute.
void setIntCustomUserAttribute(String key, int value) {
final Map<String, dynamic> params = <String, dynamic>{
'key': key,
'value': value
};
_channel.invokeMethod('setIntCustomUserAttribute', params);
}
/// Increments an integer typed custom attribute.
void incrementCustomUserAttribute(String key, int value) {
final Map<String, dynamic> params = <String, dynamic>{
'key': key,
'value': value
};
_channel.invokeMethod('incrementCustomUserAttribute', params);
}
/// Sets a location custom attribute.
void setLocationCustomAttribute(String key, double lat, double long) {
final Map<String, dynamic> params = <String, dynamic>{
'key': key,
'lat': lat,
'long': long
};
_channel.invokeMethod('setLocationCustomAttribute', params);
}
/// Sets a date custom attribute.
void setDateCustomUserAttribute(String key, DateTime value) {
final Map<String, dynamic> params = <String, dynamic>{
'key': key,
'value': value.millisecondsSinceEpoch ~/ 1000
};
_channel.invokeMethod('setDateCustomUserAttribute', params);
}
/// Unsets a custom attribute.
void unsetCustomUserAttribute(String key) {
_callStringMethod('unsetCustomUserAttribute', 'key', key);
}
/// Sets the first name default user attribute.
void setFirstName(String firstName) {
_callStringMethod('setFirstName', 'firstName', firstName);
}
/// Sets the last name default user attribute.
void setLastName(String lastName) {
_callStringMethod('setLastName', 'lastName', lastName);
}
/// Sets the email default user attribute.
/// Pass in `null` to unset the user's email.
void setEmail(String? email) {
_callStringMethod('setEmail', 'email', email);
}
/// Sets the dob default user attribute.
void setDateOfBirth(int year, int month, int day) {
final Map<String, dynamic> params = <String, dynamic>{
'year': year,
'month': month,
'day': day
};
_channel.invokeMethod('setDateOfBirth', params);
}
/// Sets the gender default user attribute.
void setGender(String gender) {
_callStringMethod('setGender', 'gender', gender);
}
/// Sets the language default user attribute.
void setLanguage(String language) {
_callStringMethod('setLanguage', 'language', language);
}
/// Sets the country default user attribute.
void setCountry(String country) {
_callStringMethod('setCountry', 'country', country);
}
/// Sets the home city default user attribute.
void setHomeCity(String homeCity) {
_callStringMethod('setHomeCity', 'homeCity', homeCity);
}
/// Sets the phone number default user attribute.
void setPhoneNumber(String phoneNumber) {
_callStringMethod('setPhoneNumber', 'phoneNumber', phoneNumber);
}
/// Sets attribution data.
void setAttributionData(
String? network, String? campaign, String? adGroup, String? creative) {
final Map<String, dynamic> params = <String, dynamic>{
'network': network,
'campaign': campaign,
'adGroup': adGroup,
'creative': creative
};
_channel.invokeMethod('setAttributionData', params);
}
/// Registers a push token for the current Android device with Braze.
/// - No-op on iOS.
void registerAndroidPushToken(String pushToken) {
_callStringMethod('registerAndroidPushToken', 'pushToken', pushToken);
}
/// Requests an immediate data flush.
void requestImmediateDataFlush() {
_channel.invokeMethod('requestImmediateDataFlush');
}
/// Wipes Data on board the SDK. Please consult iOS and Android-specific
/// implementation details before using.
void wipeData() {
_channel.invokeMethod('wipeData');
}
/// Refreshes Content Cards.
void requestContentCardsRefresh() {
_channel.invokeMethod('requestContentCardsRefresh');
}
/// Launches Content Card Feed.
void launchContentCards() {
_channel.invokeMethod('launchContentCards');
}
/// Requests location initialization.
void requestLocationInitialization() {
_channel.invokeMethod('requestLocationInitialization');
}
/// Sets the last known location.
void setLastKnownLocation(
{required double latitude,
required double longitude,
double? altitude,
double? accuracy,
double? verticalAccuracy}) {
final Map<String, dynamic> params = <String, dynamic>{
'latitude': latitude,
'longitude': longitude,
'accuracy': accuracy ?? 0.0,
};
if (altitude != null) {
params["altitude"] = altitude;
}
if (verticalAccuracy != null) {
params["verticalAccuracy"] = verticalAccuracy;
}
_channel.invokeMethod('setLastKnownLocation', params);
}
/// Enables the SDK. Please consult iOS and Android-specific
/// implementation details before using.
void enableSDK() {
_channel.invokeMethod('enableSDK');
}
/// Disables the SDK. Please consult iOS and Android-specific
/// implementation details before using.
void disableSDK() {
_channel.invokeMethod('disableSDK');
}
/// Sets push subscription state for the current user.
void setPushNotificationSubscriptionType(SubscriptionType type) {
final Map<String, dynamic> params = <String, dynamic>{
'type': type.toString()
};
_channel.invokeMethod('setPushNotificationSubscriptionType', params);
}
/// Sets email subscription state for the current user.
void setEmailNotificationSubscriptionType(SubscriptionType type) {
final Map<String, dynamic> params = <String, dynamic>{
'type': type.toString()
};
_channel.invokeMethod('setEmailNotificationSubscriptionType', params);
}
/// Adds the user to a Subscription Group using the group's UUID provided
/// in the Braze dashboard.
void addToSubscriptionGroup(String groupId) {
final Map<String, dynamic> params = <String, dynamic>{'groupId': groupId};
_channel.invokeMethod('addToSubscriptionGroup', params);
}
/// Removes the user from a Subscription Group using the group's UUID provided
/// in the Braze dashboard.
void removeFromSubscriptionGroup(String groupId) {
final Map<String, dynamic> params = <String, dynamic>{'groupId': groupId};
_channel.invokeMethod('removeFromSubscriptionGroup', params);
}
/// Gets the install tracking id.
Future<String> getInstallTrackingId() {
return _channel
.invokeMethod('getInstallTrackingId')
.then<String>((dynamic result) => result);
}
/// Sets Google Advertising Id for the current user.
/// - No-op on iOS.
void setGoogleAdvertisingId(String id, bool adTrackingEnabled) {
final Map<String, dynamic> params = <String, dynamic>{
"id": id,
"adTrackingEnabled": adTrackingEnabled
};
_channel.invokeMethod('setGoogleAdvertisingId', params);
}
void _callStringMethod(String methodName, String paramName, String? value) {
final Map<String, dynamic> params = <String, dynamic>{paramName: value};
_channel.invokeMethod(methodName, params);
}
Future<dynamic> _handleBrazeData(MethodCall call) async {
switch (call.method) {
case "handleBrazeInAppMessage":
final Map<dynamic, dynamic> argumentsMap = call.arguments;
String? inAppMessageString = argumentsMap['inAppMessage'];
if (inAppMessageString == null) {
print("Invalid input. Missing value for key 'inAppMessage'.");
return Future<void>.value();
}
final inAppMessage = BrazeInAppMessage(inAppMessageString);
if (_brazeInAppMessageHandler != null) {
_brazeInAppMessageHandler!(inAppMessage);
} else if (_replayCallbacksConfigEnabled()) {
print("Braze in-app message callback not present. Adding to queue.");
_queuedInAppMessages.add(inAppMessage);
}
// Add valid in-app message to the stream.
inAppMessageStreamController.add(inAppMessage);
return Future<void>.value();
case "handleBrazeContentCards":
final Map<dynamic, dynamic> argumentsMap = call.arguments;
List<BrazeContentCard> brazeCards = [];
for (dynamic card in argumentsMap['contentCards']) {
brazeCards.add(BrazeContentCard(card));
}
if (_brazeContentCardHandler != null) {
_brazeContentCardHandler!(brazeCards);
} else if (_replayCallbacksConfigEnabled()) {
print(
"Braze content card callback not present. Removing any queued cards and adding only the recent refresh.");
_queuedContentCards.clear();
_queuedContentCards.addAll(brazeCards);
}
// Add valid list of content cards to the stream.
contentCardsStreamController.add(brazeCards);
return Future<void>.value();
case "handleSdkAuthenticationError":
final Map<dynamic, dynamic> argumentsMap = call.arguments;
String? sdkAuthenticationErrorString =
argumentsMap['sdkAuthenticationError'];
if (sdkAuthenticationErrorString == null) {
print(
"Invalid input. Missing value for key 'sdkAuthenticationError'.");
return Future<void>.value();
}
final sdkAuthenticationError =
BrazeSdkAuthenticationError(sdkAuthenticationErrorString);
if (_brazeSdkAuthenticationErrorHandler != null) {
_brazeSdkAuthenticationErrorHandler!(sdkAuthenticationError);
} else {
print("Braze SDK Authentication error callback not present.");
}
return Future<void>.value();
default:
print("Unknown method ${call.method} called. Doing nothing.");
return Future<void>.value();
}
}
/* Braze Plugin custom configurations */
bool _replayCallbacksConfigEnabled() {
return _brazeCustomConfigs?[replayCallbacksConfigKey] == true;
}
}
enum SubscriptionType { subscribed, unsubscribed, opted_in }
/// Braze in-app message dismiss types
///
/// Messages with a dismiss type of "swipe" should appear until manually
/// dismissed by the user. Messages with a dismiss type of "auto_dismiss"
/// should dismiss automatically after the amount of time specified in the
/// "duration" field of the message's BrazeInAppMessage object elapses.
enum DismissType { swipe, auto_dismiss }
/// Braze in-app message click actions
///
/// Messages with a "uri" click action should navigate to the specified uri
/// while respecting the "useWebView" field of the message's BrazeInAppMessage
/// object if appropriate.
enum ClickAction { news_feed, uri, none }
/// Braze in-app message types
enum MessageType { slideup, modal, full, html_full }
class BrazeContentCard {
/// Content Card json
String contentCardJsonString = "";
/// Content Card clicked
bool clicked = false;
/// Content Card created
int created = 0;
/// Content Card description
String description = "";
/// Content Card dismissable
bool dismissable = false;
/// Content Card expires at
int expiresAt = -1;
/// Key-value pair extras
Map<String, String> extras = Map();
/// Content Card id
String id = "";
/// Content Card image
String image = "";
/// Content Card image aspect ratio
double imageAspectRatio = 1;
/// Content Card link text
String linkText = "";
/// Content Card pinned
bool pinned = false;
/// Content Card removed
bool removed = false;
/// Content Card title
String title = "";
/// Content Card type
String type = "";
/// Content Card url
String url = "";
/// Content Card use web view
bool useWebView = false;
/// Content Card viewed
bool viewed = false;
/// Content Card control
bool isControl = false;
BrazeContentCard(String _data) {
contentCardJsonString = _data;
var contentCardJson = json.jsonDecode(_data);
var clickedJson = contentCardJson["cl"];
if (clickedJson is bool) {
clicked = clickedJson;
}
var createdJson = contentCardJson["ca"];
if (createdJson is int) {
created = createdJson;
}
var descriptionJson = contentCardJson["ds"];
if (descriptionJson is String) {
description = descriptionJson;
}
var dismissableJson = contentCardJson["db"];
if (dismissableJson is bool) {
dismissable = dismissableJson;
}
var expiresAtJson = contentCardJson["ea"];
if (expiresAtJson is int) {
expiresAt = expiresAtJson;
}
var extrasJson = contentCardJson["e"];
if (extrasJson is Map<String, dynamic>) {
extrasJson.forEach((key, value) {
if (extrasJson[key] is String) {
extras[key] = value;
}
});
}
var idJson = contentCardJson["id"];
if (idJson is String) {
id = idJson;
}
var imageJson = contentCardJson["i"];
if (imageJson is String) {
image = imageJson;
}
var imageAspectRatioJson = contentCardJson["ar"];
if (imageAspectRatioJson is double) {
imageAspectRatio = imageAspectRatioJson;
}
var linkTextJson = contentCardJson["dm"];
if (linkTextJson is String) {
linkText = linkTextJson;
}
var pinnedJson = contentCardJson["p"];
if (pinnedJson is bool) {
pinned = pinnedJson;
}
var removedJson = contentCardJson["r"];
if (removedJson is bool) {
removed = removedJson;
}
var titleJson = contentCardJson["tt"];
if (titleJson is String) {
title = titleJson;
}
var typeJson = contentCardJson["tp"];
if (typeJson is String) {
type = typeJson;
}
if (type == "control") {
isControl = true;
}
var urlJson = contentCardJson["u"];
if (urlJson is String) {
url = urlJson;
}
var useWebViewJson = contentCardJson["uw"];
if (useWebViewJson is bool) {
useWebView = useWebViewJson;
}
var viewedJson = contentCardJson["v"];
if (viewedJson is bool) {
viewed = viewedJson;
}
}
@override
String toString() {
return "BrazeContentCard viewed:" +
viewed.toString() +
" url:" +
url +
" type:" +
type +
" useWebView:" +
useWebView.toString() +
" title:" +
title +
" removed:" +
removed.toString() +
" linkText:" +
linkText +
" pinned:" +
pinned.toString() +
" image:" +
image +
" imageAspectRatio:" +
imageAspectRatio.toString() +
" id:" +
id +
" extras:" +
extras.toString() +
" description:" +
description +
" created:" +
created.toString() +
" expiresAt:" +
expiresAt.toString() +
" clicked:" +
clicked.toString() +
" expiresAt:" +
expiresAt.toString() +
" dismissable:" +
dismissable.toString() +
" isControl:" +
isControl.toString() +
" contentCardJsonString:" +
contentCardJsonString;
}
}
class BrazeInAppMessage {
/// In-app message text
String message = "";
/// In-app message header
String header = "";
/// Url of the image to display alongside the message
String imageUrl = "";
/// Url of zipped assets to display with an HTML message
String zippedAssetsUrl = "";
/// Uri to navigate to on-click
String uri = "";
/// Whether to use a webview to display the uri when clicked
bool useWebView = false;
/// In-app message display duration in milliseconds
int duration = 5;
/// In-app message body click action
ClickAction clickAction = ClickAction.none;
/// In-app message dismiss type
DismissType dismissType = DismissType.auto_dismiss;
/// In-app message type
MessageType messageType = MessageType.slideup;
/// Buttons to display alongside this in-app message
List<BrazeButton> buttons = [];
/// Key-value pair extras
Map<String, String> extras = Map();
/// In-app message json
String inAppMessageJsonString = "";
BrazeInAppMessage(String _data) {
inAppMessageJsonString = _data;
var inAppMessageJson = json.jsonDecode(_data);
var messageJson = inAppMessageJson["message"];
if (messageJson is String) {
message = messageJson;
}
var headerJson = inAppMessageJson["header"];
if (headerJson is String) {
header = headerJson;
}
var uriJson = inAppMessageJson["uri"];
if (uriJson is String) {
uri = uriJson;
}
var imageUrlJson = inAppMessageJson["image_url"];
if (imageUrlJson is String) {
imageUrl = imageUrlJson;
}
var zippedAssetsUrlJson = inAppMessageJson["zipped_assets_url"];
if (zippedAssetsUrlJson is String) {
zippedAssetsUrl = zippedAssetsUrlJson;
}
var useWebViewJson = inAppMessageJson["use_webview"];
if (useWebViewJson is bool) {
useWebView = useWebViewJson;
}
var durationJson = inAppMessageJson["duration"];
if (durationJson is int) {
duration = durationJson;
}
var clickActionJson = inAppMessageJson["click_action"];
if (clickActionJson is String) {
for (ClickAction action in ClickAction.values) {
if (action
.toString()
.toLowerCase()
.endsWith(clickActionJson.toLowerCase())) {
clickAction = action;
}
}
}
var dismissTypeJson = inAppMessageJson["message_close"];
if (dismissTypeJson is String) {
for (DismissType type in DismissType.values) {
if (type
.toString()
.toLowerCase()
.endsWith(dismissTypeJson.toLowerCase())) {
dismissType = type;
}
}
}
var messageTypeJson = inAppMessageJson["type"];
if (messageTypeJson is String) {
for (MessageType type in MessageType.values) {
if (type
.toString()
.toLowerCase()
.endsWith(messageTypeJson.toLowerCase())) {
messageType = type;
}
}
}
var extrasJson = inAppMessageJson["extras"];
if (extrasJson is Map<String, dynamic>) {
extrasJson.forEach((key, value) {
if (extrasJson[key] is String) {
extras[key] = value;
}
});
}
var buttonsJson = inAppMessageJson["btns"];
if (buttonsJson is List<dynamic>) {
for (var buttonJson in buttonsJson) {
buttons.add(BrazeButton(buttonJson));
}
}
}
@override
String toString() {
return inAppMessageJsonString;
}
}
class BrazeButton {
/// Button text
String text = "";
/// Uri to navigate to on-click
String uri = "";
/// Whether to use a webview to display the uri when clicked
bool useWebView = false;
/// Button click action
ClickAction clickAction = ClickAction.none;
/// Id for analytics
int id = 0;
BrazeButton(dynamic buttonJson) {
var textJson = buttonJson["text"];
if (textJson is String) {
text = textJson;
}
var uriJson = buttonJson["uri"];
if (uriJson is String) {
uri = uriJson;
}
var useWebViewJson = buttonJson["use_webview"];
if (useWebViewJson is bool) {
useWebView = useWebViewJson;
}
var clickActionJson = buttonJson["click_action"];
if (clickActionJson is String) {
for (ClickAction action in ClickAction.values) {
if (action
.toString()
.toLowerCase()
.endsWith(clickActionJson.toLowerCase())) {
clickAction = action;
}
}
}
var idJson = buttonJson["id"];
if (idJson is int) {
id = idJson;
}
}
@override
String toString() {
return "BrazeButton text:" +
text +
" uri:" +
uri +
" clickAction:" +
clickAction.toString() +
" useWebView:" +
useWebView.toString();
}
}
class BrazeSdkAuthenticationError {
int code = 0;
String reason = "";
String userId = "";
String signature = "";
/// Sdk Authentication Error json
String brazeSdkAuthenticationErrorString = "";
BrazeSdkAuthenticationError(String _data) {
brazeSdkAuthenticationErrorString = _data;
var brazeSdkAuthenticationErrorJson = json.jsonDecode(_data);
var codeJson = brazeSdkAuthenticationErrorJson["code"];
if (codeJson is int) {
code = codeJson;
}
var reasonJson = brazeSdkAuthenticationErrorJson["reason"];
if (reasonJson is String) {
reason = reasonJson;
}
var userIdJson = brazeSdkAuthenticationErrorJson["userId"];
if (userIdJson is String) {
userId = userIdJson;
}
var signatureJson = brazeSdkAuthenticationErrorJson["signature"];
if (signatureJson is String) {