3

I have added date triggered local notifications to my app and now I want the user to be able to get the notification they set on one device on all of their devices that have the app. I save the notification they set in a core data entity (saving the notifcation identifier and date it's suppose to be triggered). The app uses a NSPersistentCloudKitContainer to sync the users devices together.

Since I save the notifications the user sets to this iCloud container, is there a way I can listen to iCloud changes in the app's background so when a notification is added in iCloud, I can read it and set it as a local notification on their other devices?

You can think of this as a user setting a reminder in the Apple reminders app and how that notification pops up on all of my iCloud devices at the time I set it at. I prefer to not make any type server to handle this because I am not familiar with using servers.

Thank you in advance for your help!

ap123
  • 916
  • 1
  • 8
  • 22

1 Answers1

0

Yes, it is possible for NSPersistentCloudKitContainer to sync while your app is the background, and you can react to those changes by listening to any of these notifications: NSManagedObjectContextObjectsDidChange, NSPersistentCloudKitContainer.eventChangedNotification, and NSPersistentStoreRemoteChange (if enabled). There are some important caveats:

  • iOS may schedule an import to occur with a utility priority, which may or may not execute eventually.
  • This does not occur when your app is not running as the remote change notifications are non-launching, so for example, if you restart the device sync will not occur until you open the app again. Perhaps you could schedule background tasks to periodically attempt sync to try to keep up-to-date - I have not tried this.

This does seem to be working more reliably in iOS 16 vs 15 in my testing. I've documented my findings in the dev forums.

Jordan H
  • 52,571
  • 37
  • 201
  • 351