registerPushDevice method

Future<void> registerPushDevice(
  1. PushDeviceToken token,
  2. String? locale
)

Registers a device (Android, iOS, or Web) for receiving notifications via Firebase Cloud Messaging.

Localization

You may provide the device's preferred locale via the Accept-Language HTTP header, which will localize notifications to that device using the best match of the supported locales.

In order to change the locale of the device, you should re-register it supplying the desired locale (use unregisterPushDevice, and then registerPushDevice once again).

Authentication

Mandatory.

Result

Always returns null on success.

Idempotent

Succeeds if the specified token is registered already.

Implementation

Future<void> registerPushDevice(PushDeviceToken token, String? locale) async {
  Log.debug('registerPushDevice($token, $locale)', '$runtimeType');

  final variables = RegisterPushDeviceArguments(token: token);
  final query = MutationOptions(
    operationName: 'RegisterPushDevice',
    document: RegisterPushDeviceMutation(variables: variables).document,
    variables: variables.toJson(),
  );

  final request = query.asRequest;
  final body = const RequestSerializer().serializeRequest(request);
  final encodedBody = json.encode(body);

  await client.post(
    dio.FormData.fromMap({
      'operations': encodedBody,
      'map': '{ "token": ["variables.token"] }',
      'token': token,
    }),
    options: dio.Options(
      headers: {if (locale != null) 'Accept-Language': locale},
    ),
    operationName: query.operationName,
    onException: (data) => RegisterPushDeviceException(
      data['registerPushDevice'] == null
          ? null
          : RegisterPushDevice$Mutation.fromJson(data).registerPushDevice
                as RegisterPushDeviceErrorCode,
    ),
  );
}