From 2520ccef65b76a5b515d24fbacafe7aa628f7f21 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Wed, 18 May 2022 20:16:30 -0400 Subject: [PATCH] Logging and such --- .../xcdebugger/Breakpoints_v2.xcbkptlist | 72 +++++++++++++++++++ .../xcschemes/xcschememanagement.plist | 4 +- ntfy/App/AppDelegate.swift | 57 ++++++--------- ntfy/Persistence/Store.swift | 3 + ntfyNSE/NotificationService.swift | 5 +- 5 files changed, 104 insertions(+), 37 deletions(-) create mode 100644 ntfy.xcodeproj/xcuserdata/pheckel.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist diff --git a/ntfy.xcodeproj/xcuserdata/pheckel.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ntfy.xcodeproj/xcuserdata/pheckel.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..4f806fc --- /dev/null +++ b/ntfy.xcodeproj/xcuserdata/pheckel.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + diff --git a/ntfy.xcodeproj/xcuserdata/pheckel.xcuserdatad/xcschemes/xcschememanagement.plist b/ntfy.xcodeproj/xcuserdata/pheckel.xcuserdatad/xcschemes/xcschememanagement.plist index d407d7f..cffa0e4 100644 --- a/ntfy.xcodeproj/xcuserdata/pheckel.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/ntfy.xcodeproj/xcuserdata/pheckel.xcuserdatad/xcschemes/xcschememanagement.plist @@ -28,12 +28,12 @@ ntfy.xcscheme_^#shared#^_ orderHint - 1 + 0 ntfyNSE.xcscheme_^#shared#^_ orderHint - 0 + 1 diff --git a/ntfy/App/AppDelegate.swift b/ntfy/App/AppDelegate.swift index 5e0dfa9..513ceea 100644 --- a/ntfy/App/AppDelegate.swift +++ b/ntfy/App/AppDelegate.swift @@ -17,6 +17,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { Log.d(tag, "ApplicationDelegate didFinishLaunchingWithOptions.") + // FirebaseApp.configure() DOES NOT WORK FirebaseConfiguration.shared.setLoggerLevel(.max) Messaging.messaging().delegate = self @@ -27,50 +28,38 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } - - func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { - guard let aps = userInfo["aps"] as? [String: AnyObject] else { - completionHandler(.failed) - return - } - print("didReceiveRemoteNotification") - print(userInfo) - } - - - func application(_ application: UIApplication, - didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { - print("didReceiveRemoteNotification 2") - - guard let aps = userInfo["aps"] as? [String: AnyObject] else { - return - } - print(userInfo) + func application( + _ application: UIApplication, + didReceiveRemoteNotification userInfo: [AnyHashable : Any], + fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void + ) { + Log.d(tag, "Called didReceiveRemoteNotification (with completionHandler). This is a no-op.", userInfo) } func application( _ application: UIApplication, - didFailToRegisterForRemoteNotificationsWithError error: Error + didReceiveRemoteNotification userInfo: [AnyHashable: Any] ) { - print("Failed to register: \(error)") + Log.d(tag, "Called didReceiveRemoteNotification (without completionHandler). This is a no-op.", userInfo) } + func application( + _ application: UIApplication, + didFailToRegisterForRemoteNotificationsWithError error: Error + ) { + Log.e(tag, "Failed to register for remote notifications", error) + } - - // This function is added here only for debugging purposes, and can be removed if swizzling is enabled. - // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to - // the FCM registration token. - func application(_ application: UIApplication, - didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { - print("APNs token retrieved: \(deviceToken)") - - // With swizzling disabled you must set the APNs token here. + func application( + _ application: UIApplication, + didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data + ) { + let token = deviceToken + .map { data in String(format: "%02.2hhx", data) } + .joined() + Log.d(tag, "Registered for remote notifications. Passing APNs token to Firebase: \(token)") Messaging.messaging().apnsToken = deviceToken - - let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) } - let token = tokenParts.joined() - print("Device Token: \(token)") } func registerForPushNotifications() { diff --git a/ntfy/Persistence/Store.swift b/ntfy/Persistence/Store.swift index 0b5e8e0..c3c5422 100644 --- a/ntfy/Persistence/Store.swift +++ b/ntfy/Persistence/Store.swift @@ -20,6 +20,7 @@ class Store: ObservableObject { let storeUrl = directory.appendingPathComponent("ntfy.sqlite") let description = NSPersistentStoreDescription(url: storeUrl) + // Set up container and observe changes from app extension container = NSPersistentContainer(name: "Model") container.persistentStoreDescriptions = [description] container.loadPersistentStores { description, error in @@ -27,6 +28,8 @@ class Store: ObservableObject { print("Core Data failed to load: \(error.localizedDescription)") } } + + // Shortcut for context context = container.viewContext } diff --git a/ntfyNSE/NotificationService.swift b/ntfyNSE/NotificationService.swift index 2b2366b..24a8f44 100644 --- a/ntfyNSE/NotificationService.swift +++ b/ntfyNSE/NotificationService.swift @@ -26,7 +26,10 @@ class NotificationService: UNNotificationServiceExtension { bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" let userInfo = bestAttemptContent.userInfo - Store.shared.saveNotification(fromUserInfo: userInfo) + let store = Store.shared + + store.saveNotification(fromUserInfo: userInfo) + contentHandler(bestAttemptContent) } }