AppDelegate.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import FirebaseCore
  2. import FirebaseCrashlytics
  3. import SwiftUI
  4. import UIKit
  5. import UserNotifications
  6. class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject, UNUserNotificationCenterDelegate {
  7. func application(
  8. _: UIApplication,
  9. didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?
  10. ) -> Bool {
  11. FirebaseApp.configure()
  12. // Default to `true` if the key doesn't exist
  13. let crashReportingEnabled: Bool = PropertyPersistentFlags.shared.diagnosticsSharingEnabled ?? true
  14. // The docs say that changes to this don't take effect until
  15. // the next app boot, but this is fine since the app will need
  16. // to boot after a crash
  17. Crashlytics.crashlytics().setCrashlyticsCollectionEnabled(crashReportingEnabled)
  18. Crashlytics.crashlytics().setCustomValue(Bundle.main.appDevVersion ?? "unknown", forKey: "app_dev_version")
  19. return true
  20. }
  21. func application(
  22. _: UIApplication,
  23. didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  24. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
  25. ) {
  26. debug(.remoteControl, "Received notification")
  27. do {
  28. let jsonData = try JSONSerialization.data(withJSONObject: userInfo)
  29. let encryptedMessage = try JSONDecoder().decode(EncryptedPushMessage.self, from: jsonData)
  30. Task {
  31. do {
  32. try await TrioRemoteControl.shared.handleRemoteNotification(encryptedData: encryptedMessage.encryptedData)
  33. completionHandler(.newData)
  34. } catch {
  35. debug(
  36. .default,
  37. "\(DebuggingIdentifiers.failed) failed to handle remote notification with error: \(error)"
  38. )
  39. completionHandler(.failed)
  40. }
  41. }
  42. } catch {
  43. debug(.remoteControl, "Error decoding push message shell: \(error)")
  44. completionHandler(.failed)
  45. }
  46. }
  47. func application(
  48. _: UIApplication,
  49. didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  50. ) {
  51. let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
  52. let token = tokenParts.joined()
  53. Task {
  54. do {
  55. try await TrioRemoteControl.shared.handleAPNSChanges(deviceToken: token)
  56. } catch {
  57. debug(
  58. .remoteControl,
  59. "\(DebuggingIdentifiers.failed) failed to register for remote notifications: \(error)"
  60. )
  61. }
  62. }
  63. }
  64. func application(
  65. _: UIApplication,
  66. didFailToRegisterForRemoteNotificationsWithError error: Error
  67. ) {
  68. debug(.remoteControl, "Failed to register for remote notifications: \(error)")
  69. }
  70. }