AppDelegate.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import SwiftUI
  2. import UIKit
  3. import UserNotifications
  4. class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject, UNUserNotificationCenterDelegate {
  5. func application(
  6. _ application: UIApplication,
  7. didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?
  8. ) -> Bool {
  9. UNUserNotificationCenter.current().delegate = self
  10. application.registerForRemoteNotifications()
  11. return true
  12. }
  13. func application(
  14. _: UIApplication,
  15. didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  16. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
  17. ) {
  18. debug(.remoteControl, "Received notification")
  19. Task {
  20. await TrioRemoteControl.shared.handleRemoteNotification(userInfo: userInfo)
  21. completionHandler(.newData)
  22. }
  23. }
  24. func application(
  25. _: UIApplication,
  26. didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  27. ) {
  28. let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
  29. let token = tokenParts.joined()
  30. Task {
  31. await TrioRemoteControl.shared.handleAPNSChanges(deviceToken: token)
  32. }
  33. }
  34. func application(
  35. _: UIApplication,
  36. didFailToRegisterForRemoteNotificationsWithError error: Error
  37. ) {
  38. debug(.remoteControl, "Failed to register for remote notifications: \(error.localizedDescription)")
  39. }
  40. }