Swift interactive push notification action failed

303 Views Asked by At

I've implemented the interactive push notification in my application like below,

in didFinishLaunchingWithOptions set

UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (_, _) in }

let accept = UNNotificationAction(identifier: "accept", title: "Accept", options: [])
let deny = UNNotificationAction(identifier: "deny", title: "Deny", options: [])
let category = UNNotificationCategory(identifier: "newFriendRequest", actions: [accept, deny], intentIdentifiers: [], options: [])

UNUserNotificationCenter.current().setNotificationCategories([category])

and

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .sound])
}
    
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    NSLog("FR_ didReceive userNotification")
    completionHandler()
}

When I click the any action button in push notification when app is inactive state I'm getting the following error in Console App.

FR_ didReceive userNotification
Application background launch action for notification response action deny recieved action response <BSActionResponse: 0x28043c700; error: <NSError: 0x2808847b0; domain: BSActionErrorDomain; code: 4> {
        description = "The operation couldn’t be completed. (BSActionErrorDomain error 4.)";
    }>

Note: I'm getting this error if I handled or not handled the didReceive response.

1

There are 1 best solutions below

6
Gius On

Try doing something like this:

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

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    switch response.actionIdentifier {
    case "accept":
        print("Accepted")
    case "deny":
        print("Denied")
    default:
        print("Other Action")
    }

    completionHandler()
}

For more details see this tutorial.