iOS推送权限开发判断

作者: 随缘吖 | 来源:发表于2018-01-19 15:56 被阅读120次

    推送权限的申请

    • 加入头文件
    #ifdef NSFoundationVersionNumber_iOS_9_x_Max
    #import <UserNotifications/UserNotifications.h>
    #endif
    

    申请权限

    -(void) requestNotification
    {
        if (@available(iOS 10, *))
        {
            UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
            center.delegate = self;
            [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
                
                if (granted) {
                    // 允许推送
                }else{
                    //不允许
                }
                
            }];
        }
        else if(@available(iOS 8 , *))
        {
            UIApplication * application = [UIApplication sharedApplication];
            
            [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
            [application registerForRemoteNotifications];
        }
        else
        {
            UIApplication * application = [UIApplication sharedApplication];
            [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
            [application registerForRemoteNotifications];
        }
    }
    

    查看当前的权限

    -(void) checkCurrentNotificationStatus
    {
        if (@available(iOS 10 , *))
        {
             [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                
                if (settings.authorizationStatus == UNAuthorizationStatusDenied)
                {
                    // 没权限
                }
                
            }];
        }
        else if (@available(iOS 8 , *))
        {
            UIUserNotificationSettings * setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
            
            if (setting.types == UIUserNotificationTypeNone) {
                // 没权限
            }
        }
        else
        {
            UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
            if (type == UIUserNotificationTypeNone)
            {
                // 没权限
            }
        }
    }
    

    没权限进入设置页面

    #pragma mark 没权限的弹窗
    -(void) showAlrtToSetting
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"您还没有允许****权限" message:@"去设置一下吧" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction * setAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url];
                }
            });
            
        }];
        
        [alert addAction:cancelAction];
        [alert addAction:setAction];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    

    相关文章

      网友评论

      • 其实你懂De:楼主你好 刚好做到这 问您一个问题 为什么我点击允许granted还是为0 我用的是极光推送 和这个判断权限没有关系吧?
        其实你懂De:@careyang 我也纳闷了 点击允许和不允许都是0 点击允许但是第二次就好使了 granted就为1了 不知道哪里写错了 我现在的处理办法就是第一次点击允许和不允许都没有做处理 第二次进入app如果没有允许的情况下 就提醒他去设置 我不知道我哪里出错了 这行代码是写在appDelegate里没错吧
        随缘吖:和极光没有关系的,不过你使用极光的话,也是需要APP里面申请推送权限
        你在block 加个断点再试下,我自己打印,是YES的

      本文标题:iOS推送权限开发判断

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