• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 无法在 Apple Watch 上获得可操作通知回调

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:16:01 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

我的 UNUserNotificationCenterDelegate 永远不会被调用!

这是 iOS 应用程序中的代码:

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 应用中)

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

    var message:String
    if isEntering {
        message = String(format: NSLocalizedString("OI less than %d meters", comment: ""), Int(poi.poiRegionRadius))
    } else {
        message = String(format: NSLocalizedString("OI 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) 吗?



Best Answer-推荐答案


我遇到了同样的问题,我通过将 UNNotificationAction 添加为 .foreground 类别来解决它。您必须更改以下行来修复它:

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

.foreground 将启动您的应用程序并调用 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 方法.您可以在此方法中为可操作按钮处理您的操作和重定向。示例如下:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
     switch response.actionIdentifier.lowercased() {
        case "call":
            WKInterfaceController.reloadRootControllers(withNames: ["CallInterface"], contexts: nil)
        default: break
     }
     completionHandler()
}

希望对你有帮助!

关于ios - 无法在 Apple Watch 上获得可操作通知回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47211564/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap