最近有个小需求,做一个本地的事件通知,要求选择起始时间和推送的时间间隔,并且要每天推送。借着这个机会把本地推送方面的知识有梳理一遍。
先来看一下大致的功能需求如下图:
1.png
主要实现原理
1. AppDelegate中设置
推送的样式,提醒用户使用推送原因(系统权限提醒)
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions types=UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:types completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
根据granted的状态也能判断开关是否打开,如果是只有推送功能的APP,只有打开推送才能使用的话,这里就可以写跳转设置的代码。(APP首次运行,系统弹出时候允许推送,不允许直接跳转设置)
2.创建推送前的判断
判断通知开关是否打开,没有打开弹窗提醒,是否需要跳转设置中,打开开关。(首次运行时,用户不允许推送的情况)
[[UNUserNotificationCenter currentNotificationCenter]getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if ( settings.authorizationStatus==UNAuthorizationStatusAuthorized) {
NSLog(@"通知开关打开");
}else{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *shetVC = [UIAlertController alertControllerWithTitle:@"Tip" message:@"Open notifications in settings" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"Cancle" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self.navigationController popViewControllerAnimated:YES];
}];
UIAlertAction *done = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@""} completionHandler:^(BOOL success) { }];
}];
[shetVC addAction:cancle];
[shetVC addAction:done];
[self presentViewController:shetVC animated:YES completion:nil];
});
}
}];
3.创建推送
- 用起始日期的方式实现每天的循环
-(void)addLocationNotifitionWithFrency:(NSString*)startTime endTime:(NSString*)endTime{
NSArray *startArr = [startTime componentsSeparatedByString:@":"];
NSInteger hour_start = [startArr[0]integerValue];
NSInteger minute_start = [startArr[1]integerValue];
NSInteger num = 0;
//时间间隔
if ([self.timeBaseView.interValLable.text isEqualToString:@"1 hours"]) {
num = 1;
}else if ([self.timeBaseView.interValLable.text isEqualToString:@"2 hours"]){
num = 2;
}else if([self.timeBaseView.interValLable.text isEqualToString:@"3 hours"]){
num = 3;
}else if ([self.timeBaseView.interValLable.text isEqualToString:@"4 hours"]){
num = 4;
}else{
num = 5;
}
//
NSInteger cha_hour = [NSString chaEndTimeAndStartTime:startTime endTime:endTime];
NSInteger cha = cha_hour/num;//计算出起始时间内有多少个num(间隔num小时,要推送几次),
//循环创建的时候最后一个时间(一定要区分ID,否则只会是一个推送)
for (NSInteger index=0; index<cha; index++) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"Wash Hand";
content.subtitle = @"It's time to wash hand";
content.sound = [UNNotificationSound defaultSound];
//设置最初时间,从哪一天开始,这里可以尽可能的往前设置,只要在你最初时间之前都可以
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [NSString stringWithFormat:@"2019-01-01 %ld:%ld:00",hour_start+index*num,minute_start];;
NSDate * date = [formatter dateFromString:dateString];
/*这是一个时间容器
NSCalendarUnitYear
NSCalendarUnitMonth
NSCalendarUnitDay
NSCalendarUnitHour
NSCalendarUnitMinute
NSCalendarUnitSecond
NSCalendarUnitWeekday)*/
//设置不同的容器可以实现不同的需求,这里我们是要每天推送的某时分秒推送
NSDateComponents * components = [[NSCalendar currentCalendar]
components:NSCalendarUnitHour |
NSCalendarUnitMinute |
NSCalendarUnitSecond
fromDate:date];
//两种trigger,这里UNCalendarNotificationTrigger符合我们要求
UNCalendarNotificationTrigger * trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components
repeats:YES];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[NSString stringWithFormat:@"time%ld",index] content:content trigger:trigger];
//把通知加到UNUserNotificationCenter, 到指定触发点会被触发
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"通知添加失败:%@",error);
} else {
NSLog(@"通知添加成功");
}
}];
}
}
每天都推送,那么也就是从周日到周六的七天,因此也可利用NSCalendarUnitWeekday实现,周日为本周的第一天,NSCalendarUnitWeekday中weekday这里周日到周六是用1-7
传送门:参考链接