registerForRemoteNotificationTypes不能正常工作iOS7 xcode 6

时间:2014-10-07 22:55:56

标签: ios objective-c ios7 apple-push-notifications xcode6

升级我们的应用程序以使用iOS 8后出现问题,其中registerForRemoteNotificationTypes似乎无法在运行iOS 7的手机上运行,​​因为didRegisterForRemoteNotificationsWithDeviceToken未被调用且“允许推送通知”对话框未显示该应用程序。这是我正在使用的代码

// Add registration for remote notifications
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    NSLog(@"iOS 8 Registering for remote notification");
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
    NSLog(@"Registering for remote notification");
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

此代码适用于运行iOS 8的手机,而不适用于iOS 7。 另外一点信息我们设法通过连接手机的xcode运行应用程序后,推送通知才能在iOS 7手机上运行。然后,我们将与广告Hoc部署相同的代码部署到运行iOS 7的另一部iPhone和iPad,但两者均无效。 我有什么明显的东西在这里。

4 个答案:

答案 0 :(得分:1)

试试这段代码,可能适合你。

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary   *)launchOptions
{
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:
 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
return YES;
}

答案 1 :(得分:0)

我注意到XCode 6似乎生成了自己的标有XC的配置文件,这些配置文件也出现在开发者帐户的配置文件中。导出ad Hoc版本的应用程序时,它使用其中一个配置文件,似乎无法选择您在构建设置中创建和设置的配置文件。它似乎使用了关闭推送通知的通配符配置文件。我设法再次使用一个配置文件导出,该配置文件似乎是基于我创建的一个按下开启,它开始工作。这可以解释为什么我设法让它工作,如果我从Xcode直接将应用程序加载到手机。

答案 2 :(得分:0)

我遇到了同样的问题。 在我的情况下,它变成了一个简单的问题。您应该首先检查您的通知设置。 转到设置/通知中心,检查您在代码中设置的通知类型是否已打开。 希望能帮到你!

答案 3 :(得分:0)

是的,代码非常合适,但要在您的设备上获取默认通知警报,请首先重置模拟器的内容并重新安装应用程序。我试过这个并在模拟器上运行良好。