Untitled
unknown
dart
3 years ago
6.5 kB
2
Indexable
// mainscreen fgbg stream : return FGBGEvents.stream.listen((event) async { print(event); // FGBGType.foreground or FGBGType.background if (event == FGBGType.foreground) { GlobalV.isAppOnForeground = true; flutterLocalNotificationsPlugin.cancelAll(); getJobFromBackground(); await myGetStateFunc(); } else if (event == FGBGType.background) { GlobalV.isAppOnForeground = false; } }); // main_screen fonksiyon : void getJobFromBackground() async { await WebServices.getBackgroundNotification(false).then((value) async { if (value == null) { setState(() {}); } else if (value != null && value is! int) { if (myState == null && !isJobAvailable) { int expireTime = value['TimeoutTime']; int myCurrentNTP = (await NTP.now()).toUtc().millisecondsSinceEpoch ~/ 1000; timeDiff = expireTime - myCurrentNTP; timeDiff--; if (timeDiff > 0) { jobFromNotification = true; timer = Timer.periodic(Duration(seconds: 1), (t) { print("Timediff = $timeDiff"); if (timeDiff == 0) { jobFromNotification = false; isJobAvailable = false; closeJobOffer(false); t.cancel(); } if (timeDiff != 0) { timeDiff--; } }); GlobalV.notificationID = value['NotificationID']; GlobalV.jobRequestFromNotif = RequestInfo.fromJson((value["data"]["RequestInfo"])); await handleJob(value); } } } }); } // webservices (kendi auth'una çevirip refreshtoken kısımlarını elemeyi unutma) : static Future saveBackgroundNotification( bool isRefreshed, timeouttime, notificationID, myDeviceID, RequestInfo requestInfo, UserInfo userInfo) async { bool isConnected = await connectionCheck(); if (!isConnected) return null; String token = (await SharedPreferences.getInstance()).getString("usertoken"); Response res = await post( Uri.http( GlobalV.mainURL + ":9880", "/drivers/saveBackgroundNotification"), headers: <String, String>{'Authorization': 'Bearer ' + token}, body: { 'DeviceID': myDeviceID, 'TimeoutTime': timeouttime.toString(), 'NotificationID': notificationID, 'UserID': requestInfo.userID ?? "", 'BeginLat': requestInfo.beginLat == null ? "" : requestInfo.beginLat.toString(), 'BeginLng': requestInfo.beginLng == null ? "" : requestInfo.beginLng.toString(), 'EndLat': requestInfo.endLat == null ? "" : requestInfo.endLat.toString(), 'EndLng': requestInfo.endLng == null ? "" : requestInfo.endLng.toString(), 'BeginCity': requestInfo.beginCity ?? "", 'EndCity': requestInfo.endCity ?? "", 'RouteIndex': requestInfo.routeIndex == null ? "" : requestInfo.routeIndex.toString(), 'PayMethods': requestInfo.payMethods == null ? "" : requestInfo.payMethods.toString(), 'CustomerID': userInfo.sId ?? "", 'Name': userInfo.name ?? "", 'Surname': userInfo.surname ?? "", 'Phone': userInfo.phone ?? "", 'Email': userInfo.email ?? "", "SaveTime": userInfo.saveTime ?? "", "Password": userInfo.password ?? "", "ProfilePicturePath": userInfo.profilePicturePath ?? "", "IsVerified": userInfo.isVerified ?? "", }, ); if (res.body.contains('"Result":false')) { print( "Background jobrequest postumuz false döndü (eksik/bozuk veri verdik)"); return 0; } else if (res.body.contains('"message":"Unauthorized"')) { print("Forward Price => Unauthorized"); if (!isRefreshed) { print("Yeni token alınıyor"); return await refreshToken().then((value) async { if (value == 1) { return await saveBackgroundNotification(true, timeouttime, notificationID, myDeviceID, requestInfo, userInfo); } else { print("Token yenilenirken sunucu ile bağlantıda hata"); return -1; } }); } else { print("Yeni token alınsa da ilgili işlem gerçekleştirilemedi."); return -1; } } else if (res.body.contains('"Result":true')) { print("Background bildirim post edildi"); return 1; } else { print("saveBackgroundNotification hata kodu -2"); return -2; } } static Future getBackgroundNotification(bool isRefreshed) async { bool isConnected = await connectionCheck(); if (!isConnected) return null; Response res = await get( Uri.http(GlobalV.mainURL + ":9880", "/drivers/getBackgroundNotification/" + GlobalV.tabletID), headers: <String, String>{ 'Authorization': 'Bearer ' + GlobalV.user.token }, ); var json = jsonDecode(res.body); if (res.body.contains('"Result":null')) { print("foreground getBackgroundNotif null"); return 0; } else if (res.body.contains('"message":"Unauthorized"')) { print("Son tablet kontrolü => Unauthorized"); if (!isRefreshed) { print("Yeni token alınıyor"); return await refreshToken().then((value) async { if (value == 1) { return await getBackgroundNotification(true); } else { print("Token yenilenirken sunucu ile bağlantıda hata"); return -1; } }); } else { print("Yeni token alınsa da ilgili işlem gerçekleştirilemedi."); return -1; } } else if (json["Result"]["TimeoutTime"] != null) { return json["Result"]; } else { return -2; } } // Ek not : Future<void> myGetStateFunc() async { await WebServices.getStateByDeviceID(false).then((getState) async { if (getState == null) { setState(() {}); if (subscription == null) { RemoteMessage initialMessage = await FirebaseMessaging.instance.getInitialMessage(); await handleNotification(initialMessage); getJobFromBackground(); // şartlara initialmessagelerden sonra hep bunu koy } . . . . . bla bla . . }
Editor is loading...