iOS技术点:判断用户是否开启推送开关/是否允许推送?

- ( BOOL)isAllowedNotification {
   
//iOS8 check if user allow notification
   
if (IS_IOS_8) {// system is iOS8
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
       
if (UIUserNotificationTypeNone != setting.types) {
           
return YES;
        }
    }
else {//iOS7
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
       
if(UIRemoteNotificationTypeNone != type)
           
return YES;
    }
   
   
return NO;
}

(iOS8中 用户开启的推送通知类型 对应的是 UIUserNotificationType (下边代码中 UIUserNotificationSettings 的types属性的类型) ,iOS7对应的是 UIRemoteNotificationType)

此处以iOS8的UIUserNotificationType为例,(如下图)当本地通知或push/远程通知 推送时,这个常量指明了app如何去提醒用户(比如:Badge,Sound,Alert的组合) 

那么如何获得呢,在iOS8中是通过types属性,[[UIApplication sharedApplication] currentUserNotificationSettings].types

如上图,获得之后,我们要知道的是这个property储存了所有你指定的推送类型(Badge,Sound,Alert),而在图一中我们知道了推送类型对应的bitmask:(以四位二进制为例)

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

(以前老师教c语言的时候说过,还可以把左移当做乘2,右移除2)

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


所以,如果用户没有允许推送,types的值必定为0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值