CloudKit 推送通知,应用 运行 在后台

CloudKit Push Notification, App running on background

当iOS8.2-app在后台运行时,没有收到任何推送通知,

而如果它 运行 在前台,它会很好地接收推送通知。

知道发生了什么吗?

运行 在 CloudKit Development 模式下,订阅用于添加、编辑和删除,并使用以下 didReceiveRemoteNotification:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"Push received.");

    NSDictionary * apsDict = [userInfo objectForKey:@"aps"];

    NSString * alertText = [apsDict objectForKey:@"alert"];

    //TODO: get the record information from the notification and create the appropriate message string.

    if(application.applicationState == UIApplicationStateActive) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:alertText delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    } else {

        if([application currentUserNotificationSettings].types & UIUserNotificationTypeAlert) {
            UILocalNotification * localNotification = [[UILocalNotification alloc] init];
            localNotification.alertBody = NSLocalizedString(@"alert body", nil);;
            localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
            [application presentLocalNotificationNow:localNotification];
        }
    }

    completionHandler(UIBackgroundFetchResultNoData);
}

当您转到您的应用程序设置功能时,您是否为后台模式启用了远程通知?

看这个截图:

除此之外,您是否注册了这样的所有通知类型:

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Badge | .Sound, categories: nil))
    application.registerForRemoteNotifications()

在您的订阅中,您是否为 CKNotificationInfo 发送了 alertBody 或 alertLocalizationKey?如果你这样做,那么你已经从 OS 收到通知,你不需要设置本地通知。

更新:正如下面提到的Porton,这个问题通过填写alertBody解决了。

我遇到了同样的问题,并花了几个小时在应用程序不在前台时使推送通知正常工作。

最初,我没有设置 alertBody,因为我不想让用户在 CloudKit 数据更改时看到消息。

但是,如上所述,如果没有 alertBody,当应用程序不在前台时,方法 didReceiveRemoteNotification 不会被调用。

我终于尝试将 alertBody 设置为空字符串:

CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
notificationInfo.alertBody = @"";

现在可以使用了:didReceiveRemoteNotification 即使应用未处于活动状态并且没有向用户显示任何警告消息也会被调用。