Search code examples
iosswiftios10unusernotificationcenter

User Notification request always come with default action identifier


I am using the UNUserNotificationCenterDelegate (> ios 10) and one of the delegate methods where I can check the response from the notification has always actionIdentifier equal "com.apple.UNNotificationDefaultActionIdentifier" no matter what I do. The "response.notification.request.content.categoryIdentifier" comes right, with the expected value, but the request.actionIdentifier never comes correctly ("mycustomactionidentifier" in the example below). Does anyone know if I'm missing something?

extension NotificationManager: UNUserNotificationCenterDelegate {


    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {

        completionHandler([.alert,.sound])
    }

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

        if response.notification.request.content.categoryIdentifier == "TEST" {
            if response.actionIdentifier == "mycustomactionidentifier" {
                NSLog("it finally works dude!")
            }
        }

        completionHandler()
    }
}

I added the action and category to the Notification center:

    let uploadAction = UNNotificationAction(identifier: "mycustomactionidentifier", title: "Uploaded", options: [])
    let category = UNNotificationCategory(identifier: "TEST", actions: [uploadAction], intentIdentifiers: [])
    center.setNotificationCategories([category])

and am sending the request putting the correct identifier:

    let uploadContent = UNMutableNotificationContent()
    uploadContent.title = String(number) + " asset(s) added"
    uploadContent.body = "Check your inventory to manage your assets!"
    uploadContent.categoryIdentifier = "TEST" 

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 6, repeats: false)

    let uploadRequestIdentifier = "mycustomactionidentifier"
    let uploadRequest = UNNotificationRequest(identifier: uploadRequestIdentifier, content: uploadContent, trigger: trigger)
    UNUserNotificationCenter.current().add(uploadRequest, withCompletionHandler: nil)

Solution

  • Firstly: Register your custom actions:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
    
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            if granted {
             // Access granted
            } else {
             // Access denied
            }
        }
    
        self.registerNotificationAction()
    
        return true
    }
    
    func registerNotificationAction() {
    
        let first = UNNotificationAction.init(identifier: "first", title: "Action", options: [])
        let category = UNNotificationCategory.init(identifier: "categoryIdentifier", actions: [first], intentIdentifiers: [], options: [])
        UNUserNotificationCenter.current().setNotificationCategories([category])
    }
    

    And create a content with a unique identifier:

    func scheduleNotification() {
    
        // Create a content
        let content = UNMutableNotificationContent.init()
        content.title = NSString.localizedUserNotificationString(forKey: "Some title", arguments: nil)
        content.body = NSString.localizedUserNotificationString(forKey: "Body of notification", arguments: nil)
        content.sound = UNNotificationSound.default()
        content.categoryIdentifier = "categoryIdentifier"
    
        // Create a unique identifier for each notification
        let identifier = UUID.init().uuidString
    
        // Notification trigger
        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
    
        // Notification request
        let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
    
        // Add request
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    
    }
    

    Lastly: Handle the notification with their default and custom actions.

       extension AppDelegate: UNUserNotificationCenterDelegate {
    
        func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
            if response.notification.request.content.categoryIdentifier == "categoryIdentifier" {
    
                switch response.actionIdentifier {
                case UNNotificationDefaultActionIdentifier:
                    print(response.actionIdentifier)
                    completionHandler()
                case "first":
                    print(response.actionIdentifier)
                    completionHandler()
                default:
                    break;
                }
            }
        }
    
        func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    
            completionHandler([.alert, .sound])
        }
    }
    

    Hope it helps!

    Second Edition

    Here's the results: This is going to be our UNNotificationDefaultActionIdentifier:

    UNNotificationDefaultActionIdentifier

    And this one is expanded version of the notification, we could handle both actions:

    Expanded notification