iOS - 判断用户是否允许推送通知(iOS7/iOS8)
2015-05-03 23:18
399 查看
(iOS8中用户开启的推送通知类型对应的是UIUserNotificationType(下边代码中UIUserNotificationSettings的types属性的类型),iOS7对应的是UIRemoteNotificationType)
此处以iOS8的[b]UIUserNotificationType为例,(如下图)当本地通知或push/远程通知 推送时,这个常量表明了app将通过哪些方式提醒用户(比如:Badge,Sound,Alert的组合)[/b]
那么如何获得呢,在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)
下面这个方法我是添加在UIDecive的Category中的,用于判断当前系统版本是大于iOS8还是小于iOS8的
此处以iOS8的[b]UIUserNotificationType为例,(如下图)当本地通知或push/远程通知 推送时,这个常量表明了app将通过哪些方式提醒用户(比如:Badge,Sound,Alert的组合)[/b]
那么如何获得呢,在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)
/** * check if user allow local notification of system setting * * @return YES-allowed,otherwise,NO. */ + (BOOL)isAllowedNotification { //iOS8 check if user allow notification if ([UIDevice isSystemVersioniOS8]) {// 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; }
下面这个方法我是添加在UIDecive的Category中的,用于判断当前系统版本是大于iOS8还是小于iOS8的
/** * check if the system version is iOS8 * * @return YES-is iOS8,otherwise,below iOS8 */ + (BOOL)isSystemVersioniOS8 { //check systemVerson of device UIDevice *device = [UIDevice currentDevice]; float sysVersion = [device.systemVersion floatValue]; if (sysVersion >= 8.0f) { return YES; } return NO; }
相关文章推荐
- iOS - 判断用户是否允许推送通知
- iOS技术点:判断用户是否开启推送开关/是否允许推送?
- iOS技术点:判断用户是否开启推送开关/是否允许推送?
- iOS怎么判断用户是否打开了系统设置里面推送通知的开关?
- iOS 判断用户是否允许接收远程通知
- iOS技术点:判断用户是否开启推送开关/是否允许推送?
- iOS 判断用户是否允许了通知
- iOS 判断是否允许app消息推送
- ios判断用户是否打开通知
- iOS开发——判断用户是否开启了应用推送设置
- iOS开发之判断用户是否打开APP通知开关
- iOS判断用户是否打开APP通知开关
- 判断用户是否允许通知消息
- iOS开发之判断用户是否打开APP通知开关,看我就够了。
- iOS开发之判断用户是否打开APP通知开关
- iOS开发 判断用户是否开启了热点
- 从BeejiveIM 的iPhone的Push(推送通知),用户如何完全授权它登录,是否涉及安全问题?
- iOS中判断消息推送是否打开
- 判断iOS用户手机是否安装百度、高德地图App
- JPush极光推送问题,用户是否打开允许推送信息的开关,但不管设置没设置都会弹出警告说没有设置