RegisterUserNotificationSettings is not working in

2019-02-12 18:40发布

I am using the Parse SDK for push notification in my project. I have added the code in didFinishLaunchingWithOptions: as given on the parse.com

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


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

its working fine, if device or simulator version is iOS 8, but its not working in iOS 6.1 ,and message appear

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

Can any one tell me how can i solve it?

2条回答
在下西门庆
2楼-- · 2019-02-12 19:19

use this code in didFinishLaunchingWithOptions method it is work in ios 6 and 7

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

if you want to work in ios 6,7,8 in all cases then used this code inside a 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)];
    }
查看更多
forever°为你锁心
3楼-- · 2019-02-12 19:31

For 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)];
}
查看更多
登录 后发表回答