我正在通过收集 365 个英语“谚语”来创建一个应用程序。在我的应用程序中,我想使用本地通知(每天我的应用程序中的一句谚语应该通知给用户)。我想使用本地通知,而不是推送通知。是否可以每天(365 天)用不同的说法通知用户。谁能帮助我......
问问题
1104 次
2 回答
0
Yes you can.Just create array with 365 dictionaries. dictionary keys should be id and saying.
UILocalNotification *localNotif = [[UILocalNotification alloc]init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *datePickerDate = [NSDate date];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:datePickerDate];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:datePickerDate];
NSDateComponents *dateComps = [[NSDateComponents alloc]init];
[dateComps setYear:[dateComponents year]];
[dateComps setMonth:[dateComponents month]];
[dateComps setDay:[dateComponents day] + 1];
[dateComps setHour:[timeComponents hour] ];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:00];
NSDate *fireDate = [calendar dateFromComponents:dateComps];
//NSLog(@"%@",fireDate);
localNotif.fireDate = fireDate;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.alertAction = @"Alarm";
localNotif.repeatInterval = NSDayCalendarUnit;
localNotif.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"id", nil];
localNotif.alertBody = [saying with id 1];//you can use predicate here
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
in application delegate,
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
int i = [[localNotif.userInfo objectForKey:@"id"] intValue] + 1;
localNotif.alertBody = [saying in index i ];
[localNotif.userInfo setValue:i forKey:@"id"];
}
于 2013-06-05T11:09:26.157 回答