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];
}

相关文章

  • iOS推送权限开发判断

    推送权限的申请 加入头文件 申请权限 查看当前的权限 没权限进入设置页面

  • iOS 判断是否打开定位与推送通知服务

    iOS 项目里有些业务需求用户打开定位和推送服务权限,这个demo就是判断用户是否打开这俩定位与推送,上demo:...

  • ios 推送权限判断逻辑处理

    // 检查手机是否开启推送权限功能 -(void) checkCurrentNotificationStatus...

  • iOS开发判断权限问题

    总结了下,自己项目里面使用到权限问题,如果有问题希望大家多交流指正 关于定位权限 关于使用相机权限 关于推送权限 ...

  • iOS权限完整解决

    前言 iOS开发中,权限问题不可避免; 写了文章iOS开发中的这些权限,你搞懂了吗?和[续]iOS开发中的这些权限...

  • 权限库升级了~

    关于权限相关的文章已经发了不少:iOS开发中的这些权限,你搞懂了吗?、如何获取iOS应用网络权限?、iOS开发中权...

  • 相册相机访问

    判断相册访问权限 iOS 8 之前 iOS 8之后 两者结合即是 判断相机访问权限 iOS7之前都可以访问相机,i...

  • iOS判断推送通知是否开启

    iOS判断推送通知是否开启

  • 20170313 iOS 权限相关 : 判断与 跳转 设置

    iOS 常见错误(持续更新) iOS权限获取 摄像头与麦克风 iOS各种权限判断(相机,相册,定位,录音) ios...

  • iOS系统权限获取

    iOS系统权限: 相册、相机、麦克风、推送通知、AppleMusic 相册权限状态:ALAuthorization...

网友评论

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

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

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