iPhone: Как настроить повторное ежедневное / почасовое локальное уведомление? - PullRequest
2 голосов
/ 06 августа 2010

Я хочу проверить добавить локальное уведомление.Я хочу, чтобы это повторялось ежедневно / ежечасно.Как я могу это сделать?

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

 // Get the current date
    NSDate *now = [NSDate date];

 // Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit |       NSMonthCalendarUnit |  NSDayCalendarUnit )
           fromDate:now];

NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit)
           fromDate:now];


 // Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];

[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];

[dateComps setHour:[timeComponents hour]];
 [dateComps setMinute:[timeComponents minute]];
 [dateComps setSecond:[timeComponents second]+10];

 // Notification will fire in one minute

NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;

localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];

 // Notification details
localNotif.alertBody = @"Hello World";

 // Set the action button
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;

applicationIconBadgeNumber++;

localNotif.applicationIconBadgeNumber = applicationIconBadgeNumber;

 // Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

Ответы [ 4 ]

12 голосов
/ 18 октября 2011

Если вы хотите устанавливать уведомления в определенное время ежедневно ... одна строка ... которую вы пропустили. В противном случае ваш код в порядке и будет работать.

notif.repeatInterval = NSDayCalendarUnit;
2 голосов
/ 22 сентября 2010

Ознакомьтесь с этим уроком: http://useyourloaf.com/blog/2010/9/13/repeating-an-ios-local-notification.html

2 голосов
/ 06 августа 2010

Ваш код был бы намного проще, если бы вы использовали dateByAddingComponents: toDate: options:.

(dateByAddingTimeInterval: неправильно обрабатывает календари, например, изменения часового пояса.)

Если вы хотите, чтобы это повторилось, я думаю, вам нужно добавить несколько или повторно добавить их при следующем запуске приложения. В противном случае уведомление, которое повторяется каждую секунду, будет невероятно раздражающим.

1 голос
/ 31 июля 2012
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;

NSDate *fireTime = [[NSDate date]   dateByAddingTimeInterval:900]; // 15 minutes

localNotif.fireDate = fireTime;
localNotif.alertBody = @"15 min reached";
localNotif.repeatInterval=kCFCalendarUnitMinute;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
...