getNewToken method

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

Implementation

Future<String> getNewToken(String token,
    {String? apnsToken, String? fcmToken, String? hpkToken}) 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"});

  final body = json.encode(<String, dynamic>{
    "clientToken": token,
    if (deviceSettings.isNotEmpty) "deviceSettings": deviceSettings
  });
  print("Body: $body");
  try {
    var response = await http
        .post(Uri.parse('https://sub.pushed.ru/tokens'),
            headers: {"Content-Type": "application/json"}, body: body)
        .timeout(const Duration(seconds: 10),
            onTimeout: (() => throw Exception("TimeOut")));
    token = json.decode(response.body)["token"];
  } catch (e) {
    token = "";
  }
  return (token);
}