我想要的是什么:
我希望我的iOS应用在添加CloudKit记录后执行操作。
我做了什么:
我跟着Apple的info来做这件事。
问题:
application:didReceiveRemoteNotification
永远不会被调用。
我试图解决问题:
我已成功设置订阅,以便在添加CloudKit记录时收到通知。我用application:didRegisterForRemoteNotificationsWithDeviceToken
确认了这一点。
我也确认记录确实已经创建。
我可以使用CKQueryOperation
获取记录。
我也尝试过application:didReceiveRemoteNotification:fetchCompletionHandler
,但这种方法也没有被调用。
我搜索了Apple开发者论坛以及StackOverflow,但没有找到解决问题的方法。
我该怎么办?
创建订阅的代码:
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.");
}
}
];
答案 0 :(得分:0)
您是否在线上注册了app delegate。
application.registerForRemoteNotifications()
您的应用代表中是否有类似的代码?
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)")
}
}
}
答案 1 :(得分:0)
需要添加shouldSendContentAvailable
。
let info = CKSubscription.NotificationInfo()
info.shouldSendContentAvailable = true
subscription.notificationInfo = info