iOS8和iOS7的PhoneGap推送通知令牌差异

7

我正在使用以下代码行获取推送通知的令牌,

我添加了下面的代码行以支持ios8,但是当这些代码行添加后,ipa在ios8上能够工作,但是在ios7上无法工作,在ios7上打开应用程序后立即关闭。

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{

#ifdef __IPHONE_8_0
    //Right, that is the point
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                         |UIRemoteNotificationTypeSound
                                                                                         |UIRemoteNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
    //register to receive notifications
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){
    }
    else if ([identifier isEqualToString:@"answerAction"]){
    }
}
#endif

当在iOS 7上运行应用程序时,您看到的异常是什么? - ebi
registerUserNotificationSettings:]: 在实例0x16d71c60中发送了无法识别的选择器 2014-10-31 12:56:00.899 ClickMobileCDV[1875:60b] *** 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'-[UIApplication registerUserNotificationSettings:]: 在实例0x16d71c60中发送了无法识别的选择器' *** 第一个抛出调用堆栈: - Ortal Blumenfeld Lagziel
2个回答

10

自iOS8以来,registerForRemoteNotificationTypes已被弃用。请改用registerUserNotificationSettings:registerForRemoteNotifications

测试registerUserNotificationSettings: API是否在运行时可用,这意味着您正在运行iOS8。

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }

7
解决方案:
#ifdef __IPHONE_8_0
    if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
#else
    //register to receive notifications
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif

1
#ifdef是一个预处理器指令。您能否将其打包成二进制文件并上传到应用商店,以便在iOS7和iOS8上都能正常运行? - mikebz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接