iOS推送注册相关知识点

2015-09-24  本文已影响207人  某个胖子

兼容iOS7 iOS8的注册方式

- (void)registRemoteNotification
{
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0)
{
    //iOS8 以后注册方式
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
else
{
    //iOS8以前注册方式
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
}
}

判断用户是否注册推送

     + (BOOL)isResigterNotification {
         if (iOS8以上) {
                UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
                if (UIUserNotificationTypeNone != setting.types) {
                   return YES;
                 }
            } else {
                UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
                if(UIRemoteNotificationTypeNone != type){
                     return YES;
                 } 
           return NO;
        }

option 相关

UIUserNotificationTypeNone  = 0 ,          == 0000                                 0
UIUserNotificationTypeBadge = 1 << 0 ,     == 0001      1左移0位     2^0 = 1
UIUserNotificationTypeSound = 1 << 1 ,     == 0010      1左移1位     2^1 = 2 
UIUserNotificationTypeAlert = 1 << 2 ,     == 0100      1左移2位     2^2 = 4

假如用户勾选推送时显示badge和提示sound,那么types的值就是 == 0001 | 0010 = 0011 == 2^0 + 2 ^1 = 3

消息提示

上一篇 下一篇

猜你喜欢

热点阅读