美文网首页iOS DeveloperiOS 好文自用收藏
iOS判断通知中心是否允许程序推送通知

iOS判断通知中心是否允许程序推送通知

作者: WenJim | 来源:发表于2016-05-13 18:36 被阅读5375次

    iOS 8.0后,获取推送通知类型改了,iOS 8.0的推送通知类型 是 UIUserNotificationType ,iOS 7.0 的推送通知类型 是UIRemoteNotificationType,所以需要先判断iPhone的系统版本,大于等于8.0以上的通过

    //获取通知中心 是否 允许程序通知消息的值,7.0的用 
     UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
    
    // 获取通知中心 是否 允许程序通知消息的值。
    UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    

    推送通知类型有:

    UIUserNotificationTypeNone    = 0,      // the application may not present any UI upon a notification being received
    
    UIUserNotificationTypeBadge  = 1 << 0, // the application may badge its icon upon a notification being received   中文解释就是 App右上角标推送通知消息的数量
    
    UIUserNotificationTypeSound  = 1 << 1, // the application may play a sound upon a notification being received    中文解释就是 App推送通知时的声音设置
    
    UIUserNotificationTypeAlert  = 1 << 2, // the application may display an alert upon a notification being received  中文解释就是 App推送通知时的警报设置
    

    如果通知中心不允许推送的话,types的值肯定为0,所以对应推送类型为UIUserNotificationTypeNone,贴出判断中心是否允许程序推送通知的代码

    
    if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0f) {
    
    UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
    
    if (UIUserNotificationTypeNone == setting.types) {
    NSLog(@"推送关闭 8.0");
    }
    else
    {
    NSLog(@"推送打开 8.0");
    }
    }
    else
    {
    UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    
    if(UIRemoteNotificationTypeNone == type){
    NSLog(@"推送关闭");
    }
    else
    {
    NSLog(@"推送打开");
    }
    }
    
    

    相关文章

      网友评论

      • PGOne爱吃饺子:楼主能判断出是用户禁止发送推送的判断么
      • 沙暴送葬:这样写不行啊,只是简单的NSLog而已,如果你把NSLog换成UIAlertview,在第一次提示用户打开权限时就已经弹出了你写的弹窗,这个问题怎么破
        JayAlvin:我们还要适配iOS7,我的通知都打开了,可是为什么用enabledRemoteNotificationTypes去获取的时候是0呢?
        沙暴送葬:@WenJim 也对,看来只能自己加判断,系统是没法帮我们判断了,不像相册啊麦克风那些权限一样
        WenJim:@沙暴送葬 你可以加一个判断是否第一次授权,第一次不弹出UIAlertView,授权以后都会弹出UIAlertview!
      • 5dd96619e78c:苹果什么时候能把这个 API 给禁了去,一些垃圾软件一天到晚的弹推送,关了打开软件每次都一个劲的提醒。
        王大吉Rock:你这有反人类的想法
        WenJim:@我在玩饭否 禁了你让社交app,怎么玩?你如果真不想要某个app的推送,你可以去设置-通知-对应的app不允许通知就行了

      本文标题:iOS判断通知中心是否允许程序推送通知

      本文链接:https://www.haomeiwen.com/subject/aglmrttx.html