iOS本地推送相关知识

这里主要介绍 本地通知 的添加以及移除方法

同时还介绍了查询是否开启通知权限的方法

(1).iOS10 以后 本地推送 使用 UNUserNotificationCenter 来管理通知
(2).iOS7、8本地推送使用 UILocalNotification 来管理通知

但是iOS8 需要授权才能使用本地通知
下面看代码:
iOS10以前的代码

1. 添加 通知

 + (void)registerLocalNotificationInOldWay:(NSString *)alertTime {

    UILocalNotification *notification = [[UILocalNotification alloc] init];

    // 设置触发通知的时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"ss";
    NSDate *fireDate = [formatter dateFromString:alertTime];
    NSLog(@"fireDate=%@",fireDate);
    // 设置通知的时间,可以使固定的时间,也可以是从添加开始之后的多长时间
    //第一种
     //    notification.fireDate = fireDate;
     //第二种
   notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
    // 时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 设置重复的间隔(最小重复间隔是每分钟)
    notification.repeatInterval = NSCalendarUnitMinute;

    // 通知内容
    notification.alertBody =  @"今天不要忘记签到看书哦~";
    notification.applicationIconBadgeNumber = 1;// 需要在App icon上显示的未读通知数(设置为1时,多个通知未读,系统会自动加1,如果不需要显示未读数,这里可以设置0)
    // 通知被触发时播放的声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 通知参数(添加通知的参数,以便之后找到这个通知)
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"localNotification" forKey:@"localNotification"];
    notification.userInfo = userDict;

    // ios8后,需要添加这个注册,才能得到授权
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                 categories:nil];
        UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (notificationSettings.types == UIUserNotificationTypeNone) {
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        }

        // 通知重复提示的单位,可以是天、周、月
        notification.repeatInterval = NSCalendarUnitMinute;
    } else {
        // 通知重复提示的单位,可以是天、周、月
        notification.repeatInterval = NSCalendarUnitMinute;
    }

    // 执行通知注册
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

iOS10 添加本地通知

// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

//需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"提示" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"今天不要忘记签到看书哦~"
                                                     arguments:nil];
content.sound = [UNNotificationSound defaultSound];
// 在 alertTime 后推送本地推送
// 同样的这里也可以添加固定时间 和 添加之后的多长时间
// 1.固定时间 每个小时的10分10秒发送通知(当然先添加小时,几月几日都可以,每周的第几天也可以)
NSDateComponents *components = [[NSDateComponents alloc] init];
components.minute = 10;
components.second = 10;
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
// 2,添加后的多长时间发送通知
// UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];

// localNotification 通知的标识(同样的查询通知的时候使用)
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"localNotification"
                                                                      content:content trigger:trigger];

//添加推送成功后的处理!
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

}];

2.移除通知

// 判断是 iOS 10以后的
 if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0)) {
        UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
        [center removePendingNotificationRequestsWithIdentifiers:[NSArray arrayWithObjects:@"localNotification", nil]];
    }else{
        // 设置要移除的通知id
        NSString *notificationId = @"localNotification";
        NSArray *notifiArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
        for (UILocalNotification *local in notifiArray) {
            //将来可以根据UserInfo的值,来查看这个是否是你想要删除的通知
            if ([[local.userInfo objectForKey:@"localNotification"] isEqualToString:notificationId]) {
                //删除单个通知
                [[UIApplication sharedApplication]cancelLocalNotification:local];
            }
        }

3.查询是否开启通知权限

// 由于 iOS10 是在block中会掉的 所以这里需要一个block 下面有block的声明方法
+ (void)isOpenMessageNotificationServiceWithBlock:(CheckBlock)checkBlock
{
 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
        [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
            if (checkBlock) {
                returnBlock(settings.authorizationStatus == UNAuthorizationStatusAuthorized);
            }
        }];
    }else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
        checkBlock([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]);
    }else{
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (checkBlock) {
            checkBlock(type != UIRemoteNotificationTypeNone);
        }
    }
}

4.声明block

typedef void(^CheckBlock)(BOOL isOpen);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值