我有一个推送通知的iOS应用程序(例如,当用户进入某个地理区域时) . 我还有一个Watch应用程序,可以在iPhone锁定时收到这些通知 . 我在这些通知中有2个操作(“呼叫”,“转到”),它们在Apple Watch通知中正确显示,但当用户触摸其中一个操作时,回调

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

从我的 UNUserNotificationCenterDelegate 永远不会被召唤!

这是iOS App中的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Register local notification (alert & sound)
    // They are used to notify the user when entering/exiting a monitored region
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in
        if let theError = error {
            NSLog("\(#function) error when request localNotification \(theError.localizedDescription)")
        } else {
            if !granted {
                NSLog("\(#function) Warning local notification not granted")
            }
        }

     })

    var actions = [UNNotificationAction]()
    actions.append(UNNotificationAction(identifier: "CallId", title: "Call", options:[] )) // UNNotificationActionOptions.authenticationRequired
    actions.append(UNNotificationAction(identifier: "CallId2", title: "Go To", options:[UNNotificationActionOptions.authenticationRequired] ))

    let notificationCategory = UNNotificationCategory(identifier: "MonitoringRegionCategory", actions: actions, intentIdentifiers: [],  options: [])
    let categories: Set = [notificationCategory]
    UNUserNotificationCenter.current().setNotificationCategories(categories)
    return true
}

发送通知的代码(在iOS App中)

let content = UNMutableNotificationContent()
    content.title = "title"
    content.subtitle = "subtitle"

    var message:String
    if isEntering {
        message = String(format: NSLocalizedString("POI less than %d meters", comment: ""), Int(poi.poiRegionRadius))
    } else {
        message = String(format: NSLocalizedString("POI more than %d meters", comment: ""), Int(poi.poiRegionRadius))
    }

    content.body = message
    content.badge = 1
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "MonitoringRegionCategory"
    let request = UNNotificationRequest(identifier: AppDelegate.LocalNotificationId.monitoringRegionId, content:content, trigger: nil)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
        if let theError = error {
            NSLog("\(#function) Error with notification add \(theError.localizedDescription)")
        }
    })

在WatchApp中编码以注册和实现UNUserNotificationCenterDelegate

class ExtensionDelegate: NSObject, WKExtensionDelegate, UNUserNotificationCenterDelegate {

    func applicationDidFinishLaunching() {
        NSLog("\(#function)")


        // Perform any final initialization of your application.
        UNUserNotificationCenter.current().delegate = self
}

  // MARK: UNUserNotificationCenterDelegate
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        NSLog("\(#function)")

        completionHandler()
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        NSLog("\(#function)")
        completionHandler(.alert)
    }

知道为什么userNotificationCenter(_ center:UNUserNotificationCenter,didReceive response:UNNotificationResponse,withCompletionHandler completionHandler:@escaping() - > Void)在用户从通知中选择一个按钮时从不被调用?