后台推送适用于iOS7,但不适用于iOS8

后台推送适用于iOS7,但不适用于iOS8,ios,ios8,push-notification,apple-push-notifications,Ios,Ios8,Push Notification,Apple Push Notifications,我有一个应用程序正在使用后台推送,当应用程序在iOS 7设备上运行时,一切正常,但如果应用程序在后台运行,如果应用程序在iOS 8设备上运行,推送不会传递到应用程序 我的注册码是: - (void) registerForRemoteNotification { // Check for the presense of iOS8 notification API if ([[UIApplication sharedApplication] respondsToSelector:@

我有一个应用程序正在使用后台推送,当应用程序在iOS 7设备上运行时,一切正常,但如果应用程序在后台运行,如果应用程序在iOS 8设备上运行,推送不会传递到应用程序

我的注册码是:

- (void) registerForRemoteNotification
{
    // Check for the presense of iOS8 notification API
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)])
    {
        NSLog(@"iOS8 detected");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        // iOS version < 8
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
}
这就是正在发生的事情:

1) On iOS7 when app is in the foreground -> push is delivered to the app as expected
2) On iOS7 when app is in the background -> push is delivered to the app as expected
3) On iOS8 when app is in the foreground -> push is delivered to the app as expected
4) On iOS8 when app is in the background -> push is NOT delivered to the app
5) On iOS8 when app is in the foreground and running via Xcode -> push is delivered to the app as expected
6) On iOS8 when app is in the background and running via Xcode -> push is delivered to the app as expected
(在每种情况下,都会调用didRegisterForRemoteNotificationsWithDeviceToken:调用,并且didFailToRegisterForRemoteNotificationsWithError:不调用。)

我不知道这是否相关,但看看4)和6)之间的区别,我看到了应用程序是否通过Xcode运行的区别

我的问题是,为什么在案例4中推送未被发送到应用程序? 它适用于iOS7,在iOS8上运行时注册远程通知的代码是正确的(事实上,如果通过Xcode执行应用程序,它确实可以工作,因此它必须是正确的),因此,当应用程序独立于iOS8上的Xcode运行时,为什么它不工作

**更新。发布了悬赏后,我浏览了苹果开发者论坛。似乎很多人都有类似的问题。对于某些情况,必须启用wifi才能获得后台推送,但对我来说,这不起作用-我必须为手机充电才能获得推送。如果不充电(即使电池已满),则无法交付。苹果公司表示,他们在iOS 8背景推送方面所做的工作是故意的,不是一个bug,但我的结论是,他们要么搞砸了,要么做了一些不合逻辑的事情,被认为是搞砸了。所以我希望他们能意识到他们所做的一切是一团糟的,并在未来的版本中加以改变


如果你想把背景带到相关的开发人员论坛,阅读一些相关的文章。

我认为你需要设置推送通知权限键

aps环境 ,在iOS中接收推送通知


请参考此链接

我在iOS8中找到了在后台接收远程通知所需的内容。苹果公司已经改变了为此需要调用的方法。首先,他们不推荐使用方法
registerforremotentifications
,现在应该使用
registerforremotentifications
。但这并不是改变的全部——您仍然必须指定允许的通知类型。 以下是文档()中的内容:

如果希望应用程序的推送通知显示警报、播放声音或执行其他面向用户的操作,则必须调用registerUserNotificationSettings:方法来请求要使用的通知类型。如果您不调用该方法,系统将以静默方式向您的应用程序发送所有推送通知。由于注册过程考虑了用户的首选通知设置,因此请求访问面向用户的通知类型也不能保证它们将被授予权限。要了解可用的通知设置,请使用currentUserNotificationSettings方法

现在尝试调用
currentUserNotificationSettings
,您将看到不允许任何类型的通知,因此不接收通知也就不足为奇了

试试这个:

[application registerForRemoteNotifications];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:mySettings];

它做到了

以下内容已在我的iOS7和iOS8应用程序中运行:

if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // iOS 8 Notifications
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];
}
else
{
    // iOS < 8 Notifications
    [application registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
if([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
//iOS 8通知
[应用程序注册表通知设置:[UIUserNotificationSettings设置类型:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)类别:无];
[申请登记处证明];
}
其他的
{
//iOS<8通知
[应用程序注册信息类型:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

希望这有帮助

试试这个,我发现苹果的文档有问题,我使用了Parse,它有一个更好的帮助部分

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    [Parse setApplicationId:@"asdasd" clientKey:@"asdasd"];
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                             categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
} else {
    // Register for Push Notifications before iOS 8
    [Parse setApplicationId:@"asdasd" clientKey:@"asdasd"];
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                     UIRemoteNotificationTypeAlert |
                                                     UIRemoteNotificationTypeSound)];
}

如果这就是问题所在,那么该应用程序将不会收到任何推送。我的问题不是应用程序通常不接收推送,而是它不接收后台推送。只需检查一下,设置中是否启用了后台通知。后台模式启用了“后台获取”和“远程通知”,如果这是你的意思的话。我有一个非常类似的问题。您的应用程序是否显示在设置-->通知中?是的,显示在设置-->通知中,您的应用程序如何?我的应用程序不显示。此外,它在前台接收通知,但在后台不接收。您是否尝试过尝试推送消息的内容?最初,当后台推送第一次发布时,我发现在后台交付它的唯一方法是在推送中添加“声音”部分。最近我看到了这个帖子。因此,苹果似乎对推送的内容有一个极其可笑和愚蠢的要求,即所谓的无声推送。它不再适用于我,但可能会适用于你,声音名称可以是空的。你的代码对于iOS8来说有点不合适,它应该是第二个变体,并实现以下方法:-(void)application:(UIApplication*)application didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings{[application RegisterForRemotionTifications];}……我不相信它能解决问题,但值得一试。不幸的是,这对我不起作用,我的代码已经包含了你发布的代码。苹果的文档中说:“如果你不调用该方法,系统将以静默方式向你的应用程序发送所有推送通知。”但你似乎是通过做与文档相反的事情来实现的!谢谢,这是我已经做过的。我发现苹果在iOS8中故意做了一些后台推送,在某些情况下没有推送,但他们没有公布细节。
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // iOS 8 Notifications
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];
}
else
{
    // iOS < 8 Notifications
    [application registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    [Parse setApplicationId:@"asdasd" clientKey:@"asdasd"];
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                             categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
} else {
    // Register for Push Notifications before iOS 8
    [Parse setApplicationId:@"asdasd" clientKey:@"asdasd"];
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                     UIRemoteNotificationTypeAlert |
                                                     UIRemoteNotificationTypeSound)];
}