2014-12-04 78 views
9

我在我的项目中使用Parse SDK进行推送通知。我已经添加在didFinishLaunchingWithOptions:代码上parse.com给出RegisterUserNotificationSettings在ios 6.1中无效

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

任何人都可以告诉我怎么解决它?

回答

29

使用此代码在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)]; 
    } 
+0

精美,但任何缺点作品? – okysabeni 2015-03-28 23:46:38

+2

@Yko,我在很多项目中都使用过这个功能,但没有遇到任何问题。 – 2015-03-30 05:24:00

1

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