1

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.

Vijay
  • 791
  • 1
  • 8
  • 23

1 Answers1

0

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.

Gius
  • 89
  • 1
  • 1
  • 11
  • In order to answer you, I followed the tutorial that I linked to you. It works well for me. Maybe you forgot to ask for permission for notifications? Compare your code with [this](https://github.com/bartjacobs/ActionableNotificationsWithTheUserNotificationsFramework/blob/master/LocalNotifications/ViewController.swift). – Gius Jul 09 '20 at 09:55
  • Permission is asked in ```didFinishLaunchingWithOptions``` itself and it's granted. The necessary items to handle push notification & action buttons are done. I'm getting that error when app is inactive state. Have you ever seen the error like this? – Vijay Jul 09 '20 at 14:09
  • By creating a new project and copying exactly what you linked in the previous comment, I don't get any errors. I use iPhone 11 Pro simulator and iOS 13.5. Are you trying on a physical device? – Gius Jul 09 '20 at 15:05
  • Then It may be iOS version issue? I'm using iPhone 6S with iOS 12.3 I think. – Vijay Jul 13 '20 at 07:36
  • Unfortunately I can't test it on a device with iOS 12. Can you do a test with the simulator with iOS 13? – Gius Jul 13 '20 at 09:00
  • It's a Unity project which doesn't support simulator. I'll try to check with iOS 13 device. – Vijay Jul 13 '20 at 09:47