18

I'm writing a small app that uses CloudKit. For some reason the app doesn't receive any notifications when there's a new record matching the query. Has anyone been able to get this feature to work?

I create new records in the app, but also in the CloudKit dashboard. The record is very simple, with a single integer field.

Create a record:

 CKRecord *record = [[CKRecord alloc] initWithRecordType:kSISCloudKitRecordTypeTest];
 record[@"value"] = @1;
 [self.publicDatabase saveRecord:record completionHandler:^(CKRecord *record, NSError *error)
  {
       // this call succeeds, no error.
  }];

Register for notifications:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application registerForRemoteNotifications];
}

Create a subscription:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"value = 1"];

CKSubscription *subscription = [[CKSubscription alloc]
                                initWithRecordType:kSISCloudKitRecordTypeTest
                                predicate:predicate
                                options:CKSubscriptionOptionsFiresOnRecordCreation];

CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
notificationInfo.alertLocalizationKey = @"LOCAL_NOTIFICATION_KEY";
notificationInfo.soundName = @"Party.aiff";
notificationInfo.shouldBadge = YES;
subscription.notificationInfo = notificationInfo;

[self.publicDatabase saveSubscription:subscription
                    completionHandler:^(CKSubscription *subscription, NSError *error)
 {
     // this succeeds as well, at least the 1st time I run it.
     // on subsequent calls it returns an error "duplicate subscription", which is OK by me.
 }

After running the above code, and creating a new record in the dashboard, I expect this app delegate method to be called:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    CKNotification *cloudKitNotification = [CKNotification notificationFromRemoteNotificationDictionary:userInfo];
    NSLog(@"cloudKitNotification: %@", cloudKitNotification);
}

However, it never gets called.

János
  • 32,867
  • 38
  • 193
  • 353
TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76

1 Answers1

6

I am now receiving the notifications since Beta 3:

{
    aps =     {
    };
    ck =     {
        ce = 2;
        cid = "iCloud.com.domain.App";
        nid = "0b3ae470-d2c0-4f35-a817-12a899ee5964";
        qry =         {
            dbs = 2;
            fo = 1;
            rid = 88aee11ca88d4ecc45bf57c898b360c8e7e3d8bb;
            zid = "_defaultZone";
            zoid = "_defaultOwner";
        };
    };
}

Also, there’s a shouldSendContentAvailable property on CKNotificationInfo that makes it possible to receive the notifications in background – which also appears to work now (as of Beta 4).

zoul
  • 102,279
  • 44
  • 260
  • 354
  • Seems apple changed the notification registration api. Am I supposed to receive push notifications even my app is in background with one single line `+ [UIApplication registerForRemoteNotifications];` or I need something more? – Ben Lu Jul 15 '14 at 05:34
  • The notification API is getting a bit hairy, but I think that’s it, along with the `remote-notification` key in `Info.plist` file. – zoul Jul 15 '14 at 06:29
  • 1
    I am also using Beta 3 and subscriptions are not working for me i.e. I am not getting any remote notifications. Zoul, is this still working for you? can you post the code that works for you? – harryhorn Jul 19 '14 at 10:53
  • @János, they are broken since beta 4. – Simon Jul 24 '14 at 07:52
  • 1
    I have still not been able to get this working in Beta 5. Anyone successful? – harryhorn Aug 15 '14 at 07:45
  • Has this been resolved? I still have this problem with Xcode Beta Version 6.3 (6D532l) – Timbo Mar 12 '15 at 11:41
  • hi @zoul do you know if there is another way to write the public database as an administrator, to seed and maintain data, without requiring an icloud login? full question: http://stackoverflow.com/questions/36808601/seed-and-maintain-cloudkit-public-database-still-requires-icloud-login – Crashalot Apr 23 '16 at 08:52
  • The way how notifications are handled changed for iOS 10. Pay attention for this. – Bartłomiej Semańczyk Nov 07 '16 at 23:10