TPe*_*ter 6 uilocalnotification swift apple-watch watchkit watchos-2
我正在为 Apple Watch 开发一个计时器应用程序。
在特定时间(计时器结束),iPhone 会触发计划UILocalNotification,在不活动的 Apple Watch 上应触发触觉警报,告诉用户计时器已结束。
我这样做是因为一旦手表处于非活动状态,计时器就不会继续在 Apple Watch 上运行。
我面临的问题是:
a) 通知有时会显示在 iPhone 上而不是 Apple Watch 上(基于其他帖子,我担心这无法更改...)
b) 手表上没有触觉警报(纯粹显示通知,这仅在用户下次激活手表时,而不是在实际触发通知时)
func sendAwakeNotification(nextTime:NSDate) {
print("AppDelegate: - sendAwakeNotification")
dispatch_async(dispatch_get_main_queue()) { () -> Void in
let localNotification = UILocalNotification()
localNotification.fireDate = nextTime
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.alertBody = "Finished Step"
localNotification.alertTitle = "Alert"
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.category = "myTimer"
let userInfo: [NSObject : AnyObject] = [
"notification_id" : "myTimerNotification"
]
localNotification.userInfo = userInfo
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
}
Run Code Online (Sandbox Code Playgroud)
通知中的代码
override func didReceiveLocalNotification(localNotification: UILocalNotification, withCompletion completionHandler: ((WKUserNotificationInterfaceType) -> Void)) {
print("received notification")
dispatch_async(dispatch_get_main_queue()) { () -> Void in
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.postNotificationName(NotificationAlertFromPhone, object: nil)
}
completionHandler(.Custom)
}
Run Code Online (Sandbox Code Playgroud)
我试图直接在 NotificationController 中触发触觉警报(WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Notification)),但没有奏效。因此,我恢复发送应由 ExtensionDelegate 接收以触发触觉的通知。
这是 ExtensionDelegate 中的代码:
private func setupNotificationCenter() {
print("ExtensionDelegate: - setupNotificationCenter")
notificationCenter.addObserverForName(NotificationAlertFromPhone, object: nil, queue: nil) { (notification:NSNotification) -> Void in
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Notification)
}
}
Run Code Online (Sandbox Code Playgroud)
小智 3
a) the notification is sometimes shown on the iPhone rather than on the apple watch (based on other posts, I fear that this cannot be changed...)
That's correct. It's up to iOS to determine where to show the notification.
If your iPhone is unlocked, you'll get notifications on your iPhone, instead of your Apple Watch.
If your iPhone is locked or asleep, you'll get notifications on your Apple Watch, unless your Apple Watch is locked with your passcode.
When your local notification fires in the future, it may be shown on the phone. In that case, the watch won't receive any notification at all.
b) no haptic alert is given on the watch (purely display of notification and this only at next time when the user is activating the watch, not at the time when the notification is actually triggered)
This is correct. You're expecting didReceiveLocalNotification to fire on the watch, but this only occurs when the watch is activated:
If a local notification arrives while your app is active, WatchKit calls this method to deliver the notification payload. Use this method to respond to the notification.
This is the reason why your haptic feedback doesn't happen when you expect, as the call to play the haptic feedback doesn't occur until later.
You can conditionally play your haptic by first examining the local notification's fireDate. Only play your haptic feedback if the notification handling just triggered.
It appears that you're trying to handle playing a haptic independent of the default feedback which already occurs when watchOS displays the notification for you.
You may want to skip your extension from playing its own haptic feedback, as the user has already been notified by the system.
The real issue you're trying to work around is that only Apple's timer app can schedule its own local notifications.
If you haven't already, you may want to submit a feature request asking the Apple let third-party developers post local notifications on watchOS.
| 归档时间: |
|
| 查看次数: |
1724 次 |
| 最近记录: |