(iOS8中用户开启的推送通知类型对应的是UIUserNotificationType(下边代码中UIUserNotificationSettings的types属性的类型),iOS7对应的是UIRemoteNotificationType)app
此处以iOS8的UIUserNotificationType为例,(以下图)当本地通知或push/远程通知 推送时,这个常量代表了app将经过哪些方式提醒用户(好比:Badge,Sound,Alert的组合)spa
那么如何得到呢,在iOS8中是经过types属性,[[UIApplication sharedApplication] currentUserNotificationSettings].typescode
如上图,得到以后,咱们要知道的是这个property储存了全部你指定的推送类型(Badge,Sound,Alert),而在图一中咱们知道了推送类型对应的bitmask:(以四位二进制为例)blog
UIUserNotificationTypeNone = 0, == 0000 0ci
UIUserNotificationTypeBadge = 1 << 0, == 0001 1左移0位 2^0 = 1it
UIUserNotificationTypeSound = 1 << 1, == 0010 1左移1位 2^1 = 2 io
UIUserNotificationTypeAlert = 1 << 2, == 0100 1左移2位 2^2 = 4class
(之前老师教c语言的时候说过,还能够把左移当作乘2,右移除2)二进制
假如用户勾选推送时显示badge和提示sound,那么types的值就是3(1+2) == 0001 | 0010 = 0011 == 2^0 + 2 ^1 = 3 float
因此,若是用户没有容许推送,types的值一定为0(不然用户开启了推送的任何一种类型,进行|按位或操做后,都一定大于0)
1 /** 2 * check if user allow local notification of system setting 3 * 4 * @return YES-allowed,otherwise,NO. 5 */ 6 + (BOOL)isAllowedNotification { 7 //iOS8 check if user allow notification 8 if ([UIDevice isSystemVersioniOS8]) {// system is iOS8 9 UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings]; 10 if (UIUserNotificationTypeNone != setting.types) { 11 return YES; 12 } 13 } else {//iOS7 14 UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 15 if(UIRemoteNotificationTypeNone != type) 16 return YES; 17 } 18 19 return NO; 20 }
下面这个方法我是添加在UIDecive的Category中的,用于判断当前系统版本是大于iOS8仍是小于iOS8的
1 /** 2 * check if the system version is iOS8 3 * 4 * @return YES-is iOS8,otherwise,below iOS8 5 */ 6 + (BOOL)isSystemVersioniOS8 { 7 //check systemVerson of device 8 UIDevice *device = [UIDevice currentDevice]; 9 float sysVersion = [device.systemVersion floatValue]; 10 11 if (sysVersion >= 8.0f) { 12 return YES; 13 } 14 return NO; 15 }