微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过 UserNotifications 安排的本地通知未显示在手表上

如何解决通过 UserNotifications 安排的本地通知未显示在手表上

我正在尝试使用以下代码在 WatchOS 上安排本地通知,但手表上没有显示任何内容

// sending notifications
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert,.sound]) { (granted,error) in
    // Enable or disable features based on authorization.
    print("hello?")
}
let content = UNMutableNotificationContent()
content.title = Nsstring.localizedUserNotificationString(forKey: "Hello!",arguments: nil)
content.body = Nsstring.localizedUserNotificationString(forKey: "Hello_message_body",arguments: nil)
content.sound = UNNotificationSound.defaultCritical
content.categoryIdentifier = "REMINDER_CATEGORY"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1,repeats: false)
let id = String(Date().timeIntervalSinceReferenceDate)
let request = UNNotificationRequest(identifier: id,content: content,trigger: trigger)
center.add(request,withCompletionHandler: {(_ error: Error?) -> Void in
    if error != nil {
        print("Something went wrong: \(String(describing: error))")
    } else {
        print("notif triggered successfully")
    }
})

“notif 触发成功”打印显示在日志中。关于这里可能发生什么的任何想法?谢谢!

解决方法

我想通了!通知有效,但我需要将应用设置为后台才能显示。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。