WatchOS 3 上的后台通知 (XCode 8b6)

Background Notifications on WatchOS 3 (XCode 8b6)

我在手表上有前台通知:

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

我只需要一声简单的哔哔声,我什至不需要通知警报屏幕。

请post 代码,如果你有一些为此工作或确认此功能不适用于手表。

谢谢

格雷格

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
}

虽然委托完成处理程序似乎还没有在手表上工作,但似乎可以解决问题。