gpt4 book ai didi

swift - UserNotifications 扩展服务 UNNotificationAction didReceive 操作委托(delegate)未被调用。

转载 作者:行者123 更新时间:2023-11-30 11:44:42 27 4
gpt4 key购买 nike

我正在使用 UNNotificationAction 创建本地通知的通知服务扩展,但点击它代表不会被调用。

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

这是我的 3 个行动

// Define Actions
let actionReadLater = UNNotificationAction(identifier: Notification.Action.readLater, title: "Read Later", options: [])
let actionShowDetails = UNNotificationAction(identifier: Notification.Action.showDetails, title: "Show Details", options: [.foreground])
let actionUnsubscribe = UNNotificationAction(identifier: Notification.Action.unsubscribe, title: "Unsubscribe", options: [.destructive, .authenticationRequired])

// Define Category
let tutorialCategory = UNNotificationCategory(identifier: Notification.Category.tutorial, actions: [actionReadLater, actionShowDetails, actionUnsubscribe], intentIdentifiers: [], options: [])

// Register Category
UNUserNotificationCenter.current().setNotificationCategories([tutorialCategory])

安排我正在使用的通知

private func scheduleLocalNotification() {
// Create Notification Content
let notificationContent = UNMutableNotificationContent()

// Configure Notification Content
notificationContent.title = "Cocoacasts"
notificationContent.subtitle = "Local Notifications"
notificationContent.body = "In this tutorial, you learn how to schedule local notifications with the User Notifications framework."
notificationContent.userInfo = ["customNumber": 100]
// Set Category Identifier
notificationContent.categoryIdentifier = Notification.Category.tutorial

// Add Trigger
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)

// Create Notification Request
let notificationRequest = UNNotificationRequest(identifier: "exampleNotification", content: notificationContent, trigger: notificationTrigger)

// Add Request to User Notification Center
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
if let error = error {
print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
}
}
}

我的扩展程序正在显示,但操作没有被触发,并且没有扩展程序操作也可以正常工作。

最佳答案

尝试类似的事情:

    class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var label: UILabel?


override func viewDidLoad() {
super.viewDidLoad()
// Do any required interface initialization here.

preferredContentSize = CGSize.init(width: self.view.bounds.width / 2, height: self.view.bounds.height / 5)
}

func didReceive(_ notification: UNNotification) {
self.label?.text = "Extension"
}


// Implement this method
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {

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

switch response.actionIdentifier {

case "first":
// Do something
completion(.dismissAndForwardAction)

case "second":

// Do something
completion(.dismissAndForwardAction)

default:
break;
}
}
}
}

您应该实现 UNNotificationContentExtension 的方法:

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {}

在此 block 内,您可以检查您的操作。希望对您有所帮助。

关于swift - UserNotifications 扩展服务 UNNotificationAction didReceive 操作委托(delegate)未被调用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48970969/

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