2

在 Apple 的Frontmost App Behavior文档中,指出

“[...] 当最前面的应用程序收到通知时,它可以播放触觉反馈或自定义声音,而不是显示通知警报,然后更新其用户界面。”

但是,我无法实现这一点。现在,按下按钮后,我在安排通知之前将应用程序声明为最前面:

@IBAction func StartButtonPressed() {
    WKExtension.shared().isFrontmostTimeoutExtended = true
    scheduleNotification()
}

当应用程序在前台收到通知时,它会播放一个触觉:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    //MARK: Handle foreground notification here
    WKInterfaceDevice.current().play(.success)
    print("Foreground notification received")
    return
}

这在用户的手腕抬起时有效,但在用户点击按钮后降低手腕时无效。当手腕抬起或放下时,控制台会打印收到的前台通知。

如果不是播放触觉,我设置completionHandler([.alert,.sound])通知在手腕抬起或放下时提醒用户。这是我想要的行为,但仅限于触觉。

我该如何实施?

4

1 回答 1

0

检查applicationState。如果状态为或play(:),则方法不起作用。backgroundinactive

当您的共享 WKExtension 对象的 applicationState 属性为背景或非活动状态时调用此方法无效。默认情况下,您无法在后台播放触觉反馈。唯一的例外是具有活跃锻炼会话的应用程序。有关更多信息,请参阅 HKWorkoutSession 中的后台运行。

https://developer.apple.com/documentation/watchkit/wkinterfacedevice/1628128-play

于 2017-10-25T18:47:55.260 回答