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

@ -11,6 +11,9 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
@ -19,14 +22,16 @@ import com.google.firebase.messaging.FirebaseMessaging;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.keyboardcrumbs.hassclient/native";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("getFCMToken")) {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
@ -36,9 +41,21 @@ public class MainActivity extends FlutterActivity {
String token = task.getResult().getToken();
UpdateTokenTask updateTokenTask = new UpdateTokenTask(context);
updateTokenTask.execute(token);
result.success(token);
} else {
result.error("fcm_error", task.getException().getMessage(), task.getException());
}
}
});
}
}
}
);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}

View File

@ -38,7 +38,7 @@ public class UpdateTokenTask extends AsyncTask<String, String, String> {
String token = params[0];
SharedPreferences prefs = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("flutter.notification-token", token);
editor.putString("flutter.npush-token", token);
editor.commit();
}
return null;

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');
String fcmToken = await AppSettings().loadSingle('npush-token');
if (fcmToken != null) {
Logger.d("[MobileAppIntegrationManager] token exist");
await _doCheck(fcmToken);
done = true;
} else {
Logger.d("[MobileAppIntegrationManager] no fcm token. Retry in 5 seconds");
attempts++;
await Future.delayed(Duration(seconds: 5));
Logger.d("[MobileAppIntegrationManager] no fcm token. Requesting...");
try {
fcmToken = await platform.invokeMethod('getFCMToken');
await _doCheck(fcmToken);
} 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) {

View File

@ -1,7 +1,7 @@
name: hass_client
description: Home Assistant Android Client
version: 0.0.0+1149
version: 0.0.0+1150
environment: