Request FCM token from native
This commit is contained in:
parent
95ca80949f
commit
9a09a83dc6
@ -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,26 +22,40 @@ 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
|
||||
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
|
||||
if (call.method.equals("getFCMToken")) {
|
||||
FirebaseInstanceId.getInstance().getInstanceId()
|
||||
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<InstanceIdResult> task) {
|
||||
if (task.isSuccessful()) {
|
||||
Context context = getActivity();
|
||||
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);
|
||||
FirebaseInstanceId.getInstance().getInstanceId()
|
||||
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<InstanceIdResult> task) {
|
||||
if (task.isSuccessful()) {
|
||||
Context context = getActivity();
|
||||
String token = task.getResult().getToken();
|
||||
UpdateTokenTask updateTokenTask = new UpdateTokenTask(context);
|
||||
updateTokenTask.execute(token);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
name: hass_client
|
||||
description: Home Assistant Android Client
|
||||
|
||||
version: 0.0.0+1149
|
||||
version: 0.0.0+1150
|
||||
|
||||
|
||||
environment:
|
||||
|
Reference in New Issue
Block a user