Ios 从未调用CloudKit推送通知didReceiveRemoteNotification
我正在使用CloudKit存储用户数据,并希望在记录更改或创建新记录时获得推送通知。但它不起作用 我注册订阅,如下所示:Ios 从未调用CloudKit推送通知didReceiveRemoteNotification,ios,objective-c,macos,apple-push-notifications,cloudkit,Ios,Objective C,Macos,Apple Push Notifications,Cloudkit,我正在使用CloudKit存储用户数据,并希望在记录更改或创建新记录时获得推送通知。但它不起作用 我注册订阅,如下所示: - (void) updateCloudSubscriptions { NSPredicate *allPredicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"]; CKSubscription *newOrUpdateSubscription = [[CKSubscription alloc
- (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");
}];
}
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仪表板上,已正确创建订阅
在myAppDelegate中,我现在有以下内容:
- (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
。但从未调用过“DidReceiveMotonification”。
我通过在Mac版和iPhone版上更改值来测试这一点。两者都上载更改,但都不会触发通知。我还尝试直接在仪表板上更改值,但也没有引起通知
我错过了什么
如果相关:我使用的是OS X 10.10和XCode 6.4
我激活了apsd
日志记录,但只收到如下消息:
- (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");
}];
}
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.
Jul 12 18:25:29 Mac.local apsd[44748]:APSMessageStore-保存数据库。
7月12日18:25:29 Mac.local apsd[44748]:APSMessageStore-正在销毁数据库。
7月12日18:25:29 Mac.local apsd[44748]:APSMessageStore-关闭数据库。
7月12日18:25:29 Mac.local apsd[44748]:APSMessageStore-重新打开数据库
七月12日18:25:29 Mac.local apsd[44748]:APSMessageStore-正在初始化线程上的数据库:{number=55,name=(null)}
7月12日18:25:29 Mac.local apsd[44748]:APSMessageStore-启用自动真空。
7月12日18:25:29 Mac.local apsd[44748]:APSMessageStore-启用WAL日志模式。
7月12日18:25:29 Mac.local apsd[44748]:APSMessageStore-启用外键支持。
指出,如果要发送静默推送通知,需要在notificationInfo字典中将shouldSendContentAvailable
设置为true,如下所示:
let notificationInfo = CKNotificationInfo()
notificationInfo.shouldSendContentAvailable = true
subscription.notificationInfo = notificationInfo
对于iOS 8,您的通知注册是正确的,但在OS X上,您必须使用
registerforremotentificationtypes:
方法NSApplication
,而在iOS 7上,您必须使用registerforremotentificationtypes:
方法UIApplication
:
应用程序必须向Apple推送通知服务(APNs)注册才能
接收应用程序推送提供商发送的远程通知。在iOS 8中
之后,注册分为四个阶段:
Info.plist
,并选中“启用后台模式”和“远程通知”复选框:
因为您需要一个静默通知,根据文档,您应该只将“contentavailable”标志发送到1,所以这是正确的。但是也有其他用户在静默通知方面有问题,显然,将“sound”属性作为空字符串发送确实有帮助(),因此您可以尝试将
CKNotificationInfo.soundName
设置为空字符串。目前,有一个CloudKit错误与更新通知有关。您是否尝试了添加新记录的应用程序?有关更新错误的更多信息,请参阅@EdwinVermeer,这很有趣。但是,我也没有收到关于创建记录的通知。在目标应用程序功能中,是否打开远程通知的后台模式?发送通知时,您的应用程序是否在后台?应用程序始终在前台,但我添加了后台功能,但没有成功。感谢您的详细回答,巴里斯。我确实在OSX上正确注册了;我的问题中没有列出代码。我总是在iOS 8上测试,所以那里的注册应该是正确的。当我测试时,这些应用程序已经出现在前台,但我也添加了后台通知和didReceiveEmotentification:fetchCompletionHandler:
,但它仍然没有被调用。对我来说这是非常神秘的…@codingFriend1查看我的更新答案:尝试将CKNotificationInfo.soundName
设置为空字符串。也许它有帮助,它似乎已经为…是的!那是丢失的拼图<必须设置代码>声音名称,才能接收通知。很抱歉,我花了这么长时间来核实,而你只收到了一半的赏金。无论如何:非常感谢你的帮助!为此非常感谢!!当应用程序在backgro中时,我一直在努力寻找不被调用的方法