简体   繁体   English

在 Apple Watch 上接收来自 iCloud 的推送通知

[英]Receiving push notifications from iCloud on Apple Watch

I have an app that supports both iOS and watchOS.我有一个同时支持 iOS 和 watchOS 的应用程序。 They operate on data stored in iCloud.它们对存储在 iCloud 中的数据进行操作。 What I am trying to do is receive notifications on the Apple Watch when data changes on the iCloud server so I can update complications.我想做的是在 iCloud 服务器上的数据发生变化时在 Apple Watch 上接收通知,以便我可以更新并发症。

I'm trying to set up a subscription using CKQuerySubscription in applicationDidFinishLaunching in the extension delegate.我正在尝试在扩展委托的applicationDidFinishLaunching中使用CKQuerySubscription设置订阅。 I'm also calling registerForRemoteNotifications on WKExtension .我还在WKExtension上调用registerForRemoteNotifications

  • The container and record type are correct, my predicate is TRUEPREDICATE容器和记录类型正确,我的谓词是TRUEPREDICATE
  • I'm operating on the private database我在私有数据库上操作
  • The subscription save process (ie database.save(subscription) ) succeeds订阅保存过程(即database.save(subscription) )成功
  • The remote notification registration process succeeds ( didRegisterForRemoteNotifications is called)远程通知注册过程成功( didRegisterForRemoteNotifications
  • I am running on an actual device and have breakpoints confirming everything sets up fine and nothing comes up as a result我在一个实际的设备上运行,并有断点确认一切设置正常,结果没有任何结果
  • The watch extension has remote notification background mode and push notification capabilities set up手表扩展设置了远程通知后台模式和推送通知功能
  • The data comes up fine through regular queries when I launch the actual app on the watch当我在手表上启动实际应用程序时,数据通过常规查询很好

The problem is:问题是:

  • didReceiveRemoteNotification is never called从未调用didReceiveRemoteNotification

Is it actually possible to get push notifications from iCloud or should I just abandon this idea?是否真的可以从 iCloud 获得推送通知,还是我应该放弃这个想法?

The problem might be the following:问题可能如下:
A silent push notification is indeed handled by watchOS, but only very rarely and pushes may be dropped. watchOS 确实可以处理静默推送通知,但很少发生推送,并且推送可能会被丢弃。 The docs say: 文档说:

The system treats background notifications as low priority: you can use them to refresh your app's content, but the system doesn't guarantee their delivery.系统将后台通知视为低优先级:您可以使用它们来刷新应用程序的内容,但系统不保证它们的交付。 In addition, the system may throttle the delivery of background notifications if the total number becomes excessive.此外,如果总数变得过多,系统可能会限制后台通知的传递。 The number of background notifications allowed by the system depends on current conditions, but don't try to send more than two or three per hour.系统允许的后台通知数量取决于当前情况,但不要尝试每小时发送超过两三个。

When a device receives a background notification, the system may hold and delay the delivery of the notification, which can have the following side effects: When the system receives a new background notification, it discards the older notification and only holds the newest one.当设备收到后台通知时,系统可能会保留并延迟通知的传递,这可能会产生以下副作用: 当系统收到新的后台通知时,它会丢弃旧的通知,只保留最新的通知。

So iCloud pushes are not really suited for updating a complication.所以 iCloud 推送并不适合更新复杂功能。

If one does not use iCloud but a separate server, one can use PushKit to update complications.如果不使用 iCloud 而是使用单独的服务器,则可以使用PushKit来更新并发症。 I find it strange that Apple did not implement this (yet?) for iCloud.我觉得奇怪的是,Apple 没有为 iCloud 实现这个(还没有?)。
See also my question & answer here .另请参阅我的问题和答案。

I have exactly the same scenario as @whistler.我的情况与@whistler 完全相同。 Everything works and is in order for received remote notifications on watchOS, except for: didReceiveRemoteNotification一切正常,为了在 watchOS 上接收远程通知,除了: didReceiveRemoteNotification

But I found out that by requesting authorisation for user notifications helped deliver them more often, still not delivering all of them but at least more frequently (and still only on the foreground it seems).但我发现,通过请求用户通知的授权有助于更频繁地传递它们,但仍然没有传递所有通知,但至少更频繁地传递(而且似乎仍然只是在前台)。

To request authorisation for user notifications simply:简单地请求用户通知的授权:

try? await UNUserNotificationCenter.current().requestAuthorization(options: [.alert])

This doesn't seem to prompt the user with an alert on watchOS.这似乎不会提示用户在 watchOS 上发出警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM