iOS--使用本地推送实现自定义闹钟

#####需求: 从服务器获取到一组时间,都设置成闹铃,默认是每天循环不可修改,同时可以自定义闹铃,可选择周几循环。可以设置打开关闭或者删除。 #####一. 固定闹钟,每天循环。 很简单,将需要设置成闹钟的时间放入一个数组中,通过for循环设置成对应的本地推送,详情见下面的代码和注释。

/**
 下载完成  添加巡逻提醒

 @param array 下载来的巡逻提醒数据
 */
- (void)addLocalNotification:(NSArray *)array
{
        // 设置一个按照固定时间的本地推送
        NSDate *now = [NSDate date];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *components = [[NSDateComponents alloc] init];
        NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        components = [calendar components:unitFlags fromDate:now];
    
    //   通过循环  将每一个时间都设置成本地推送
        for (int i=0; i<array.count; i++) {
                //设置时区(跟随手机的时区)
                UILocalNotification *localNotification = [[UILocalNotification alloc] init];
                localNotification.timeZone = [NSTimeZone defaultTimeZone];
                if (localNotification) {
                    //   设置推送时的显示内容
                    localNotification.alertBody = array[i][@"site_name"];
                    localNotification.alertAction = NSLocalizedString(@"All_open",@"");
                    //   推送的铃声  不能超过30秒  否则会自定变为默认铃声
                    localNotification.soundName = @"2.caf";
                    //小图标数字
                    localNotification.applicationIconBadgeNumber++;
                    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                    [formatter setDateFormat:@"HH:mm"];
                    NSDate *date = [formatter dateFromString:array[i][@"timer"]];
                    NSLog(@"date====%@",date);
                    //通知发出的时间
                    localNotification.fireDate = date;
                }
                //循环通知的周期   每天
                localNotification.repeatInterval = kCFCalendarUnitDay;
                
                //设置userinfo方便撤销
                NSDictionary * info = @{@"infoKey":array[i][@"infoKey"],@"site_name":array[i][@"site_name"]};
                localNotification.userInfo = info;
                //启动任务
                [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
            }
}

复制代码
自定义闹钟

将要进行循环的星期储存在一个数组里面,通过获取当前日期计算出数组中的星期分别是几号,然后按照上面的方式进行周循环。详细见下面的代码和注释。

/**
 自定义闹钟    有重复

 @param dict 巡逻提醒数据
 @param array 要重复的日期(周几)
 */
-(void)addMineLocalNotification:(NSDictionary *)dict againTime:(NSMutableArray *)array
{
    NSArray *clockTimeArray = [dict[@"timer"] componentsSeparatedByString:@":"];
    NSDate *dateNow = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    //[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    //[comps setTimeZone:[NSTimeZone timeZoneWithName:@"CMT"]];
    NSInteger unitFlags =  NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    comps = [calendar components:unitFlags fromDate:dateNow];
    [comps setHour:[[clockTimeArray objectAtIndex:0] intValue]];
    [comps setMinute:[[clockTimeArray objectAtIndex:1] intValue]];
    [comps setSecond:0];
    
    //获取当前的日期以及设定好对比数据
    Byte weekday = [comps weekday];
    Byte i = 0;
    Byte j = 0;
    int days = 0;
    int	temp = 0;
    Byte count = [array count];
    Byte clockDays[7];
    
    NSArray *tempWeekdays = [NSArray arrayWithObjects:@"日", @"一", @"二", @"三", @"四", @"五", @"六", nil];
    //查找设定的周期模式
    for (i = 0; i < count; i++) {
        for (j = 0; j < 7; j++) {
            if ([[array objectAtIndex:i] isEqualToString:[tempWeekdays objectAtIndex:j]]) {
                clockDays[i] = j + 1;
                break;
            }
        }
    }
    //  根据相差天数  计算出第一次响铃的日期  并设置周循环
    for (i = 0; i < count; i++) {
        temp = clockDays[i] - weekday;
        days = (temp >= 0 ? temp : temp + 7);
        NSDate *newFireDate = [[calendar dateFromComponents:comps] dateByAddingTimeInterval:3600 * 24 * days];
        
        UILocalNotification *newNotification = [[UILocalNotification alloc] init];
        if (newNotification) {
            newNotification.fireDate = newFireDate;
            newNotification.alertBody = dict[@"site_name"];
            newNotification.applicationIconBadgeNumber++;
            newNotification.soundName = @"2.caf";
            newNotification.alertAction = NSLocalizedString(@"All_open",@"");
            newNotification.repeatInterval = NSCalendarUnitWeekday;
            NSDictionary * info = @{@"infoKey":dict[@"infoKey"],@"site_name":dict[@"site_name"]};
            newNotification.userInfo = info;
            [[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
        }
        NSLog(@"Post new localNotification:%@", [newNotification fireDate]);
    }
}
复制代码
删除闹钟
/**
 删除某一个巡逻提醒   开关关闭

 @param dict 要删除的巡逻提醒数据
 */
-(void) deleteLocalNotification:(NSDictionary *)dict
{
    // 获取所有本地通知数组
    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    for (UILocalNotification *notification in localNotifications)
    {
        NSDictionary *userInfo = notification.userInfo;
        if ([dict[@"infoKey"] isEqualToString:userInfo[@"infoKey"]]) {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
        }
    }
}
复制代码

有一些是我自己程序中用到的代码,注意舍弃,不要被误导。 本地推送的数量是有限制的,好像是64个,不太确定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值