WatchOS 3上的背景通知(XCode 8b6)

时间:2016-08-18 19:55:35

标签: ios swift notifications watchkit watch-os

我在观看时使用前景通知:

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卡被锁定或在主屏幕上或者在屏幕变黑时如果你不移动你的手臂,则没有运气通知。

我正在寻找的是一个简单的嘟嘟声,我甚至不需要通知警报屏幕。

如果您有相关信息,请发布代码或确认此功能无法正常使用。

由于

格雷格

1 个答案:

答案 0 :(得分:2)

watchOS 3存在一个错误,只显示第一个通知:

暂时将我的代码更改为:

    let id: String = WatchNotify.getUUID()

    //        let request = UNNotificationRequest(identifier: focusWarningIdentifier,
    let request = UNNotificationRequest.init(identifier: id,
                                        content: content,
                                        trigger: trigger)

    UNUserNotificationCenter.current().add(request)

使用UUID的这个附加功能:

class func getUUID() -> String
{
    let uuidObj = CFUUIDCreate(nil)
    let uuidString = CFUUIDCreateString(nil, uuidObj)!
    return uuidString as String
}

虽然委托完成处理程序似乎尚未在手表上运行,但似乎可以解决问题。