我正在使用以下CKNotification Info,这似乎工作正常:
CKNotificationInfo *note = [[CKNotificationInfo alloc] init];
note.alertBody = @"Something Happened";
note.shouldBadge = NO;
note.shouldSendContentAvailable = NO;
当iOS设备上的内容发生变化时,我的Mac应用会根据此通知的订阅收到推送通知。但是,didReceiveRemoteNotification
永远不会被调用,因此我无法处理该事件。我需要能够刷新并获取新的更改。我该怎么做?
答案 0 :(得分:5)
致电registerForRemoteNotificationTypes:
并实施didRegisterForRemoteNotificationsWithDeviceToken:
应该是足够的代码,应用程序ID应包括推送通知服务。
我在跨平台(iOS / OS X)应用中使用CloudKit来同步设备之间的收藏夹,如下所示:
// OS X specific code
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[NSApp registerForRemoteNotificationTypes:NSRemoteNotificationTypeNone];// silent push notification!
}
- (void)application:(NSApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[self.favCon handleCloudKitNotificationWithUserInfo:userInfo];
}
请注意NSRemoteNotificationTypeNone
的使用,这意味着无声推送通知!这就是我在FavController类中设置CloudKit的方法:
- (void)getOrCreateFavZoneWithCompletionHandler:(successCompletionHandler)handler {
// check if FavZone exists op
__block int createZone = 0;
CKFetchRecordZonesOperation *fetchRecZonesOp = [[CKFetchRecordZonesOperation alloc] initWithRecordZoneIDs:@[[FavController favRecordZoneID]]];
CKModifyRecordZonesOperation *saveRecZoneOp = [[CKModifyRecordZonesOperation alloc] initWithRecordZonesToSave:nil recordZoneIDsToDelete:nil];
fetchRecZonesOp.fetchRecordZonesCompletionBlock = ^(NSDictionary *recordZonesByZoneID, NSError *operationError) {
if (recordZonesByZoneID.count == 0) {// zone doesn't exist
createZone = 1;
CKRecordZone *favZone = [[CKRecordZone alloc] initWithZoneName:UTXAFavZoneName];
saveRecZoneOp.recordZonesToSave = @[favZone];
NSLog(@"Creating new Zone %@", favZone.zoneID.zoneName);
} else {
NSLog(@"Zone %@ already exists.", [FavController favRecordZoneID].zoneName);
}
};
// create FavZone op
saveRecZoneOp.modifyRecordZonesCompletionBlock = ^(NSArray *savedRecordZones, NSArray *deletedRecordZoneIDs, NSError *operationError) {
[self successCompletionHandler:(savedRecordZones.count == createZone) error:operationError informDelegate:YES handler:handler];
};
[saveRecZoneOp addDependency:fetchRecZonesOp];
[[FavController favDatabase] addOperation:fetchRecZonesOp];
[[FavController favDatabase] addOperation:saveRecZoneOp];
}
- (void)subscribeToFavChanges:(successCompletionHandler)handler {
// get current subscription
[[FavController favDatabase] fetchSubscriptionWithID:UTXAFavConCKSubscriptionID completionHandler:^(CKSubscription *subscription, NSError *error) {
if (subscription) {
NSLog(@"using existing subscription: %@", subscription);
[self successCompletionHandler:YES error:nil informDelegate:NO handler:handler];
} else {
CKSubscription *sub = [[CKSubscription alloc] initWithZoneID:[FavController favRecordZoneID]
subscriptionID:UTXAFavConCKSubscriptionID
options:0];// "You must specify 0 for this parameter. Zone subscriptions currently do not support any options."
[[FavController favDatabase] saveSubscription:sub completionHandler:^(CKSubscription *subscription, NSError *error) {
NSLog(@"created new subscription: %@ %@", subscription, error);
[self successCompletionHandler:(error == nil) error:error informDelegate:YES handler:handler];
}];
}
}];
}
只要我在一台设备上添加或删除记录,我就会收到所有其他设备的通知,我在FavController类中处理这样的通知:
/// @abstract Handle push notifications sent by iCloud.
/// @discussion App delegates call this method when they receive a push notification through didReceiveRemoteNotification.
/// Currently, only airport favorites produce a PN, it is of type CKNotificationTypeRecordZone.
/// @param userInfo The userInfo dict tied to each push notification.
- (void)handleCloudKitNotificationWithUserInfo:(NSDictionary *)userInfo {
[self recursivelyCheckForPreviousCloudKitNotifications];
}
- (void)recursivelyCheckForPreviousCloudKitNotifications {
CKFetchNotificationChangesOperation *fetchOp = [[CKFetchNotificationChangesOperation alloc] initWithPreviousServerChangeToken:_defCon.notificationChangeToken];
__weak CKFetchNotificationChangesOperation *weakOp = fetchOp;
fetchOp.notificationChangedBlock = ^(CKNotification *notification) {
[self handleNotification:notification];
};
fetchOp.fetchNotificationChangesCompletionBlock = ^( CKServerChangeToken *serverChangeToken, NSError *operationError) {
NSLog(@"new notification change token: %@", serverChangeToken);
_defCon.notificationChangeToken = serverChangeToken;
if (weakOp.moreComing) {
NSLog(@"more coming!!");
[self recursivelyCheckForPreviousCloudKitNotifications];
} else {
NSLog(@"done handling notification changes.");
}
};
[[FavController favContainer] addOperation:fetchOp];
}
- (void)handleNotification:(CKNotification *)notification {// withCompletionHandler:(successCompletionHandler)handler {
if (notification.notificationType == CKNotificationTypeRecordZone) {// make sure we handle only zone changes
CKRecordZoneNotification *noti = (CKRecordZoneNotification *)notification;
if ([noti.recordZoneID.zoneName isEqualToString:[FavController favRecordZoneID].zoneName]) {
// received an update for the fav zone
[self queuedFavUpdateFromCloud];
} else {
// received an update for an unknown zone
NSLog(@"WARNING: received an update for an unknown zone: %@", noti.recordZoneID.zoneName);
}
} else {
NSLog(@"WARNING: received unknown notification: %@", notification);
}
}
答案 1 :(得分:0)
好的,我终于明白了。如果您使用CKNotificationInfo作为警报,则除非您将acc
设置为空字符串,否则不会在Mac上调用didReceiveRemoteNotification
!这看起来只是OS X中的一个错误(到目前为止10.10和10.11),但可以通过这个简单的更改来解决。