简体   繁体   English

registerForRemoteNotificationTypes不能正常工作iOS7 xcode 6

[英]registerForRemoteNotificationTypes not working iOS7 xcode 6

I have an issue after upgrading our app to work with iOS 8, where registerForRemoteNotificationTypes does not appear to be working on phone running iOS 7, as in didRegisterForRemoteNotificationsWithDeviceToken does not get called and the "Allow Push Notifications" dialog is not appearing on the app. 升级我们的应用程序以使用iOS 8后出现问题,其中registerForRemoteNotificationTypes似乎无法在运行iOS 7的手机上运行,​​因为didRegisterForRemoteNotificationsWithDeviceToken没有被调用,并且“允许推送通知”对话框没有出现在应用程序上。 This is the code I am using 这是我正在使用的代码

// 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)];
}

This code works fine for phones running iOS 8, just not iOS 7. One other bit of information we managed to get push notifications to work on an iOS 7 phone only after running the app through xcode with the phone attached. 此代码适用于运行iOS 8的手机,而不适用于iOS 7.我们设法通过附带手机的xcode运行应用程序后,我们设法获得推送通知才能在iOS 7手机上运行。 We then deployed the same code as an ad Hoc deployment to another iPhone and iPad running iOS 7 and neither worked. 然后,我们将与广告Hoc部署相同的代码部署到运行iOS 7的另一部iPhone和iPad,但两者均无效。 Is there something obvious I am missing here. 有什么明显的东西我在这里失踪了。

Try this code, may work for you. 试试这个代码,可能适合你。

-(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;
}

I noticed that XCode 6 appears to generate it's own provisioning profiles labeled with XC these also appear in the provisioning profiles of the developer account. 我注意到XCode 6似乎生成了自己的标有XC的配置文件,这些配置文件也出现在开发者帐户的配置文件中。 When you export the app for ad Hoc release it uses one of these profiles and there appears to be no way to select the profile you created and set in the build setting. 导出ad Hoc版本的应用程序时,它使用其中一个配置文件,似乎无法选择您在构建设置中创建和设置的配置文件。 It appears it was using a wild card profile that had push notification turned off. 它似乎使用了关闭推送通知的通配符配置文件。 I managed to export again with a profile that seemed to be based on one I created with push turn on and it started working. 我设法再次使用一个配置文件导出,该配置文件似乎是基于我创建的一个按下开启,它开始工作。 This would explain why I managed to get it to work if I loaded the app directly to the phone from Xcode. 这可以解释为什么我设法让它工作,如果我从Xcode直接将应用程序加载到手机。

I got hit the same problem. 我遇到了同样的问题。 In my situation, it turns a simple problem. 在我的情况下,它变成了一个简单的问题。 You should examine your notification setting first. 您应该首先检查您的通知设置。 Going to Setting/Notification Center, and check whether the notification type you set in your code are opened. 转到设置/通知中心,检查您在代码中设置的通知类型是否已打开。 Hoping that can help you! 希望能帮到你!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM