iPhone: How to set repeat daily/hourly local notification?

北慕城南 提交于 2019-12-18 03:47:03

问题


I want to test add local notification. I want it repeat daily/hourly. How can I do that?

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];

回答1:


If you want to set the Notifications at a particular time on daily ... .then you need to set the notification repeatInterval property.iOS handles of it;s own about the notification.Just add this single line ...which you missed out. Otherwise you code is fine & will work.

notif.repeatInterval = NSDayCalendarUnit;



回答2:


Your code would be much simpler if you used dateByAddingComponents:toDate:options:.

(dateByAddingTimeInterval: does not handle calendars properly, e.g. time zone changes.)

If you want it to repeat, I think you have to add several, or re-add them when your app is next launched. Otherwise, a notification that repeated every second would be incredibly annoying.




回答3:


Check out this tutorial: http://useyourloaf.com/blog/2010/9/13/repeating-an-ios-local-notification.html




回答4:


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];


来源:https://stackoverflow.com/questions/3426882/iphone-how-to-set-repeat-daily-hourly-local-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!