iOS 本地推送开发记录二

重复推送参考了这篇文章,感谢作者:
UILocalNotification 本地推送 周一到周五重复提醒

本地推送记录相关的两篇文章:
iOS 本地推送开发记录一
iOS 本地推送开发记录二

在之前的工作中,基本没有使用过本地推送,不过对推送也有过了解,至于重复推送就没有研究过了,此次项目中有设置重复推送的需求,于是记录如下。

在iOS8上本地推送有一个属性repeatInterval,用来设置重复间隔。看了上述文章后,便了解了重复推送的原理。如果需要设置每个星期的某几天出现推送,就需要根据当前时间算出接下来一周内需要设置的那几天,都注册上推送,再通过以下设置完成每周重复:

localNotification.repeatInterval = NSWeekCalendarUnit;

在iOS10上初始化本地推送时使用

UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:notificationComps repeats:YES];

实现的原理也基本上同iOS8上一致,就是设置好最近一周的推送,设置repeats:YES即可。

代码如下:

iOS 8

    NSDate *now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps = [calendar components:unitFlags fromDate:now];

NSDate *fireDate = nil;
        if (time > 0) {
            // 定时时间存在,以定时时间为准
            fireDate = [NSDate dateWithTimeIntervalSinceNow:time];
        } else {
            fireDate = [NSDate dateWithTimeIntervalSince1970:dateTime];
        }
        
        for (NSInteger newWeekDay = 1;newWeekDay <= 7; newWeekDay++) {
            if (![supporDays containsObject:@(newWeekDay)]) {
                continue;
            }
            
            NSInteger temp = 0;
            NSInteger days = 0;
            temp = newWeekDay - comps.weekday;
            days = (temp >= 0 ? temp : temp + 7);
            
            NSDate *date = [fireDate dateByAddingTimeInterval:3600 * 24 * days];
            
            // 1.创建通知
            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            // 设置通知显示的标题
            if (@available(iOS 8.2, *)) {
                localNotification.alertTitle = title ? title : @"";
            }
            // 设置通知显示的内容
            localNotification.alertBody = body ? body : @"";
            // 设置通知的发送时间,单位秒
            localNotification.fireDate = date;
            // 每周重复
            localNotification.repeatInterval = NSWeekCalendarUnit;
            //收到通知时App icon的角标
            localNotification.applicationIconBadgeNumber = 1;
            //推送是带的声音提醒,设置默认的字段为UILocalNotificationDefaultSoundName
            localNotification.soundName = soundName;
            // 3.发送通知( : 根据项目需要使用)
            // 方式一: 根据通知的发送时间(fireDate)发送通知
            NSMutableDictionary *newParam = [NSMutableDictionary dictionaryWithDictionary:param];
            newParam[@"identifier"] = [NSString stringWithFormat:@"%@-%@", notificationId, @(newWeekDay)];
            localNotification.userInfo = newParam;
            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        }

iOS 10+

    NSDate *now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps = [calendar components:unitFlags fromDate:now];
    
    
    // 使用 UNUserNotificationCenter 来管理通知
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    
    //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = title;
    content.body  = body;
    UNNotificationSound *sound = [UNNotificationSound soundNamed:soundName];
    content.sound = sound;
    content.userInfo = param;
    
    NSTimeInterval timeInterval = 0;
    if (time > 0) {
        timeInterval = time;
    } else {
        timeInterval = [[NSDate dateWithTimeIntervalSince1970:dateTime] timeIntervalSince1970] - [[NSDate date] timeIntervalSince1970];
    }
    
    for (NSInteger newWeekDay = 1;newWeekDay <= 7; newWeekDay++) {
        if (![supporDays containsObject:@(newWeekDay)]) {
            continue;
        }
        NSInteger temp = 0;
        NSInteger days = 0;
        temp = newWeekDay - comps.weekday;
        days = (temp >= 0 ? temp : temp + 7);
        
        NSDate *date = [[NSDate dateWithTimeIntervalSinceNow:timeInterval] dateByAddingTimeInterval:3600 * 24 * days];
        NSDateComponents *notificationComps = [[NSDateComponents alloc] init];
        notificationComps = [calendar components:unitFlags fromDate:date];
        
        UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:notificationComps repeats:YES];
        
        UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:[NSString stringWithFormat:@"%@-%@", notificationId, @(newWeekDay)]
                                                                              content:content trigger:trigger];
        
        //添加推送成功后的处理!
        [center addNotificationRequest:request withCompletionHandler:nil];
    }

代码可能无法直接拷贝使用,仅供参考思路。

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