我正在处理本地通知。我收到了通知。但我正在努力设定正确的日期。
首先让我描述一下情况。我正在制作节日应用。这个节日是在6月28日至29日。每个艺术家对象都有一个art_day。这可以是1 - 2或3。
1 --> 28 JUNE
2 --> 29 JUNE
3 --> 30 JUNE
我想在艺术家开始前15分钟收到本地通知。所以这就是我如何制作通知的fireDate。
-(NSDate *)getDateForArtist:(Artists *)artist{
int day = [artist.art_day intValue];
int timeStart = [artist.art_timestart intValue];
NSString *timeStart2 = [self getTimeStamp:artist.art_timestart];
int hours = [[timeStart2 substringWithRange:NSMakeRange(0,2)]intValue];
int minutes = [[timeStart2 substringWithRange:NSMakeRange(2,2)]intValue];
//in my db timeStart looks like 1530 --> 15h30 (I know bad db structure!!!)
int day2;
if(day == 1){
NSLog(@"day is 28");
day2 = 28;
}else if (day == 2){
NSLog(@"day is 29");
day2 = 29;
}else{
NSLog(@"day is 30");
day2 = 30;
}
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
[comps setTimeZone:timeZone];
[comps setDay:day2];
[comps setMonth:06];
[comps setYear:2013];
[comps setHour:hours];
[comps setMinute:minutes];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSLog(@"date is %@",date);
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setMinute:-15];
NSDate *date2 = [gregorian dateByAddingComponents:offsetComponents toDate:date options:0];
NSLog(@"date -15 min %@", date2);
return date2;
}
这是我创建通知的方式
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
NSDate *datePush = [self getDateForArtist:artist];
NSLog(@"artist plays on: %@ on hour %@",artist.art_day,artist.art_timestart);
NSLog(@"Push notification should send on: %@",datePush);
notifyAlarm.fireDate = datePush;
NSDictionary *dicNotification = [[NSDictionary alloc]initWithObjectsAndKeys:artist.art_id,@"pushKey", nil];
notifyAlarm.userInfo = dicNotification;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = @"Glass.aiff";
notifyAlarm.alertBody = [NSString stringWithFormat:@"%@ starts in 15 minutes",artist.art_name];
[app scheduleLocalNotification:notifyAlarm];
NSLog(@"Added");
}
但由于某种原因,我没有得到任何推送通知。或者我认为日期不正确。
这是我从日志中得到的
2013-06-29 15:37:17.801 genkonstage[14120:907] artist plays on day: 2 on hour 2020
2013-06-29 15:37:17.803 genkonstage[14120:907] Push notification should send on: 2013-06-29 20:05:00 +0000
有人可以帮我吗?