【发布时间】:2015-09-30 22:19:11
【问题描述】:
我正在使用 CloudKit 存储用户数据,并希望在记录更改或创建新记录时获得推送通知。但它不起作用......
我像这样注册订阅:
- (void) updateCloudSubscriptions {
NSPredicate *allPredicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
CKSubscription *newOrUpdateSubscription = [[CKSubscription alloc]
initWithRecordType:kMyRecordType predicate:allPredicate options:
(CKSubscriptionOptionsFiresOnRecordCreation | CKSubscriptionOptionsFiresOnRecordUpdate)];
CKNotificationInfo *newOrUpdateNotificationInfo = [CKNotificationInfo new];
newOrUpdateNotificationInfo.shouldBadge = NO;
newOrUpdateNotificationInfo.shouldSendContentAvailable = YES;
newOrUpdateSubscription.notificationInfo = newOrUpdateNotificationInfo;
CKDatabase *publicDatabase = [[CKContainer containerWithIdentifier:kMyContainerID]
publicCloudDatabase];
[publicDatabase saveSubscription:newOrUpdateSubscription
completionHandler:^(CKSubscription *theSubscription, NSError *saveError) {
if (saveError){
//error handling
}
NSLog(@"Subscription created");
}];
}
这成功了。在 CloudKit 仪表板上,订阅已正确创建。
在我的 AppDelegate 中,我现在拥有以下内容:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeNone | UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:notificationSettings];
[application registerForRemoteNotifications];
}
并实现了这些委托方法:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"%@ with token = %@", NSStringFromSelector(_cmd), deviceToken);
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"%@ with error = %@", NSStringFromSelector(_cmd), error);
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
didRegisterForRemoteNotificationsWithDeviceToken使用令牌成功调用。但是didReceiveRemoteNotification 永远不会被调用。
我通过更改我的应用程序的 Mac 版本和 iPhone 版本的值对此进行了测试。两者都上传更改,但都不会触发通知。我也尝试直接在仪表板上更改值,但这也没有引起通知。
我在这里错过了什么?
如果相关:我使用的是带有 XCode 6.4 的 OS X 10.10
我激活了apsd 日志记录,但只收到这样的消息:
Jul 12 18:25:29 Mac.local apsd[44748]: APSMessageStore - Saving database.
Jul 12 18:25:29 Mac.local apsd[44748]: APSMessageStore - Destroying database.
Jul 12 18:25:29 Mac.local apsd[44748]: APSMessageStore - Closed database.
Jul 12 18:25:29 Mac.local apsd[44748]: APSMessageStore - Reopening database
Jul 12 18:25:29 Mac.local apsd[44748]: APSMessageStore - Initializing database on thread: <NSThread: 0x7f8f1bf80dd0>{number = 55, name = (null)}
Jul 12 18:25:29 Mac.local apsd[44748]: APSMessageStore - Enabling auto vacuum.
Jul 12 18:25:29 Mac.local apsd[44748]: APSMessageStore - Enabling WAL journal mode.
Jul 12 18:25:29 Mac.local apsd[44748]: APSMessageStore - Enabling Foreign Key support.
【问题讨论】:
-
当前存在更新通知的 CloudKit 错误。您是否尝试过添加新记录的应用程序?有关更新错误的更多信息,请参阅forums.developer.apple.com/thread/7288
-
@EdwinVermeer 这很有趣。但是,我也没有收到有关创建记录的通知。
-
在目标应用程序功能中,您是否为远程通知开启了后台模式?
-
发送通知时你的应用在后台吗?
-
该应用程序一直在前台,但我添加了后台功能,但没有成功。
标签: ios objective-c macos apple-push-notifications cloudkit