iOS 监听用户是否开启通知权限弹窗逻辑

前言
推送对App的重要性不言而喻,APP里的某些内容是需要用户开启推送权限才能给用户精准展示,才能及时通知用户。

下面我们通过一个demo实现对推送的监听,以及弹窗的效果,希望能帮助到大家。

效果图如下:
在这里插入图片描述
一、在AppDelegate里添加以下代码可以使APP弹窗通知权限框

#pragma mark ==========请求用户给权限==========
- (void)registerPushNotificationAuthorization:(UIApplication *)application{
    if (@available(iOS 10.0, *)) {
        //iOS 10 later
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        //必须写代理,不然无法监听通知的接收与点击事件
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (!error && granted) {
                //用户点击允许
                NSLog(@"注册成功");
            }else{
                //用户点击不允许
                NSLog(@"注册失败");
            }
        }];
        // 可以通过 getNotificationSettingsWithCompletionHandler 获取权限设置
        //之前注册推送服务,用户点击了同意还是不同意,以及用户之后又做了怎样的更改我们都无从得知,现在 apple 开放了这个 API,我们可以直接获取到用户的设定信息了。注意UNNotificationSettings是只读对象哦,不能直接修改!
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            NSLog(@"========%@",settings);
        }];
    }else if (@available(iOS 8.0, *)){
        //iOS 8 - iOS 10系统
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [application registerUserNotificationSettings:settings];
    }else{
        //iOS 8.0系统以下
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
    }
    //注册远端消息通知获取device token
    [application registerForRemoteNotifications];

}

二、判断用户是否同意了推送权限

#pragma mark ==========YES有权限  NO无权限==========
-(void)seeCurrentNotificationStatus{
    if (@available(iOS 10 , *)){
        WEAKSELF;
         [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
             STRONGSELF;
            if (settings.authorizationStatus == UNAuthorizationStatusDenied){
                // 没权限
                NSLog(@"无推送权限,弹窗处理");
                [strongSelf pushNotifcationView];
            }
        }];
    }else if (@available(iOS 8 , *)){
        UIUserNotificationSettings * setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (setting.types == UIUserNotificationTypeNone) {
            // 没权限
            NSLog(@"无推送权限,弹窗处理");
            [self pushNotifcationView];
        }
    }else{
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (type == UIUserNotificationTypeNone){
            // 没权限
            NSLog(@"无推送权限,弹窗处理");
            [self pushNotifcationView];
        }
    }
}

三、推送权限弹窗逻辑

#pragma mark ==========弹窗出推送权限==========
-(void)pushNotifcationView{
    //一天之内只提示一次
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //拿到当前时间
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //当前时间
    NSDate *nowDate = [NSDate date];
    NSString *nowDateStr= [dateFormatter stringFromDate:nowDate];
    //之前时间
    NSString *agoDateStr = [userDefault objectForKey:@"saveNowDate"];
    //判断时间差
    if ([agoDateStr isEqualToString:nowDateStr]) {
        //是当天时间
    }else{
        //弹窗
        //主线程使用
        dispatch_async(dispatch_get_main_queue(), ^{
            [[HBOpenPushNoticeView new] show];
        });
        //保存当前时间
        [userDefault setObject:nowDateStr forKey:@"saveNowDate"];
        [userDefault synchronize];
    }

}

附上Demo地址

END.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明似水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值