How to notify another device that a CloudKit record has been updated?

1k Views Asked by At

The CKSubscription documentation says: When a record modification causes a subscription to fire, the server sends push notifications to all devices with that subscription except for the one that made the original change to the record.

Let's assume I have two devices: device 1 an iPad and device 2 an iPhone both logged in with the same iCloud accounts. Let's assume both devices subscribe for record updates for a certain record type.

My code looks like this (I've taken out some of the housekeeping stuff)

NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"fromUserRecordIDName == %@", _member.userRecordIDName];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"fromUserRecordIDName == %@", notificationPublic = [[CKNotificationInfo alloc] init];
notificationPublic.shouldBadge =YES;
notificationPublic.desiredKeys = @[@"fromMemberNameText"];
notificationPublic.shouldSendContentAvailable = YES;


if ([[NSUserDefaults standardUserDefaults] objectForKey:@"TickleRecordUpdateSubscription"] == nil) {

    [cloudManager subscribe:@"TickleRecord" public: YES options:CKSubscriptionOptionsFiresOnRecordUpdate predicate:predicate2 notification:notificationPublic completionHandler:^(CKSubscription *subscription, NSError *operationError) {

      if (operationError) {

       NSLog(@"An error occured in %@: %@", NSStringFromSelector(_cmd), operationError);



      } else{
        [[NSUserDefaults standardUserDefaults] setObject:subscription.subscriptionID forKey:@"TickleRecordUpdateSubscription"];
        [[NSUserDefaults standardUserDefaults] setObject:@"TickleRecordUpdateSubscription"forKey:subscription.subscriptionID ];

      }
    }];

It seems to me that when one device updates a record, the other device should be notified. But that is not what is happening - there is no notification. In the WWDC 2014 Advanced iCloud video they talk about being notified when one device marks a notification as read, but I haven’t had any success with that either.

Has anyone been successful in notifying one device when another device signed into the same account updates a record it?

2

There are 2 best solutions below

0
On

No, you have to use 2 different accounts. I would like to think this is a CloudKit bug instead of a missing feature. Now there is no way you could have both devices with the most up to dat data.

3
On

I had the same problem. I got it working with this subscription:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recipient == %@", [[CKReference alloc] initWithRecordID:self.userRecordId action:CKReferenceActionNone]];
CKSubscription *subscription = [[CKSubscription alloc] initWithRecordType:@"Message" predicate:predicate options:CKSubscriptionOptionsFiresOnRecordCreation];
subscription.notificationInfo = [CKNotificationInfo new];
subscription.notificationInfo.shouldSendContentAvailable = YES;
subscription.notificationInfo.soundName = UILocalNotificationDefaultSoundName;
subscription.notificationInfo.shouldBadge = YES;
CKDatabase *publicDatabase = [[CKContainer defaultContainer] publicCloudDatabase];
[publicDatabase saveSubscription:subscription completionHandler:^(CKSubscription *subscription, NSError *error) {...}];

With that I'm able to create a record from one device and get a notification on another signed into the same iCloud account.

Edit: I just realized you're asking about record updates, not creation, but maybe my answer can still help. One thing I found peculiar when I had this problem was I started receiving notifications after I set a value for the soundName property of CKNotificationInfo.