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

时间:2014-12-30 18:16:14

标签: 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:@selector(registerForRemoteNotifications)])
    {
        NSLog(@"iOS8 detected");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        // iOS version < 8
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
}

我也试过这个变种:

- (void) registerForRemoteNotification
{
    // Check for the presense of iOS8 notification API
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)])
    {
        NSLog(@"iOS8 detected");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    }
    else
    {
        // iOS version < 8
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
}

这是推送有效载荷的内容:

aps =     {
    "content-available" = 1;
    somePayload = "The payload";
    sound = "";
};

这就是发生的事情:

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,AFAICT用于在iOS8上运行时注册远程通知的代码是正确的(事实上,如果通过Xcode执行应用程序它确实有效,因此它必须是正确的),因此当应用程序是在iOS8上独立于Xcode运行?

**更新。在为此发布赏金之后,我浏览了Apple开发者论坛。似乎很多人都有类似的问题。因为有些wifi必须启用才能获得后台推送,但对我来说却不起作用 - 我必须通过手机充电才能获得它们。如果它没有充电(即使电池已满),那么它们也无法送达。苹果公司表示他们在iOS 8背景推动方面所做的是故意而不是一个错误,但我的结论是,他们要么搞砸了,要么做了一些不合逻辑的事情,以至于被视为搞砸了。所以我希望他们能够意识到他们所做的事情,并在未来的版本中改变它。

如果你想让背景转到相关的开发论坛并阅读一些相关的主题。

4 个答案:

答案 0 :(得分:0)

我想出了在iOS8中接收背景远程通知所需的内容。 Apple已经改变了需要调用的方法。首先,他们已弃用方法registerForRemoteNotificationTypes,现在您应该使用registerForRemoteNotifications。但这并不是所有变化的东西 - 您仍然需要指定允许通知的类型。 这里来自文档(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instm/UIApplication/registerForRemoteNotifications):

  

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

现在尝试拨打currentUserNotificationSettings,您会发现不允许任何类型的通知,因此难怪他们没有被收到。

试试这个:

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

它做的事情!

答案 1 :(得分:0)

以下内容适用于我的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)];
}

希望这有帮助!

答案 2 :(得分:0)

试试这个,我发现我的Apples文档存在问题,我使用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)];
}

答案 3 :(得分:-1)

我认为您需要设置推送通知授权密钥。

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

请参阅此链接Push Notification Entitlement Keys