Jam*_*ley 3 apple-push-notifications ios swift watchos watchos-3
我正在构建iOS + watchOS应用,这些应用需要在iOS和watch应用状态的所有可能组合中通知用户更新的信息:当iOS和watchOS都在前台运行时,当它们都在后台运行时,当一个或另一个在后台时。
我已经成功处理了这四种情况中的三种的推送通知。卡住的地方是手表应用在前台打开而iOS应用在后台-我找不到手表通知控制器,其ExtensionDelegate或在以下情况下会触发的iOS AppDelegate的任何方法:在这种情况下,将推送远程通知。
在iOS AppDelegate中,当iOS应用程序处于前台状态且观看应用程序处于任一状态时,在推送通知时将触发:
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
Run Code Online (Sandbox Code Playgroud)
在WatchKit Extension NotificationController中,当iOS和watch应用都在后台时推送通知时将触发此事件:
override func didReceive(_ notification: UNNotification, withCompletion completionHandler: @escaping (WKUserNotificationInterfaceType) -> Swift.Void) { ... }
Run Code Online (Sandbox Code Playgroud)
但是,如果在推送通知时手表应用程序位于前台,而iOS应用程序位于后台,则通知横幅会出现在iPhone上(即使屏幕锁定),但我发现没有办法处理和更新除非当时手表也在后台。
有任何想法吗?
想通了。要在监视应用程序处于后台和前台时处理通知,似乎您必须同时在NotificationController的didReceive方法(用于在后台通知)中实现方法和ExtensionDelegate的UNUserNotificationDelegate协议(通过willPresent方法来实现)(前台通知):
func userNotificationCenter(_: UNUserNotificationCenter, willPresent: UNNotification, withCompletionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("willPresent fired from watch delegate")
}
Run Code Online (Sandbox Code Playgroud)
让我感到困惑的是文档说:
用户通知框架提供了可在iOS,watchOS和tvOS应用中使用的统一API,并支持与本地和远程通知相关的大多数任务。这是您可以在此框架下执行的一些任务示例...
但是,尽管他们针对iOS,tvOS和macOS进行了此操作,但文档并没有确切说明手表的操作方法。
在Watch ExtensionDelegate中不需要执行注册和设备令牌步骤(并且实际上是不可能的,因为您需要UIApplication模块来执行此步骤),但是必须调用:
UNUserNotificationCenter.current().delegate = self
Run Code Online (Sandbox Code Playgroud)
启动ExtensionDelegate时。
| 归档时间: |
|
| 查看次数: |
1122 次 |
| 最近记录: |