| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import SwiftUI
- import UIKit
- import UserNotifications
- class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject, UNUserNotificationCenterDelegate {
- func application(
- _ application: UIApplication,
- didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?
- ) -> Bool {
- application.registerForRemoteNotifications()
- return true
- }
- func application(
- _: UIApplication,
- didReceiveRemoteNotification userInfo: [AnyHashable: Any],
- fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
- ) {
- debug(.remoteControl, "Received notification")
- do {
- let jsonData = try JSONSerialization.data(withJSONObject: userInfo)
- let pushMessage = try JSONDecoder().decode(PushMessage.self, from: jsonData)
- Task {
- do {
- try await TrioRemoteControl.shared.handleRemoteNotification(pushMessage: pushMessage)
- completionHandler(.newData)
- } catch {
- debug(
- .default,
- "\(DebuggingIdentifiers.failed) failed to handle remote notification with error: \(error.localizedDescription)"
- )
- }
- }
- } catch {
- debug(.remoteControl, "Error decoding push message: \(error.localizedDescription)")
- completionHandler(.failed)
- }
- }
- func application(
- _: UIApplication,
- didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
- ) {
- let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
- let token = tokenParts.joined()
- Task {
- do {
- try await TrioRemoteControl.shared.handleAPNSChanges(deviceToken: token)
- } catch {
- debug(
- .remoteControl,
- "\(DebuggingIdentifiers.failed) failed to register for remote notifications: \(error.localizedDescription)"
- )
- }
- }
- }
- func application(
- _: UIApplication,
- didFailToRegisterForRemoteNotificationsWithError error: Error
- ) {
- debug(.remoteControl, "Failed to register for remote notifications: \(error.localizedDescription)")
- }
- }
|