setPushRegistration method

Future<void> setPushRegistration({
  1. Provider? provider,
  2. String? pushRegistrationId,
})

Registers a single mobile device, as one user can be connected to multiple mobile devices.

If the provider and pushRegistrationId parameters are not passed, it registers the default Firebase token for Android, or the default Apns token for iOS, for this device.

If passing parameters to this function, both provider and pushRegistrationId must not be null

Implementation

Future<void> setPushRegistration({
  Provider? provider,
  String? pushRegistrationId,
}) async {
  if ((provider == null && pushRegistrationId != null) ||
      (provider != null && pushRegistrationId == null)) {
    throw StateError('provider and pushRegistrationId must both be non-null');
  }

  if (_headlessWebView == null) {
    throw StateError(
        'The setPushRegistration method cannot be called after destroying the session');
  }

  if (!_completer.isCompleted) {
    if (_me == null) {
      throw StateError(
          'The me property needs to be set for the Session object before calling setPushRegistration');
    }

    if (kDebugMode) {
      print(
          '📗 session setPushRegistration: !_completer.isCompleted, awaiting for _completer.future');
    }
    await _completer.future;
  }

  if (kDebugMode) {
    print('📗 session setPushRegistration: Enabling push notifications');
  }

  if (provider == null && pushRegistrationId == null) {
    await _setOrUnsetPushRegistration(true);
  } else {
    await _executeAsync(
        'await session.setPushRegistration({provider: "${provider!.name}", pushRegistrationId: "$pushRegistrationId"});');
  }
}