I would like to write a simple watchOs 7 app for the Apple Watch which is independent from an iPhone with Xcode 12 using SwiftUI 5.3.
(1) The app shall implement the option to create new time triggered alarms/notifications.
(2) The app shall show a dynamic notification (with customized) buttons (e.g. SNOOZE) when the trigger is fired.
(3) The app shall execute code when the SNOOZE button is pressed.
(1) and (2) are already solved, the problem is (3). I have found lots of manuals/howTos and git hub examples dealing with the issue, but I have not found anything for SwiftUI5.3. I have no idea, where to put the method
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler:
@escaping () -> Void)
which should be able to solve my problem.
These lines of code create the notification request (implemented in my class 'Alarm').
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
}
else if let error = error {
print(error.localizedDescription)
}
}
let content = UNMutableNotificationContent()
content.title = "title"
content.categoryIdentifier = "MyNotification"
content.body = "body"
content.sound = UNNotificationSound.default
// Configure the recurring date.
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = 12
dateComponents.minute = 30
// Create the trigger as a repeating event.
let trigger = UNCalendarNotificationTrigger(
dateMatching: dateComponents, repeats: true)
let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION",
title: "Snooze",
options: UNNotificationActionOptions(rawValue: 0))
let request = UNNotificationRequest(identifier: self.id.uuidString,
content: content, trigger: trigger)
let meetingInviteCategory =
UNNotificationCategory(identifier: "MyNotification",
actions: [snoozeAction],
intentIdentifiers: [],
options: .customDismissAction)
notificationCenter.setNotificationCategories([meetingInviteCategory])
notificationCenter.add(request) { (error) in
if error != nil {
print("error")
}
}
Any help is much appreciated!
question from:https://stackoverflow.com/questions/66064821/swift-5-3-handling-of-actions-from-dynamic-notifications-how-and-where