简体   繁体   中英

Receiving push notifications from iCloud on Apple Watch

I have an app that supports both iOS and watchOS. They operate on data stored in 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.

I'm trying to set up a subscription using CKQuerySubscription in applicationDidFinishLaunching in the extension delegate. I'm also calling registerForRemoteNotifications on WKExtension .

  • The container and record type are correct, my predicate is TRUEPREDICATE
  • I'm operating on the private database
  • The subscription save process (ie database.save(subscription) ) succeeds
  • The remote notification registration process succeeds ( didRegisterForRemoteNotifications is called)
  • 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

Is it actually possible to get push notifications from iCloud or should I just abandon this idea?

The problem might be the following:
A silent push notification is indeed handled by watchOS, but only very rarely and pushes may be dropped. 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.

If one does not use iCloud but a separate server, one can use PushKit to update complications. I find it strange that Apple did not implement this (yet?) for iCloud.
See also my question & answer here .

I have exactly the same scenario as @whistler. Everything works and is in order for received remote notifications on watchOS, except for: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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