Search code examples
swiftlocalnotificationunusernotificationcenter

UserNotifications Extension Service UNNotificationAction didReceive action delegate not getting called.


I am creating Notification Service extension for local notification with UNNotificationAction but on tap of it delegate not getting called.

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

here are my 3 actions

// 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])

to schedule notification I am using

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

My extension is getting displayed but actions are not getting triggered & without extension actions are working fine.


Solution

  • Try something like that:

        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;
                }
            }
        }
    }
    

    You should implement the UNNotificationContentExtension's method:

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

    And inside of this block you can check your actions. Hope it helps.