最近项目要做一个吃药提醒的功能,安卓那边直接调用系统闹钟,iOS这边主要用推送的功能来实现,在做的过程中发现几个问题:
1.闹钟的震动的问题:
闹钟的提示震动,只能震动一次提醒,如果需要连续震动,需要用户点击去才会触发连续震动的事件(如果谁有好的方法,可以在评论中给在下)
2.时间的处理的问题:
当登录一个账号,加入闹钟(实际就是推送),再切换到另一个账号的时候,上一个账号的闹钟的时间的还在,当时间到的时候,会继续推送,当时想直接存在缓存中,当切换的时候本地的推送关闭,重新登录的时候,再重新加载,但考虑到有些用户,不会点击此功能,感觉用户体验不好就去掉了
3.时间计算的问题:
计算的时候,一定看清是一周的每天,还是一个月的每周循环,还是一年的每周的循环。我就是没看清,测试那边的测试机每天都响😄,然后回头查原因的时候才看到
4.对于时间的计算与加入问题,直接上代码吧
/**
@param dates 传入天数数组 1,2,3
@param times 传入时间数组 8:00,9:00
@param music 0,1,2 响铃方式
*/
-(void)addNotitifacate:(NSString *)dates withTimes:(NSString *)times withtype:(NSString *)music withTitle:(NSString*)title withId:(NSString *)alarmId{
NSArray *Dates = [dates componentsSeparatedByString:@","];
NSArray *Times = [times componentsSeparatedByString:@","];
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ZH_cn"];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitMinute fromDate:now];
NSInteger weekDay = [comp weekday];
NSInteger currentday = [comp day];
long firstDiff;
// if (weekDay == 1) {
// firstDiff = 1;
// lastDiff = 0;
// }else{
// lastDiff = 9 - weekDay;
// }
NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:now];
for (NSString *timeString in Times) {
NSString *minte = [timeString substringToIndex:2];
NSString *seconde = [timeString substringFromIndex:3];
for (NSString *daystring in Dates) {
if(weekDay==0) weekDay=8;
firstDiff = [daystring longLongValue] - weekDay+2;
[firstDayComp setDay:currentday + firstDiff];
NSDate *DayOfWeek= [calendar dateFromComponents:firstDayComp];
NSDate *dayofTime = [DayOfWeek dateByAddingTimeInterval:[minte integerValue]*3600+[seconde integerValue]*60];
if ([dayofTime timeIntervalSinceNow]<0) {
[dayofTime dateByAddingTimeInterval:7*24*3600];
}
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"星期一开始 %@",[formater stringFromDate:dayofTime]);
[[AlarmManager sharedManager] setLocalNotification:dayofTime withbodyTitle:title withType:music withId:alarmId];
}
}
}