From 4bde4547219f04d99fb2d21fcd88ad2200d4da4b Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Tue, 2 Jul 2024 19:53:07 -0700 Subject: [PATCH] Hide notifications (#773) * Updates for latest change to tags Signed-off-by: Dan Cunningham * revert preference changes Signed-off-by: Dan Cunningham * revert prefs changes Signed-off-by: Dan Cunningham --------- Signed-off-by: Dan Cunningham --- NotificationService/NotificationService.swift | 16 ++++++++-- openHAB.xcodeproj/project.pbxproj | 20 ++++++------- openHAB/AppDelegate.swift | 30 ++++++++++++++++++- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/NotificationService/NotificationService.swift b/NotificationService/NotificationService.swift index dad6de49..8b609930 100644 --- a/NotificationService/NotificationService.swift +++ b/NotificationService/NotificationService.swift @@ -25,7 +25,15 @@ class NotificationService: UNNotificationServiceExtension { if let bestAttemptContent { var notificationActions: [UNNotificationAction] = [] let userInfo = bestAttemptContent.userInfo - os_log("handleNotification userInfo %{PUBLIC}@", log: .default, type: .info, userInfo) + + os_log("didReceive userInfo %{PUBLIC}@", log: .default, type: .info, userInfo) + + if let title = userInfo["title"] as? String { + bestAttemptContent.title = title + } + if let message = userInfo["message"] as? String { + bestAttemptContent.body = message + } // Check if the user has defined custom actions in the payload if let actionsArray = parseActions(userInfo), let category = parseCategory(userInfo) { @@ -33,7 +41,7 @@ class NotificationService: UNNotificationServiceExtension { if let action = actionDict["action"], let title = actionDict["title"] { var options: UNNotificationActionOptions = [] - // navigate options need to bring the app forward + // navigate/browser options need to bring the app to the foreground if action.hasPrefix("ui") || action.hasPrefix("http") { options = [.foreground] } @@ -46,7 +54,7 @@ class NotificationService: UNNotificationServiceExtension { } } if !notificationActions.isEmpty { - os_log("handleNotification registering %{PUBLIC}@ for category %{PUBLIC}@", log: .default, type: .info, notificationActions, category) + os_log("didReceive registering %{PUBLIC}@ for category %{PUBLIC}@", log: .default, type: .info, notificationActions, category) let notificationCategory = UNNotificationCategory( identifier: category, @@ -189,6 +197,8 @@ class NotificationService: UNNotificationServiceExtension { os_log("Failed to parse data: %{PUBLIC}@", log: .default, type: .error, error.localizedDescription) } completion(nil) + } else { + completion(nil) } } } diff --git a/openHAB.xcodeproj/project.pbxproj b/openHAB.xcodeproj/project.pbxproj index b6130765..77423c95 100644 --- a/openHAB.xcodeproj/project.pbxproj +++ b/openHAB.xcodeproj/project.pbxproj @@ -1624,7 +1624,7 @@ CODE_SIGN_ENTITLEMENTS = openHABIntents/openHABIntents.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1580410538; + CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = dwarf; DEVELOPMENT_TEAM = PBAPXHRAM9; GCC_C_LANGUAGE_STANDARD = gnu11; @@ -1666,7 +1666,7 @@ CODE_SIGN_ENTITLEMENTS = openHABIntents/openHABIntents.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1580410538; + CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_TEAM = PBAPXHRAM9; GCC_C_LANGUAGE_STANDARD = gnu11; @@ -1724,7 +1724,7 @@ "@executable_path/../../Frameworks", ); LOCALIZATION_PREFERS_STRING_CATALOGS = YES; - MARKETING_VERSION = 1.0; + MARKETING_VERSION = 1.0.0; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = org.openhab.app.NotificationService; @@ -1769,7 +1769,7 @@ "@executable_path/../../Frameworks", ); LOCALIZATION_PREFERS_STRING_CATALOGS = YES; - MARKETING_VERSION = 1.0; + MARKETING_VERSION = 1.0.0; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = org.openhab.app.NotificationService; @@ -1795,7 +1795,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1580410538; + CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = dwarf; DEVELOPMENT_TEAM = PBAPXHRAM9; GCC_C_LANGUAGE_STANDARD = gnu11; @@ -1837,7 +1837,7 @@ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1580410538; + CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_TEAM = PBAPXHRAM9; GCC_C_LANGUAGE_STANDARD = gnu11; @@ -1878,7 +1878,7 @@ CODE_SIGN_ENTITLEMENTS = "openHABWatch Extension/openHABWatch Extension.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1580410538; + CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = dwarf; DEVELOPMENT_TEAM = PBAPXHRAM9; GCC_C_LANGUAGE_STANDARD = "compiler-default"; @@ -1923,7 +1923,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1580410538; + CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_TEAM = PBAPXHRAM9; GCC_C_LANGUAGE_STANDARD = "compiler-default"; @@ -1965,7 +1965,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1580410538; + CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = dwarf; DEVELOPMENT_TEAM = PBAPXHRAM9; GCC_C_LANGUAGE_STANDARD = gnu11; @@ -2008,7 +2008,7 @@ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1580410538; + CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_TEAM = PBAPXHRAM9; GCC_C_LANGUAGE_STANDARD = gnu11; diff --git a/openHAB/AppDelegate.swift b/openHAB/AppDelegate.swift index d3969742..338a1447 100644 --- a/openHAB/AppDelegate.swift +++ b/openHAB/AppDelegate.swift @@ -152,6 +152,35 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD os_log("Failed to get token for notifications: %{PUBLIC}@", log: .notifications, type: .error, error.localizedDescription) } + // this is called for "content-available" silent notifications (background notifications) + func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { + os_log("didReceiveRemoteNotification %{PUBLIC}@", log: .default, type: .info, userInfo) + // Hide notification logic + if let type = userInfo["type"] as? String, type == "hideNotification" { + if let refid = userInfo["reference-id"] as? String { + os_log("didReceiveRemoteNotification remove id %{PUBLIC}@", log: .default, type: .info, refid) + UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [refid]) + } + if let tag = userInfo["tag"] as? String { + UNUserNotificationCenter.current().getDeliveredNotifications { notifications in + let notificationsWithSeverity = notifications.filter { notification in + notification.request.content.userInfo["tag"] as? String == tag + } + + // Get the identifiers of these notifications + let identifiers = notificationsWithSeverity.map(\.request.identifier) + + if !identifiers.isEmpty { + os_log("didReceiveRemoteNotification remove tag %{PUBLIC}@ %{PUBLIC}@", log: .default, type: .info, tag, identifiers) + // Remove the filtered notifications + UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers) + } + } + } + } + completionHandler(.newData) + } + // this is called when a notification comes in while in the foreground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo @@ -164,7 +193,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD // this is called when clicking a notification while in the background func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { var userInfo = response.notification.request.content.userInfo - let actionIdentifier = response.actionIdentifier os_log("Notification clicked: action %{PUBLIC}@ userInfo %{PUBLIC}@", log: .notifications, type: .info, actionIdentifier, userInfo) if actionIdentifier != UNNotificationDismissActionIdentifier {