1

I'm writing a CloudKit-based iOS and Mac application that uses a CKSubscription to get notified when an update occurs in the remote data set. I've got the subscription setup correctly and the notifications are being received. Everything works great! The only issue is that the device receives a user-facing notification.

User Facing Notification for CloudKit CKSubscription

I would prefer that the remote update notification be an application internal implementation detail; I don't want the user receiving a notification every time they update their own collection of objects. I can't seem to find anything to address this in the documentation. Apple's own docs here talk about this like "duh, of course you want to do a notification." Well, I don't.

Aaron Vegh
  • 5,217
  • 7
  • 48
  • 75

1 Answers1

4

If you leave the alertBody of the CKNotificationInfo blank, then you won't get a user facing notification. The notification will be received in your app where you can handle it as usual.

    var subscription = CKSubscription(recordType: recordType, predicate: predicate, options: .FiresOnRecordCreation | .FiresOnRecordUpdate | .FiresOnRecordDeletion)
    subscription.notificationInfo = CKNotificationInfo()
    subscription.notificationInfo.shouldSendContentAvailable = true
    subscription.notificationInfo.soundName = UILocalNotificationDefaultSoundName
    subscription.notificationInfo.alertBody = ""
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • I extended the code in the sample a little. Are you using it like that? – Edwin Vermeer Nov 29 '14 at 15:56
  • Yes I am, but here's some new info: I'm saving this subscription every time my app runs. Since the first save was successful (and presumably with a different alertBody setting), subsequent saves have failed with "Duplicate subscription". I can change the UUID and make this work, but is there a way to only save it once? – Aaron Vegh Nov 30 '14 at 02:23
  • Eddie, I see you're something of a frequent answerer on SO with CloudKit issues. Thank you so much! As it happens, I'm only using private databases as well. I found your advice elsewhere about checking for subs, but it seems the issue here is, how to replace a sub? I think I have a clue... I'll check back in. – Aaron Vegh Nov 30 '14 at 02:27
  • What I do during development of the app is to remove all existing subscriptions on startup and then recreate the subscriptions that you need. For production you would need a mechanism so that you can update your subscription if it's necessary. You could remove the subscriptions by enumerating them and then deleting it. See http://stackoverflow.com/questions/27203023/cloudkit-fetch-all-subscriptions-from-the-current-user and then do a database.deleteSubscriptionWithID in the loop – Edwin Vermeer Nov 30 '14 at 10:27
  • Yes indeed. Looks like this is the way to go: iterating through all the subs and deleting them, then resetting them works. And look: no user facing alerts anymore! thanks again! – Aaron Vegh Nov 30 '14 at 15:59
  • @EdwinVermeer does `UILocalNotificationDefaultSoundName` works for you? I can not hear anything when alert comes up. Anyway set `alertBody = " "` I think better then empty string×Comments may only be edited for 5 minutes×Comments may only be edited for 5 minutes×Comments may only be edited for 5 minutes http://stackoverflow.com/questions/32033589/cknotificationinfo-soundname-does-not-work – János Aug 16 '15 at 10:14