/
main.dart
513 lines (465 loc) · 15.8 KB
/
main.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
///
/// Copyright (c) 2022 Inomera Research.
///
import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:netmera_flutter_example/page_coupon.dart';
import 'package:netmera_flutter_example/page_event.dart';
import 'package:netmera_flutter_example/page_push_inbox.dart';
import 'package:netmera_flutter_example/page_user.dart';
import 'package:netmera_flutter_sdk/Netmera.dart';
import 'package:netmera_flutter_sdk/NetmeraPushBroadcastReceiver.dart';
import 'package:flutter_config/flutter_config.dart';
import 'package:flutter/services.dart';
import 'package:uni_links/uni_links.dart';
import 'page_category.dart';
// This method must be a top-level function
@pragma('vm:entry-point')
void _onPushReceiveBackgroundHandler(Map<dynamic, dynamic> bundle) async {
print("onPushReceiveBackground: $bundle");
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// This method must be called before the runApp and the provided handler must be a top-level function.
await FlutterConfig.loadEnvVariables();
NetmeraPushBroadcastReceiver.onPushReceiveBackground(
_onPushReceiveBackgroundHandler);
runApp(const MyApp());
}
void initBroadcastReceiver() {
void _onPushRegister(Map<dynamic, dynamic> bundle) async {
print("onPushRegister: $bundle");
_MyAppState._pushTokenString = bundle['pushToken'];
}
void _onPushReceive(Map<dynamic, dynamic> bundle) async {
print("onPushReceive: $bundle");
}
void _onPushDismiss(Map<dynamic, dynamic> bundle) async {
print("onPushDismiss: $bundle");
}
void _onPushOpen(Map<dynamic, dynamic> bundle) async {
print("onPushOpen: $bundle");
}
void _onPushButtonClicked(Map<dynamic, dynamic> bundle) async {
print("onPushButtonClicked: $bundle");
}
void _onCarouselObjectSelected(Map<dynamic, dynamic> bundle) async {
print("onCarouselObjectSelected: $bundle");
}
NetmeraPushBroadcastReceiver().initialize(
onPushRegister: _onPushRegister,
onPushReceive: _onPushReceive,
onPushDismiss: _onPushDismiss,
onPushOpen: _onPushOpen,
onPushButtonClicked: _onPushButtonClicked,
onCarouselObjectSelected: _onCarouselObjectSelected,
);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _MyAppState();
}
class _MyAppState extends State<HomePage> {
String _apiKey = "";
String _baseUrl = "";
bool _isPushEnabled = true;
bool _isPopUpPresentationEnabled = true;
static String _pushTokenString = "";
static const platform = MethodChannel('nm_flutter_example_channel');
TextEditingController _controllerApiKey = TextEditingController();
TextEditingController _controllerBaseUrl = TextEditingController();
@override
void initState() {
super.initState();
initUniLinks();
initBroadcastReceiver();
// Add this to enable popup presentation on app start.
Netmera.enablePopupPresentation();
if (Platform.isIOS) {
Netmera.requestPushNotificationAuthorization();
Netmera.setAppGroupName(
"group.com.netmera.flutter"); // Set your app group name
}
Netmera.isPushEnabled().then((value) {
if (value != null) {
setState(() {
_isPushEnabled = value;
});
}
});
String msg;
Future.delayed(const Duration(milliseconds: 1000), () {
Netmera.getCurrentExternalId().then((value) {
msg = value == null
? "ExternalID was not set yet"
: "ExternalId :: " + value;
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: const Color.fromARGB(255, 166, 186, 171),
textColor: Colors.white,
fontSize: 16.0);
});
});
}
onCancelPress() {
Navigator.pop(context);
}
onSetPress() {
if (_baseUrl != '' && _apiKey != '') {
if (Platform.isAndroid) {
Netmera.skipAppConfigAndSetBaseUrl(_baseUrl);
} else {
Netmera.setBaseUrl(_baseUrl);
}
Netmera.setApiKey(_apiKey);
platform.invokeMethod('setApiKey', {"apiKey": _apiKey});
platform.invokeMethod('setBaseUrl', {"baseUrl": _baseUrl});
}
onCancelPress();
}
onSetLongPress() {
if (_baseUrl == 'b' && _apiKey == 'b') {
setState(() {
_baseUrl = FlutterConfig.get('NETMERA_PREPROD_BASE_URL');
_controllerBaseUrl.text = FlutterConfig.get('NETMERA_PREPROD_BASE_URL');
_apiKey = FlutterConfig.get('NETMERA_PREPROD_API_KEY');
_controllerApiKey.text = FlutterConfig.get('NETMERA_PREPROD_API_KEY');
});
} else if (_baseUrl == 'c' && _apiKey == 'c') {
setState(() {
_baseUrl = FlutterConfig.get('NETMERA_TEST_BASE_URL');
_controllerBaseUrl.text = FlutterConfig.get('NETMERA_TEST_BASE_URL');
_apiKey = FlutterConfig.get('NETMERA_TEST_API_KEY');
_controllerApiKey.text = FlutterConfig.get('NETMERA_TEST_API_KEY');
});
} else if (_baseUrl == 'd' && _apiKey == 'd') {
setState(() {
_baseUrl = FlutterConfig.get('NETMERA_PROD_BASE_URL');
_controllerBaseUrl.text = FlutterConfig.get('NETMERA_PROD_BASE_URL');
_apiKey = FlutterConfig.get('NETMERA_PROD_API_KEY');
_controllerApiKey.text = FlutterConfig.get('NETMERA_PROD_API_KEY');
});
}
}
setProperties() {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: SizedBox(
height: 140,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: "BaseUrl"),
onChanged: (text) {
setState(() {
_baseUrl = text;
});
},
controller: _controllerBaseUrl,
),
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: "API Key"),
onChanged: (text) {
setState(() {
_apiKey = text;
});
},
controller: _controllerApiKey,
),
])),
actions: [
TextButton(
onPressed: onCancelPress,
child: const Text(
'Cancel',
style: TextStyle(
color: Colors.blue, // Set your desired text color here
),
),
),
TextButton(
onPressed: onSetPress,
onLongPress: onSetLongPress,
child: const Text(
'SET',
style: TextStyle(
color:
Colors.deepPurple, // Set your desired text color here
),
),
),
],
));
}
enableData() {
Netmera.startDataTransfer();
}
disableData() {
Netmera.stopDataTransfer();
}
enableLocationAndGeofence() {
Netmera.requestPermissionsForLocation();
}
requestPushNotificationAuthorization() {
Netmera.requestPushNotificationAuthorization();
}
navigateToCoupons() {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const CouponPage()));
}
currentExternalId() {
Netmera.getCurrentExternalId().then((externalId) {
Fluttertoast.showToast(
msg: 'Current External Id: $externalId',
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: const Color.fromARGB(255, 166, 186, 171),
textColor: Colors.white,
fontSize: 16.0);
});
}
navigateToEvents() {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const EventPage()));
}
navigateToUser() {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const UserPage()));
}
navigateToPushInbox() {
Navigator.push(context,
MaterialPageRoute(builder: (context) => const PushInboxPage()));
}
navigateToCategory() {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const CategoryPage()));
}
isPushEnabled() {
Netmera.isPushEnabled().then((enabled) {
Fluttertoast.showToast(
msg: 'Is Push Enabled: $enabled',
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: const Color.fromARGB(255, 166, 186, 171),
textColor: Colors.white,
fontSize: 16.0);
});
}
enableOrDisablePush() {
if (_isPushEnabled) {
Netmera.disablePush();
setState(() {
_isPushEnabled = false;
});
} else {
Netmera.enablePush();
setState(() {
_isPushEnabled = true;
});
}
}
disableOrEnablePopUpPresentation() {
if (_isPopUpPresentationEnabled) {
Netmera.disablePopupPresentation();
setState(() {
_isPopUpPresentationEnabled = false;
});
} else {
Netmera.enablePopupPresentation();
setState(() {
_isPopUpPresentationEnabled = true;
});
}
}
setNetmeraMaxActiveRegion() {
Netmera.setNetmeraMaxActiveRegions(10);
}
toastPushTest() {
Fluttertoast.showToast(
msg: 'Pushtoken :: $_pushTokenString',
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: const Color.fromARGB(255, 166, 186, 171),
textColor: Colors.white,
fontSize: 16.0);
}
turnOffSendingEventAndUserUpdate() {
Netmera.turnOffSendingEventAndUserUpdate(false);
}
Future<void> initUniLinks() async {
try {
final uri = await getInitialUri();
if (uri != null) {
Fluttertoast.showToast(
msg: 'Initial url is: $uri',
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.TOP,
backgroundColor: const Color.fromARGB(255, 166, 186, 171),
textColor: Colors.white,
fontSize: 16.0);
}
} catch (error) {}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 16.0, right: 16.0),
child: Column(children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Flutter Example',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Colors.grey),
),
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 13),
),
onPressed: setProperties,
child: const Text('SET PROPERTIES'),
),
],
),
Expanded(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(top: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
button('ENABLE DATA', enableData),
button('DISABLE DATA', disableData),
],
),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('ENABLE LOCATION & GEOFENCE', enableLocationAndGeofence),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('REQUEST PUSH NOTIFICATION AUTHORIZATION',
requestPushNotificationAuthorization),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('COUPONS', navigateToCoupons),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('CURRENT EXTERNAL ID', currentExternalId),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('EVENTS', navigateToEvents),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('USER', navigateToUser),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('PUSH INBOX', navigateToPushInbox),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('CATEGORY', navigateToCategory),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('IS PUSH ENABLED', isPushEnabled),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button(_isPushEnabled ? 'DISABLE PUSH' : 'ENABLE PUSH',
enableOrDisablePush),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button(
_isPopUpPresentationEnabled
? 'DISABLE PRESENTATION STATE'
: 'ENABLE PRESENTATION STATE',
disableOrEnablePopUpPresentation),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('SET NETMERA MAX ACTIVE REGION',
setNetmeraMaxActiveRegion),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('TOAST PUSH TOKEN', toastPushTest),
),
Container(
margin: const EdgeInsets.only(top: 8),
child: button('TURN OFF SENDING EVENT AND USER UPDATE',
turnOffSendingEventAndUserUpdate),
),
Container(
margin: const EdgeInsets.only(top: 8, bottom: 16),
child: Column(children: [
const Text('DEVICE TOKEN',
style: TextStyle(
fontSize: 16, fontWeight: FontWeight.w700)),
Container(
margin: const EdgeInsets.only(top: 4),
child: Text(_pushTokenString),
),
])),
],
)))
]),
))),
);
}
}
ElevatedButton button(String text, VoidCallback onPressed) {
return ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: Text(
text,
style: const TextStyle(color: Colors.white),
),
);
}