WatchOS 3中的本地通知 [英] Local Notification in WatchOS 3
问题描述
我正在使用WatchOS 3 Beta,并尝试在手表上启动本地通知.该接口只是一个按钮,该按钮在下面的代码中调用"buttonPushed"方法.该应用程序运行正常,但我从未收到通知.对于WatchKit应用程序,该应用程序结构是Xcode 8中的默认结构.
I'm using the WatchOS 3 beta and trying to initiate a local notification on the watch. The interface is just one button which calls the "buttonPushed" method in the code below. The app runs fine but I never get a notification. The app structure is the default from Xcode 8 for a WatchKit app.
此代码在WatchKit扩展的InterfaceController.swift文件中
This code is in the InterfaceController.swift file of the WatchKit extension
我错过了完全显而易见的东西吗?
Am I missing something totally obvious?
@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
}
}
推荐答案
根据此.您应该每次为请求指定一个唯一的新标识符.
According to this. You should specify a unique and new identifier for request each time.
我的:
let id = String(Date().timeIntervalSinceReferenceDate)
let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
这篇关于WatchOS 3中的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!