entrypoint function
Implementation
@pragma('vm:entry-point')
Future<void> entrypoint(List<String> args) async {
WidgetsFlutterBinding.ensureInitialized();
MethodChannel channel = const MethodChannel(
'flutter_pushed_messaging_bg',
JSONMethodCodec(),
);
await channel.invokeMethod<dynamic>("lock");
addLog(channel, "Start entrypoint");
await setNewStatus(channel, currentStatus);
final int rawHandle = int.parse(args[0]);
final String token = args[1];
addLog(channel, "Token: $token");
final callbackHandle = CallbackHandle.fromRawHandle(rawHandle);
final onMessage = PluginUtilities.getCallbackFromHandle(callbackHandle);
List<ConnectivityResult> lastConnectivity = [];
Connectivity().onConnectivityChanged.listen((result) async {
await channel.invokeMethod<dynamic>("lock");
await addLog(channel, "Connectivity changed: $result");
if (result.contains(ConnectivityResult.wifi) &&
!lastConnectivity.contains(ConnectivityResult.wifi)) {
webChannel?.sink.close();
}
if (result.contains(ConnectivityResult.none)) {
connected = false;
} else {
connected = true;
}
lastConnectivity = result;
connect(token, channel, onMessage);
});
channel.setMethodCallHandler((call) async {
if (call.arguments["method"] == "reconnect" && active) {
await channel.invokeMethod<dynamic>("lock");
await addLog(channel, "Reconnect");
await setNewStatus(channel, currentStatus);
webChannel?.sink.close();
}
});
await Future.delayed(const Duration(seconds: 5));
await channel.invokeMethod<dynamic>("unlock");
}