我正在尝试让我的应用程序使用本地通知。但我无法转过头来。我正在处理这个问题好几天。我有core database
一个实体Favorites
(所有我最喜欢的艺术家)和一个实体Artists
(艺术家他的详细信息。
我有一个按钮来设置本地通知。当我按下按钮时,我执行以下操作。
-(void)addLocalNotifications{
GenkonStageDataModel *model = [[GenkonStageDataModel alloc]init];
NSMutableArray *allFavorites = [model getAllFavorites];
NSLog(@"allFavoriets count is %d",allFavorites.count);
UIApplication* app = [UIApplication sharedApplication];
for (int i = 0; i<allFavorites.count; i++){
Favorites *favorite = [allFavorites objectAtIndex:i];
int artId = [favorite.fav_art_id intValue];
Artists *artist = [model getArtistById:artId];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
NSDate *datePush = [self getDateForArtist:artist];
NSLog(@"Push notification should send on: %@",datePush);
notifyAlarm.fireDate = datePush;
NSDictionary *dicNotification = [[NSDictionary alloc]initWithObjectsAndKeys:artist.art_id,@"pushKey", nil];
notifyAlarm.userInfo = dicNotification;
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = @"Glass.aiff";
notifyAlarm.alertBody = [NSString stringWithFormat:@"%@ starts in 15 minutes",artist.art_name];
[app scheduleLocalNotification:notifyAlarm];
NSLog(@"Added");
}
}
}
该方法的作用如下。
1. Get all the favorites
2. Loop through the favorites and get the artists linked with that favorite by ID
3. Get the date for when the local notification should be sent
4. Set in the userInfo dictionary the artist ID (I do this for deleting the local notification when I want to)
5. Shedule the local notification.
现在所有这些都被添加了(我认为),因为它正确地循环通过数组,并且总是在我的日志中给我正确的日期和“添加”。
但是现在当我更改我的设备时,日期时间是我通常应该收到本地通知的时间。我什么都没有收到!!!!
我还将日期更改为早 2 小时以获取正确的时间。因为当我使用示例本地通知进行测试时。他们这样设置了fireDate。
NSDate *alertTime = [NSDate date];
这导致在我单击按钮后立即发送本地通知。但是当我记录这个时,我注意到它比实际时间早了 2 小时......?
我真诚地希望任何人都可以帮助我解决这个问题!提前致谢!
编辑
这就是我得到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];
int day2;
if(timeStart >=2400 ){
day = day++;
}
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];
[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;
}
我得到这个日志
2013-06-30 14:37:13.910 genkonstage[1633:907] date is 2013-06-30 16:50:00 +0000
2013-06-30 14:37:13.913 genkonstage[1633:907] Push notification should send on: 2013-06-30 16:35:00 +0000