callbackDispatcher function

void callbackDispatcher()

Background isolate entry point for NetmeraPushLifecycleCallbacks. Must remain a top-level function annotated with @pragma('vm:entry-point').

Implementation

@pragma('vm:entry-point')
void callbackDispatcher() {
  WidgetsFlutterBinding.ensureInitialized();
  const MethodChannel backgroundChannel =
      MethodChannel('flutter_nm_sdk_background');

  backgroundChannel.setMethodCallHandler((MethodCall call) async {
    final args = call.arguments as List<dynamic>;
    final callback = PluginUtilities.getCallbackFromHandle(
        CallbackHandle.fromRawHandle(args[0] as int))!;

    // args[1] is either a flat Map from WorkManager or a Map with
    // 'pushObjectJson' key containing the full typed push as JSON string.
    Map<dynamic, dynamic> pushData;
    final raw = args[1];
    if (raw is Map) {
      final pushJson = raw['pushObjectJson'] as String?;
      if (pushJson != null) {
        pushData = Map<dynamic, dynamic>.from(jsonDecode(pushJson) as Map);
      } else {
        pushData = raw;
      }
    } else {
      return;
    }

    final push = NetmeraPushObject.fromJson(pushData);
    await (callback as Future<void> Function(NetmeraPushObject))(push);
  });

  backgroundChannel.invokeMethod('serviceInitialized');
}