首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用UserNotifications扩展服务UNNotificationAction didReceive操作委派。

未调用UserNotifications扩展服务UNNotificationAction didReceive操作委派。
EN

Stack Overflow用户
提问于 2018-02-25 14:44:08
回答 1查看 530关注 0票数 0

我正在使用UNNotificationAction为本地通知创建通知服务扩展,但它的代理未被调用。

代码语言:javascript
复制
userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 

下面是我的3个动作

代码语言:javascript
复制
// 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])

要计划我使用的通知,请执行以下操作

代码语言:javascript
复制
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))")
            }
        }
    }

我的扩展正在显示,但是操作没有被触发&没有扩展,操作就可以正常工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-26 10:45:44

试试这样的东西:

代码语言:javascript
复制
    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的方法:

代码语言:javascript
复制
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {}

在这个代码块中,你可以检查你的动作。希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48970969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档