• iOS本地推送


    通过iOS本地推送实现定时推送

    • 先在appdelegate的didFinishLaunch方法中请求推送许可
      if (@available(iOS 10.0, *)) {
      UNUserNotificationCenter
      *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { }]; center.delegate = self; } else { // Fallback on earlier versions [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:[NSSet set]] ]; }
    •   收到本地通知的处理
    //iOS10之前收到本地通知
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ NSLog(@"receive local notifi%@",notification.userInfo); }
    //iOS10以后,这个代理代表app在前台的时候
     - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler NS_AVAILABLE_IOS(10){ 

      NSLog(@"willPresentNotification local notifi%@",notification.request.content.userInfo);
      //代表交给didReceiveNotificationResponse处理
      completionHandler(UNNotificationPresentationOptionSound); 
    }
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler NS_AVAILABLE_IOS(10){ NSLog(@"userNotificationCenter local notifi%@",response.notification.request.content.userInfo); if ([response.actionIdentifier isEqualToString:@"sure"]) { NSLog(@"notif sure"); }else if ([response.actionIdentifier isEqualToString:@"cancel"]){ NSLog(@"notif cancel"); } completionHandler(); }

     设置本地推送

    - (void)addAlarmIndentifier:(NSString*)requestIndetifier isRepeat:(BOOL)repeat{
        NSDateComponents*cmp = [[NSCalendar currentCalendar] components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitWeekday fromDate:self.datepicker.date];
        NSDateComponents *components = [[NSDateComponents alloc] init];
        components.hour = cmp.hour;
        components.minute = cmp.minute  ;
        components.weekday = cmp.weekday;
        //通知声音
        NSString *soundName =  @"soundName";
        if (@available(iOS 10.0, *)) {
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
            content.title = @"wake up!!";
            content.body = @"time to massage" ;
            //类别,用于自定义通知动作
            content.categoryIdentifier = @"alarm";
            content.userInfo =@{@"name":@"jack"};
         
            //通知声音
            content.sound = [UNNotificationSound soundNamed:soundName];
            //触发器repeats:代表是否重复
            UNCalendarNotificationTrigger *trig = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:(repeat?YES:NO)];
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIndetifier content:content trigger:trig];
            //动作1
            UNNotificationAction*action1 = [UNNotificationAction actionWithIdentifier:@"sure" title:@"sure" options:UNNotificationActionOptionForeground];
            //动作2
            UNNotificationAction*action2 = [UNNotificationAction actionWithIdentifier:@"cacel" title:@"cancel" options:UNNotificationActionOptionAuthenticationRequired];
            //identifier:alarm
            UNNotificationCategory*category =    [UNNotificationCategory categoryWithIdentifier:@"alarm" actions:@[action1,action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
            //设置分类
            [center setNotificationCategories: [NSSet setWithObject:category]];
            //设置通知
            [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                if (error) {
                    NSLog(@"error===%@",error.localizedDescription);
                }
            }];
            
            
        } else {//ios 10 以下
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
            UILocalNotification *notification       = [[UILocalNotification alloc] init];
            //如果需要循环可以以过去的时间为标准,然后设置repeatInterval
            notification.fireDate  = self.datepicker.date;
            
            notification.repeatCalendar             = [NSCalendar currentCalendar];
            notification.repeatInterval             = repeat?NSCalendarUnitWeekOfYear:0;
            //        //    NSMinuteCalendarUnit
            notification.timeZone                   = [NSTimeZone systemTimeZone];
            notification.alertBody                  = @"wake up!!!";
            notification.soundName                  = soundName;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
        
    }
    

    取消通知(全部取消)

    - (void)cancel{
     
        if (@available(iOS 10.0, *)) {
            [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
        } else {
              [[UIApplication sharedApplication] cancelAllLocalNotifications];
        }
    }
    
    • 效果如下图:

  • 相关阅读:
    2022/3/31周四学业有成项目小结
    2022/4/1周五学业有成项目小结
    2022/3/30周三学业有成项目小结
    2022/4/8学业有成项目大总结
    Centos 常用命令处理
    Springboot 各个版本特点
    thymeleaf取list下标
    如何修改文件Root of Spring boot2嵌入式tomcat
    有些道理,应该懂得~~~
    install mysql 后续
  • 原文地址:https://www.cnblogs.com/cnman/p/9256372.html
Copyright © 2020-2023  润新知