Updating watch complications from the server when using WidgetKit

I have a time tracking app that has sync between devices. I am using background push notifications and PKPushRegistry to react to updates from other devices - to perform a sync with the server from the watch app and afterwards to reload the complications.

My question is: after I move to using WidgetKit for my complications, can I still reload my complications from the background, within the pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith:..) method? Or is it subject to strict limits for updating from the background such as on iOS?

If the above is not possible, what is the recommended way of keeping WidgetKit-based complications on the user's watch face up to date? Considering that I also need to do a sync with the server from the watch app before it has the latest data to be able to display the correct state of the complication.

Answered by Systems Engineer in 891622022

I would suggest that you look at registering for APNS pushes directly from your widget extension. This will behave as if your app had called reloadTimelines(). The API for this is described here: https://developer.apple.com/documentation/WidgetKit/Updating-widgets-with-widgetkit-push-notifications

after I move to using WidgetKit for my complications, can I still reload my complications from the background

Yes this can be done. Updates will be subject to the same budgetary conditions as pushes registered directly by the extension. However, relying on pushes to the app alone may be problematic if the user never launches the app so it can get a push token

A combination approach could work best here if you want both your complication and your app to be able to update from pushes.

If you're concerned about sending two pushes for every update, your server could use the widget push token, and then if it receives an app push token for the same device, just choose to use the app one instead

I would suggest that you look at registering for APNS pushes directly from your widget extension. This will behave as if your app had called reloadTimelines(). The API for this is described here: https://developer.apple.com/documentation/WidgetKit/Updating-widgets-with-widgetkit-push-notifications

after I move to using WidgetKit for my complications, can I still reload my complications from the background

Yes this can be done. Updates will be subject to the same budgetary conditions as pushes registered directly by the extension. However, relying on pushes to the app alone may be problematic if the user never launches the app so it can get a push token

A combination approach could work best here if you want both your complication and your app to be able to update from pushes.

If you're concerned about sending two pushes for every update, your server could use the widget push token, and then if it receives an app push token for the same device, just choose to use the app one instead

Thank you for your reply. I was looking into registering for APNS pushes directly from the widget extension last year, but the problem is that my push doesn't contain enough data by itself to do the refresh.

From what I understood from the API, when a push is received in the widget extension, it just calls the reload timeline method on the provider with no metadata attached to it. That is not enough for me to determine if the update should be treated differently. I need the main app target to do the sync first, so that the widget extension can get updated afterwards.

But nevertheless, thank you for your tips. I will look more into it to see if I could somehow make it work with combining these approaches that you suggested.

Please give it a try to see what works for your use case. I'm not sure if the timing of the push notification processing will be predictable with two push notifications if one is carrying the data and the other is reloading the widget timeline. You can also try calling WidgetCenter.reloadTimelines(ofKind:) from pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith:..).

If this doesn't work, please do file Feedback with the description of your use case.

Updating watch complications from the server when using WidgetKit
 
 
Q