【发布时间】:2016-08-18 19:55:34
【问题描述】:
我在手表上使用前台通知:
class WorkoutInterfaceController: WKInterfaceController,
UNUserNotificationCenterDelegate
设置委托:
UNUserNotificationCenter.current().delegate = self
接收前台通知:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void)
{
print("Received Notification .....")
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
let completionSound: UNNotificationPresentationOptions = [.sound, .alert]
completionHandler(completionSound)
}
添加通知:
let content: UNMutableNotificationContent = UNMutableNotificationContent()
content.title = "TITLE"
content.subtitle = "sub title"
content.body = "message body"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: duration, repeats: false)
let request = UNNotificationRequest(identifier: restOverIdentifier,
content: content,
trigger: trigger)
UNUserNotificationCenter.current().add(request)
{
(error) in // ...
}
我在 sim 卡和设备上都试过了,但在 sim 卡被锁定或在主屏幕上或手表上,如果您不移动手臂,屏幕变黑时却没有收到通知。
我正在寻找的只是一个简单的哔声,我什至不需要通知警报屏幕。
如果您有对此有用的东西,请发布代码或确认此功能在手表上不起作用。
谢谢
格雷格
【问题讨论】:
标签: ios swift notifications watchkit watchos