Swift 5.3:处理来自动态通知的操作:如何以及在哪里?

问题描述

我想为 Apple Watch 编写一个简单的 watchOs 7 应用程序,它独立于使用 SwiftUI 5.3 的 Xcode 12 的 iPhone。

(1) 应用应实现创建新的时间触发警报/通知的选项。

(2) 当触发器被触发时,应用程序应显示动态通知(带有自定义)按钮(例如 SNOOZE)。

(3) 应用程序应在按下 SNOOZE 按钮时执行代码。

(1)和(2)已经解决了,问题是(3)。我找到了很多处理这个问题的手册/howTos 和 git hub 示例,但我没有找到任何适用于 SwiftUI5.3 的内容。我不知道把方法放在哪里

func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: 
               @escaping () -> Void)

应该可以解决我的问题。

这些代码行创建了通知请求(在我的“警报”类中实现)。

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")
   }
}

非常感谢任何帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...