在将我们的应用程序升级为适用于 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 手机上运行。然后,我们将相同的代码作为临时部署部署到另一台运行 iOS 7 的 iPhone 和 iPad,但都没有成功。 有什么明显的我在这里遗漏的东西吗?
最佳答案
试试这个代码,可能对你有用。
-(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;
}
关于ios - registerForRemoteNotificationTypes 不工作 iOS7 xcode 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26246606/