前言
推送对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.