initialize method
key
shouldn't be null
wait for it to initialize before calling events
Implementation
Future<void> initialize({required String key}) async {
// _database.deleteEntireData();
apiKey = key;
WidgetsBinding.instance.addObserver(this);
await Permission.location.request();
await Permission.phone.request();
if (await Permission.location.isGranted &&
await Permission.phone.isGranted) {
await _getDeviceInfo();
NotificationBody? body;
// try {
if (!kIsWeb) {
final RemoteMessage? remoteMessage =
await FirebaseMessaging.instance.getInitialMessage();
if (remoteMessage != null) {
body = NotificationHelper.convertNotification(remoteMessage.data);
EventModel event = EventModel(
name: Constants.click,
eventTime: DateTime.now().millisecondsSinceEpoch,
properties: jsonEncode(body.toJson()),
);
await DatabaseHelper().insertEvent(event);
}
await NotificationHelper().initialize(flutterLocalNotificationsPlugin);
FirebaseMessaging.onBackgroundMessage(myBackgroundMessageHandler);
}
String? did = await _storage.read(key: "did");
String? sid = await _storage.read(key: "sid");
String? stime = await _storage.read(key: "stime");
String? uid = await _storage.read(key: "uid");
_session.data!.uid = uid;
packageInfo = await PackageInfo.fromPlatform();
if (did == null) {
await _createDevice();
await _createSession();
await _appInstall();
} else if (sid == null) {
_session.data!.did = did;
await _createSession();
} else {
if (stime != null) {
int? stimeMilli = int.tryParse(stime);
if (stimeMilli != null && !_isSessionExpired(stimeMilli)) {
_session.data!.sid = sid;
_session.data!.did = did;
_session.data!.stime = stimeMilli;
} else {
_session.data!.did = did;
await _createSession();
}
} else {
_session.data!.did = did;
await _createSession();
}
}
await _appLaunch();
}
}