iOS开发本地推送

最近有个小需求,做一个本地的事件通知,要求选择起始时间和推送的时间间隔,并且要每天推送。借着这个机会把本地推送方面的知识有梳理一遍。

先来看一下大致的功能需求如下图:


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

每天都推送,那么也就是从周日到周六的七天,因此也可利用NSCalendarUnitWeekday实现,周日为本周的第一天,NSCalendarUnitWeekday中weekday这里周日到周六是用1-7

传送门:参考链接

你可能感兴趣的:(iOS开发本地推送)