【问题标题】:Local notification scheduled through UserNotifications not showing on watch通过 UserNotifications 安排的本地通知未在手表上显示
【发布时间】:2021-04-06 10:05:40
【问题描述】:

我正在尝试使用以下代码在 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")
    }
})

“通知已成功触发”打印显示在日志中。关于这里可能发生的事情有什么想法吗?谢谢!

【问题讨论】:

    标签: swift watchos usernotifications


    【解决方案1】:

    我想通了!通知正在运行,但我需要将应用程序设置为后台才能显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多