John
John

Reputation: 8548

didReceiveRemoteNotification not called when CloudKit record is added

What I want:

I want my iOS app to perform an action once an CloudKit record is added.

What I did:

I followed Apple's info to do this.

Problem:

application:didReceiveRemoteNotification is never called.

What I tried to solve the problem:

I've successfully set up a subscription to get notified when an CloudKit record is added. I confirmed this with application:didRegisterForRemoteNotificationsWithDeviceToken.

I've also confirmed that the record is indeed created.

I can fetch the record using CKQueryOperation.

I've also tried it with application:didReceiveRemoteNotification:fetchCompletionHandler, but this method is also not being called.

I've searched the Apple Developer Forums as well as StackOverflow but did not find a solution to my proble.

What can I do?

Code to create the subscription:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];

    CKSubscription *subscription = [[CKSubscription alloc] initWithRecordType:@"command" predicate:predicate options:CKSubscriptionOptionsFiresOnRecordCreation];


    CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
    notificationInfo.alertLocalizationKey = @"New command.";
    notificationInfo.shouldBadge = YES;

    subscription.notificationInfo = notificationInfo;

    [VMKGlobalVariables.GLOBAL_appDelegate.privateDatabase saveSubscription:subscription
                   completionHandler:^(CKSubscription *subscription, NSError *error) {
                       if (error)
                       {
                           // insert error handling
                       }
                       else
                       {
                           DDLogVerbose(@"Added command subscription successfully.");
                       }
                   }

     ];

Upvotes: 1

Views: 964

Answers (2)

Steve Ham
Steve Ham

Reputation: 3154

Need to add shouldSendContentAvailable.

let info = CKSubscription.NotificationInfo()
info.shouldSendContentAvailable = true
subscription.notificationInfo = info

Upvotes: 0

user3069232
user3069232

Reputation: 8995

Did you register you app delegate with the line.

application.registerForRemoteNotifications()

Did you have some code look like this in your app delegate?

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    let notification = CKQueryNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])

    let container = CKContainer(identifier: "iCloud.blah.com")
    let publicDB = container.publicCloudDatabase

    if notification.notificationType == .Query {
        let queryNotification = notification as! CKQueryNotification
        if queryNotification.queryNotificationReason  == .RecordUpdated {
            print("queryNotification.recordID \(queryNotification.recordID)")


        }
    }
}

Upvotes: 1

Related Questions