微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios8 – CloudKit推送通知,应用程序在后台运行

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

如果它在前台运行,它会收到推送通知.

知道发生了什么事吗?

在CloudKit开发模式下运行,订阅用于添加,编辑和删除,并使用以下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?如果您这样做,那么您将从操作系统获得通知,而您无需设置本地通知.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐