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

Posted

技术标签:

【中文标题】未调用 UserNotifications 扩展服务 UNNotificationAction didReceive 操作委托。【英文标题】:UserNotifications Extension Service UNNotificationAction didReceive action delegate not getting called. 【发布时间】:2018-02-25 06:44:08 【问题描述】:

我正在使用 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))")
            
        
    

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

【问题讨论】:

你能解释一下你的主要设计目标吗?应该怎么样? @Mannopson 这是一个本地通知,我在下面添加了带有 UIViewcontroller 的扩展名,当我点击按钮时,我有 3 个 UNNotificationAction 按钮,如果没有扩展名,委托不会触发默认其工作正常 【参考方案1】:

试试这样的:

    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) 

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

【讨论】:

以上是关于未调用 UserNotifications 扩展服务 UNNotificationAction didReceive 操作委托。的主要内容,如果未能解决你的问题,请参考以下文章

蚂蚁金服开源 Java RPC 框架-SOFARPC v5.7.0 发布

未调用 SiriKit 扩展

调试时未调用推送通知扩展

Swift 2.3 中没有这样的模块“UserNotifications”

未调用 iOS 推送通知服务扩展 (didReceive)

未调用 iOS 13 缩略图扩展 (QLThumbnailProvider)