Skip to content

Commit

Permalink
Hide notifications (#773)
Browse files Browse the repository at this point in the history
* Updates for latest change to tags

Signed-off-by: Dan Cunningham <[email protected]>

* revert preference changes

Signed-off-by: Dan Cunningham <[email protected]>

* revert prefs changes

Signed-off-by: Dan Cunningham <[email protected]>

---------

Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan authored Jul 3, 2024
1 parent 2d52489 commit 4bde454
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
16 changes: 13 additions & 3 deletions NotificationService/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@ 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) {
for actionDict in actionsArray {
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]
}
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions openHAB.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 29 additions & 1 deletion openHAB/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 4bde454

Please sign in to comment.