通过 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 触发成功”打印显示在日志中。关于这里可能发生什么的任何想法?谢谢!

解决方法

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...