我想发出本地通知,每天通知用户 5 次并每天重复它们,我将它们放在一个可变数组中,其中对象是“hh:mm”,它的小时和分钟是固定的 GMT+3 城镇,所以我得到了当前日期并找到时间间隔,然后为通知创建一个日期,这是我实施的方法。-first 应用时区,-second 如果当前时间之前的时间,则将其设置为第二天。-third 设置该日期的本地通知。请帮帮我
问问题
173 次
1 回答
0
通过使用此示例代码,我安排了 2 个通知,一个在早上 7 点,一个在晚上 6 点,并且每天重复一次,它工作得非常好,希望你能找到你的解决方案。
#pragma mark
#pragma mark - Notification Setup
-(void)clearNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
-(void)scheduleNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSMutableArray *arrTemp = [APPDELEGATE.userDefaults valueForKey:@"ParsingResponse"];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
NSDate *now = [NSDate date];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:7];
[components setMinute:0];
NSDate *today7am = [calendar dateFromComponents:components];
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = today7am;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.repeatCalendar = [NSCalendar currentCalendar];
notif.alertBody = [[arrTemp objectAtIndex:0] objectForKey:@"Noti_Morning"];
notif.alertAction = @"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Morning", @"key", nil];
notif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
NSCalendar *calendar2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components2 = [calendar2 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components2 setHour:18];
[components2 setMinute:0];
NSDate *today6pm = [calendar2 dateFromComponents:components2];
UILocalNotification *notif2 = [[cls alloc] init];
notif2.fireDate = today6pm;
notif2.timeZone = [NSTimeZone defaultTimeZone];
notif2.repeatCalendar = [NSCalendar currentCalendar];
notif2.alertBody = [[arrTemp objectAtIndex:0] objectForKey:@"Noti_Evening"];
notif2.alertAction = @"Show me";
notif2.soundName = UILocalNotificationDefaultSoundName;
notif2.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Evening", @"key", nil];
notif2.userInfo = infoDict2;
[[UIApplication sharedApplication] scheduleLocalNotification:notif2];
[notif2 release];
}
}
于 2013-10-03T03:59:44.630 回答