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.content.SharedPreferences.Editor;
import android.os.Bundle; 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.OnCompleteListener;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceId;
@ -19,14 +22,16 @@ import com.google.firebase.messaging.FirebaseMessaging;
public class MainActivity extends FlutterActivity { public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.keyboardcrumbs.hassclient/native";
@Override @Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine); GeneratedPluginRegistrant.registerWith(flutterEngine);
} new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { public void onMethodCall(MethodCall call, MethodChannel.Result result) {
super.onCreate(savedInstanceState); if (call.method.equals("getFCMToken")) {
FirebaseInstanceId.getInstance().getInstanceId() FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() { .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override @Override
@ -36,9 +41,21 @@ public class MainActivity extends FlutterActivity {
String token = task.getResult().getToken(); String token = task.getResult().getToken();
UpdateTokenTask updateTokenTask = new UpdateTokenTask(context); UpdateTokenTask updateTokenTask = new UpdateTokenTask(context);
updateTokenTask.execute(token); 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]; String token = params[0];
SharedPreferences prefs = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE); SharedPreferences prefs = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit(); SharedPreferences.Editor editor = prefs.edit();
editor.putString("flutter.notification-token", token); editor.putString("flutter.npush-token", token);
editor.commit(); editor.commit();
} }
return null; return null;

View File

@ -21,26 +21,22 @@ class MobileAppIntegrationManager {
return '${HomeAssistant().userName}\'s ${DeviceInfoManager().model}'; return '${HomeAssistant().userName}\'s ${DeviceInfoManager().model}';
} }
static const platform = const MethodChannel('com.keyboardcrumbs.hassclient/native');
static Future checkAppRegistration() async { static Future checkAppRegistration() async {
int attempts = 1; String fcmToken = await AppSettings().loadSingle('npush-token');
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) { if (fcmToken != null) {
Logger.d("[MobileAppIntegrationManager] token exist"); Logger.d("[MobileAppIntegrationManager] token exist");
await _doCheck(fcmToken); await _doCheck(fcmToken);
done = true;
} else { } else {
Logger.d("[MobileAppIntegrationManager] no fcm token. Retry in 5 seconds"); Logger.d("[MobileAppIntegrationManager] no fcm token. Requesting...");
attempts++; try {
await Future.delayed(Duration(seconds: 5)); 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) { static Future _doCheck(String fcmToken) {

View File

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