Request FCM token from native

This commit is contained in:
Yegor Vialov
2020-05-25 15:41:29 +00:00
parent 95ca80949f
commit 9a09a83dc6
4 changed files with 43 additions and 30 deletions

View File

@ -21,26 +21,22 @@ class MobileAppIntegrationManager {
return '${HomeAssistant().userName}\'s ${DeviceInfoManager().model}';
}
static const platform = const MethodChannel('com.keyboardcrumbs.hassclient/native');
static Future checkAppRegistration() async {
int attempts = 1;
bool done = false;
Logger.d("[MobileAppIntegrationManager] Stratring mobile app integration check...");
while (attempts <= 5 && !done) {
Logger.d("[MobileAppIntegrationManager] check attempt $attempts");
String fcmToken = await AppSettings().loadSingle('notification-token');
if (fcmToken != null) {
Logger.d("[MobileAppIntegrationManager] token exist");
String fcmToken = await AppSettings().loadSingle('npush-token');
if (fcmToken != null) {
Logger.d("[MobileAppIntegrationManager] token exist");
await _doCheck(fcmToken);
} else {
Logger.d("[MobileAppIntegrationManager] no fcm token. Requesting...");
try {
fcmToken = await platform.invokeMethod('getFCMToken');
await _doCheck(fcmToken);
done = true;
} else {
Logger.d("[MobileAppIntegrationManager] no fcm token. Retry in 5 seconds");
attempts++;
await Future.delayed(Duration(seconds: 5));
} on PlatformException catch (e) {
Logger.e('[MobileAppIntegrationManager] Failed to get FCM token from native: ${e.message}');
}
}
if (!done) {
Logger.e("[MobileAppIntegrationManager] No FCM token");
}
}
static Future _doCheck(String fcmToken) {