gpt4 book ai didi

ios - WatchOS 3 中的本地通知

转载 作者:可可西里 更新时间:2023-11-01 03:45:21 26 4
gpt4 key购买 nike

我正在使用 WatchOS 3 beta 并尝试在 watch 上启动本地通知。该界面只是一个按钮,在下面的代码中调用“buttonPushed”方法。该应用程序运行良好,但我从未收到通知。应用程序结构是 Xcode 8 中 WatchKit 应用程序的默认结构。

这段代码在WatchKit扩展的InterfaceController.swift文件中

我是否漏掉了一些非常明显的东西?

@IBAction func buttonPushed() {
sendMyNotification()
}

func sendMyNotification(){
if #available(watchOSApplicationExtension 3.0, *) {

let center = UNUserNotificationCenter.current()

center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}


let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Hello_message_body", arguments: nil)
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "REMINDER_CATEGORY"
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)

// Schedule the notification.

center.add(request ,withCompletionHandler: nil)



} else {
// Fallback on earlier versions
}


}

最佳答案

根据 this .您应该为每次请求指定一个唯一的新标识符。

我的:

let id = String(Date().timeIntervalSinceReferenceDate)
let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)

关于ios - WatchOS 3 中的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38937385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com