gpt4 book ai didi

ios - UNNotificationAction 按钮未显示,为什么?

转载 作者:行者123 更新时间:2023-11-30 11:31:37 28 4
gpt4 key购买 nike

我引用了Apple的代码,但不起作用,这是我的代码:

override func viewDidLoad() {
super.viewDidLoad()

let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert,.sound]) { (granted, error) in
print(granted)
}

//Set content
let content = UNMutableNotificationContent()
content.title = "Hello World"
content.body = "My First Notification App"
content.sound = UNNotificationSound.default()
//Set trigger
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
//Set request
let uuid = UUID().uuidString
let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)

//Set actions
let blueAction = UNNotificationAction(identifier: "ACTION_BLUE", title: "Blue", options: .init(rawValue: 0))
let greenAction = UNNotificationAction(identifier: "ACTION_GREEN", title: "Green", options: .init(rawValue: 0))
let category = UNNotificationCategory(identifier: "COLOR_ACTION", actions: [blueAction,greenAction], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "", options: .customDismissAction)
center.setNotificationCategories([category]) //category

center.add(request) { (err) in
if err != nil {
print(err)
}
}


// Do any additional setup after loading the view, typically from a nib.
}

这里是 UNUserNotificationCenterDelegate

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

if response.notification.request.content.categoryIdentifier == "COLOR_ACTION" {

}

completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}

我认为注释是错误的,但这不起作用。希望有人能帮助我,我现在真的很困惑哈哈。

最佳答案

您需要在调用 UNNotificationRequest 之前添加 content.categoryIdentifier = "COLOR_ACTION",如下面的 swift 代码。

    //Set content
let content = UNMutableNotificationContent()
content.title = "Hello World"
content.body = "My First Notification App"
content.sound = UNNotificationSound.default()

content.categoryIdentifier = "COLOR_ACTION"

以下是拖动通知时的输出: enter image description here

关于ios - UNNotificationAction 按钮未显示,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50201017/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com