我有一个应用程序正在使用后台推送,当应用程序在运行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上可行,我认为在iOS8上运行时注册远程通知的代码是正确的(并且确实,如果通过Xcode执行应用程序,则它可以工作),那么为什么在独立于iOS 8的Xcode上运行应用程序时它不起作用? **更新。在发布此赏金后,我浏览了苹果开发者论坛。似乎有很多人遇到类似的问题。对于一些人来说,必须启用wifi才能获得背景推送,但对我来说,那行不通-我必须充电手机才能收到它们。如果不充电(即使电池已满),则它们将无法传递。苹果表示,他们在iOS 8中关于后台推送所做的事情是故意的而不是bug,但我的结论是他们要么搞砸了,要么做了某些非常不合逻辑的事情,以至于被认为是一个搞砸。因此,我希望他们会意识到他们所做的是一堆烂摊子,并在将来的版本中进行更改。 如果您想获得背景,请前往相关开发论坛并阅读一些相关主题。