Quy*_*Quy 2 iphone notifications local repeat ios4
我想测试添加本地通知.我希望它每天/每小时重复一次.我怎样才能做到这一点?
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];
Run Code Online (Sandbox Code Playgroud)
Aja*_*rma 12
如果你想在每天的特定时间设置通知....然后你需要设置通知的repeatInterval属性.iOS处理它; s拥有关于通知.只需添加这一行......你错过了.否则你的代码很好并且会起作用.
notif.repeatInterval = NSDayCalendarUnit;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16603 次 |
| 最近记录: |