RegisterUserNotificationSettings 在 ios 6.1 中不起作用

Posted

技术标签:

【中文标题】RegisterUserNotificationSettings 在 ios 6.1 中不起作用【英文标题】:RegisterUserNotificationSettings is not working in ios 6.1 【发布时间】:2014-12-04 12:36:09 【问题描述】:

我在我的项目中使用 Parse SDK 进行推送通知。我已经在 parse.com 上添加了 didFinishLaunchingWithOptions: 中的代码

UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                UIUserNotificationTypeBadge |
                                                UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                         categories:nil];


[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];

如果设备或模拟器版本是 ios 8,它工作正常,但在 iOS 6.1 中不工作,并出现消息

[UIApplication registerUserNotificationSettings:]: unrecognized selector sent to instance 0x208406c0

谁能告诉我怎么解决?

【问题讨论】:

【参考方案1】:

在 didFinishLaunchingWithOptions 方法中使用此代码,它适用于 ios 6 和 7

[application registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];

如果您想在所有情况下都在 ios 6、7、8 中工作,则在 didFinishLaunchingWithOptions 中使用此代码

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

【讨论】:

效果很好,但有什么缺点吗? @Yko,我在很多项目中都使用过它,但没有遇到任何问题。【参考方案2】:

对于 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)];

【讨论】:

以上是关于RegisterUserNotificationSettings 在 ios 6.1 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章