getNewToken method

Future<String> getNewToken(
  1. String token, {
  2. String? apnsToken,
  3. String? fcmToken,
  4. String? hpkToken,
  5. String? ruStoreToken,
})

Implementation

Future<String> getNewToken(String token,
    {String? apnsToken,
    String? fcmToken,
    String? hpkToken,
    String? ruStoreToken}) async {
  var deviceSettings = [];
  if (apnsToken != null)
    deviceSettings.add({"deviceToken": apnsToken, "transportKind": "Apns"});
  if (fcmToken != null)
    deviceSettings.add({"deviceToken": fcmToken, "transportKind": "Fcm"});
  if (hpkToken != null)
    deviceSettings.add({"deviceToken": hpkToken, "transportKind": "Hpk"});
  if (ruStoreToken != null)
    deviceSettings
        .add({"deviceToken": ruStoreToken, "transportKind": "RuStore"});

  final body = json.encode(<String, dynamic>{
    "clientToken": token,
    if (deviceSettings.isNotEmpty) "deviceSettings": deviceSettings
  });
  print("Body: $body ");
  print("RuStoreToken: $ruStoreToken");

  try {
    var response = await http
        .post(Uri.parse('https://sub.pushed.ru/v2/tokens'),
            headers: {"Content-Type": "application/json"}, body: body)
        .timeout(const Duration(seconds: 10),
            onTimeout: (() => throw Exception("TimeOut")));
    token = json.decode(response.body)["model"]["clientToken"];
  } catch (e) {
    token = "";
  }
  return (token);
}