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.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,26 +22,40 @@ 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
|
||||||
|
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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(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];
|
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;
|
||||||
|
@ -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;
|
if (fcmToken != null) {
|
||||||
Logger.d("[MobileAppIntegrationManager] Stratring mobile app integration check...");
|
Logger.d("[MobileAppIntegrationManager] token exist");
|
||||||
while (attempts <= 5 && !done) {
|
await _doCheck(fcmToken);
|
||||||
Logger.d("[MobileAppIntegrationManager] check attempt $attempts");
|
} else {
|
||||||
String fcmToken = await AppSettings().loadSingle('notification-token');
|
Logger.d("[MobileAppIntegrationManager] no fcm token. Requesting...");
|
||||||
if (fcmToken != null) {
|
try {
|
||||||
Logger.d("[MobileAppIntegrationManager] token exist");
|
fcmToken = await platform.invokeMethod('getFCMToken');
|
||||||
await _doCheck(fcmToken);
|
await _doCheck(fcmToken);
|
||||||
done = true;
|
} on PlatformException catch (e) {
|
||||||
} else {
|
Logger.e('[MobileAppIntegrationManager] Failed to get FCM token from native: ${e.message}');
|
||||||
Logger.d("[MobileAppIntegrationManager] no fcm token. Retry in 5 seconds");
|
|
||||||
attempts++;
|
|
||||||
await Future.delayed(Duration(seconds: 5));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!done) {
|
|
||||||
Logger.e("[MobileAppIntegrationManager] No FCM token");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future _doCheck(String fcmToken) {
|
static Future _doCheck(String fcmToken) {
|
||||||
|
@ -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:
|
||||||
|
Reference in New Issue
Block a user