TrioRemoteControl+APNS.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import Foundation
  2. extension TrioRemoteControl {
  3. internal func handleAPNSChanges(deviceToken: String?) async {
  4. let previousDeviceToken = UserDefaults.standard.string(forKey: "deviceToken")
  5. let previousIsAPNSProduction = UserDefaults.standard.bool(forKey: "isAPNSProduction")
  6. let isAPNSProduction = isRunningInAPNSProductionEnvironment()
  7. var shouldUploadProfiles = false
  8. if let token = deviceToken, token != previousDeviceToken {
  9. UserDefaults.standard.set(token, forKey: "deviceToken")
  10. debug(.remoteControl, "Device token updated: \(token)")
  11. shouldUploadProfiles = true
  12. }
  13. if previousIsAPNSProduction != isAPNSProduction {
  14. UserDefaults.standard.set(isAPNSProduction, forKey: "isAPNSProduction")
  15. debug(.remoteControl, "APNS environment changed to: \(isAPNSProduction ? "Production" : "Sandbox")")
  16. shouldUploadProfiles = true
  17. }
  18. if shouldUploadProfiles {
  19. await nightscoutManager.uploadProfiles()
  20. } else {
  21. debug(.remoteControl, "No changes detected in device token or APNS environment.")
  22. }
  23. }
  24. private func isRunningInAPNSProductionEnvironment() -> Bool {
  25. BuildDetails.default.isTestFlightBuild()
  26. }
  27. }