setBackgroundPushHandler method

  1. @override
Future<void> setBackgroundPushHandler(
  1. BackgroundPushHandler? handler
)
override

Registers handler to receive Customer.io silent pushes when Customer.io handles push in your app. Pass null to stop. The plugin remembers the registration across launches until then, or until a push finds no handler in a later version of the app.

handler must be a top-level or static function annotated with @pragma('vm:entry-point'). Register it at the start of main, so a push that launches the app in the background reaches it.

On Android the handler runs from a WorkManager job, on the isolate that registered it while that engine runs, and otherwise on a background isolate the plugin starts, which shares no state with the rest of the app. On iOS it runs on the main isolate and needs CioAppDelegateWrapper. iOS gives a push about 30 seconds, and Customer.io completes it after 25.

Implementation

@override
Future<void> setBackgroundPushHandler(BackgroundPushHandler? handler) async {
  if (handler == null) {
    methodChannel.setMethodCallHandler(null);
    await methodChannel
        .invokeNativeMethod<void>(NativeMethods.clearBackgroundPushHandler);
    return;
  }
  final handlerHandle = PluginUtilities.getCallbackHandle(handler);
  if (handlerHandle == null) {
    throw ArgumentError.value(handler, 'handler',
        "must be a top-level or static function annotated with @pragma('vm:entry-point')");
  }
  methodChannel.setMethodCallHandler(
      (call) async => handler(_backgroundPush(call.arguments as Map)));
  await methodChannel
      .invokeNativeMethod<void>(NativeMethods.registerBackgroundPushHandler, {
    NativeMethodParams.dispatcherHandle:
        PluginUtilities.getCallbackHandle(_backgroundPushDispatcher)!
            .toRawHandle(),
    NativeMethodParams.handlerHandle: handlerHandle.toRawHandle(),
  });
}