From 977f01406c7ecc2e08f6b679b6c8cff8f32c7c37 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sun, 15 May 2022 21:08:42 -0400 Subject: [PATCH] Ahh this is tedious --- ntfy.xcodeproj/project.pbxproj | 4 ++++ ntfy/App.swift | 4 ++-- ntfy/AppDelegate.swift | 5 +++++ ntfyNSE/NotificationService.swift | 10 +++++++--- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/ntfy.xcodeproj/project.pbxproj b/ntfy.xcodeproj/project.pbxproj index 00323ed..e33dca6 100644 --- a/ntfy.xcodeproj/project.pbxproj +++ b/ntfy.xcodeproj/project.pbxproj @@ -23,6 +23,8 @@ 9474F1FD2831311A00CDE4DD /* SubscriptionAddView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1FC2831311A00CDE4DD /* SubscriptionAddView.swift */; }; 9474F1FF28316ACE00CDE4DD /* Subscription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1FE28316ACE00CDE4DD /* Subscription.swift */; }; 9474F20128316D1100CDE4DD /* Messenger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F20028316D1100CDE4DD /* Messenger.swift */; }; + 9474F2052831D51500CDE4DD /* Store.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1F82830835400CDE4DD /* Store.swift */; }; + 9474F2062831D73C00CDE4DD /* Model.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1F52830830700CDE4DD /* Model.xcdatamodeld */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -298,6 +300,8 @@ buildActionMask = 2147483647; files = ( 9474F1E7282F3FFD00CDE4DD /* NotificationService.swift in Sources */, + 9474F2052831D51500CDE4DD /* Store.swift in Sources */, + 9474F2062831D73C00CDE4DD /* Model.xcdatamodeld in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ntfy/App.swift b/ntfy/App.swift index e30f25e..6981117 100644 --- a/ntfy/App.swift +++ b/ntfy/App.swift @@ -12,7 +12,7 @@ import Firebase @main struct ntfyApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate: AppDelegate - @StateObject private var dataController = Store() + @StateObject private var store = Store() init() { FirebaseApp.configure() @@ -21,7 +21,7 @@ struct ntfyApp: App { var body: some Scene { WindowGroup { ContentView() - .environment(\.managedObjectContext, dataController.container.viewContext) + .environment(\.managedObjectContext, store.container.viewContext) } } } diff --git a/ntfy/AppDelegate.swift b/ntfy/AppDelegate.swift index d7aab76..2b51895 100644 --- a/ntfy/AppDelegate.swift +++ b/ntfy/AppDelegate.swift @@ -5,6 +5,7 @@ import Firebase import FirebaseCore // https://stackoverflow.com/a/41783666/1440785 +// https://stackoverflow.com/questions/47374903/viewing-core-data-data-from-your-app-on-a-device class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? @@ -22,6 +23,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { registerForPushNotifications() UNUserNotificationCenter.current().delegate = self + + + print("Documents Directory: ", FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last ?? "Not Found!") + // Check if launched from notification let notificationOption = launchOptions?[.remoteNotification] diff --git a/ntfyNSE/NotificationService.swift b/ntfyNSE/NotificationService.swift index ac0607e..a9c287a 100644 --- a/ntfyNSE/NotificationService.swift +++ b/ntfyNSE/NotificationService.swift @@ -13,12 +13,15 @@ import CoreData class NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent? -// var store: Store? + var store: Store? override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) - + store = Store() + + let context = store?.container.viewContext + print("NotificationService didReceive") if let bestAttemptContent = bestAttemptContent { // Modify the notification content here... bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" @@ -30,10 +33,11 @@ class NotificationService: UNNotificationServiceExtension { let notificationTimestampInt = Int64(notificationTimestamp), let notificationMessage = userInfo["message"] as? String { print("notification service \(notificationId)") -/* let notification = Notification(context: context) + /*let notification = Notification() notification.id = notificationId notification.time = notificationTimestampInt notification.message = notificationMessage*/ +// try? context?.save() } contentHandler(bestAttemptContent)