6

我为创建和更新创建订阅。创作总是完美无缺,我会收到来自其他设备的推送通知。更新到 iCloud,您可以在那里看到它们,但推送通知不会发送到设备。

- (void)subscribeWithType:(NSString*)type completionHandler:(void(^)(CKSubscription *))completionHandler
{
    if (self.enabled == NO || _available == NO || _reachability.currentReachabilityStatus != ReachableViaWiFi)
    {
        if (completionHandler)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                completionHandler(nil);
            });
        }
        return;
    }

    NSPredicate *truePredicate = [NSPredicate predicateWithValue:YES];

    CKSubscriptionOptions options = CKSubscriptionOptionsFiresOnRecordCreation | CKSubscriptionOptionsFiresOnRecordUpdate;

    CKSubscription *subscription = [[CKSubscription alloc] initWithRecordType:type
                                                                    predicate:truePredicate
                                                               subscriptionID:[CTUtils GetUUID]
                                                                      options:options];

    CKNotificationInfo *notification = [[CKNotificationInfo alloc] init];
    notification.shouldSendContentAvailable = YES;

    notification.alertLocalizationKey = @"";
    notification.shouldBadge = NO;

    subscription.notificationInfo = notification;

    CKModifySubscriptionsOperation *operation = [[CKModifySubscriptionsOperation alloc] initWithSubscriptionsToSave:@[subscription] subscriptionIDsToDelete:nil];

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    __block CKSubscription *resultSubscription = nil;

    operation.modifySubscriptionsCompletionBlock = ^(NSArray *savedSubscriptions, NSArray *deletedSubscriptionIDs, NSError *operationError) {
        if (operationError)
        {
            if (iCloudLog)
            {
                NSLog(@"create %@ subscriptions error : %@", type, operationError);
            }
        }
        else
        {
            resultSubscription = savedSubscriptions.firstObject;

            if (iCloudLog)
            {
                NSLog(@"created subscription for %@: %@", type, savedSubscriptions);
            }
        }

        dispatch_semaphore_signal(semapthore);
    };

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.privateDatabase addOperation:operation];
    });

    dispatch_time_t time = DISPATCH_TIME_FOREVER;

    dispatch_semaphore_wait(semapthore, time);

    if (completionHandler)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            completionHandler(resultSubscription);
        });
    }
}

有人在这里看到错误或为什么不实施推送通知来更新?

4

0 回答 0