setDeviceToken static method

Future<void> setDeviceToken(
  1. String token
)

Implementation

static Future<void> setDeviceToken(String token) async {
  //throws exception if uninitialized
  _checkIfSDKInitialized();

  if (token.isEmpty) {
    return;
  }

  //check if token already sent to server
  if (Prefs.getString(PREF_USER_FCM_TOKEN, null) == token) {
    return;
  }

  //check if token already present with server
  User? currentUser = getCurrentUser();
  var isUpdate = false;
  String? platform = ANDROID_PLATFORM;

  if(Platform.isIOS) {
    platform = IOS_PLATFORM;
  }

  for (var item in currentUser?.devices ?? []) {
    if (item.fcmToken == token && item.platform == platform) {
      return;
    }

    if (item.deviceSid == Prefs.getString(PREF_USER_DEVICE_ID, null)) {
      if (item.fcmToken != token && item.platform == platform) {
        isUpdate = true;
      }
    }
  }

  String? userId = Prefs.getString(PREF_USER_ID, null);
  if (userId != null) {
    await UpdateTokenController().start(Prefs.getString(PREF_APP_ID, null)!, userId, token, isUpdate);
  }
}