Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone:如何设置重复的每日/每小时本地通知?_Iphone_Ios4_Local_Notifications_Repeat - Fatal编程技术网

iPhone:如何设置重复的每日/每小时本地通知?

iPhone:如何设置重复的每日/每小时本地通知?,iphone,ios4,local,notifications,repeat,Iphone,Ios4,Local,Notifications,Repeat,我想测试添加本地通知。我想每天/每小时重复一次。我该怎么做 NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; // Get the current date NSDate *now = [NSDate date]; // Break the date up into components NSDateComponents *dateComponents = [calendar components:(

我想测试添加本地通知。我想每天/每小时重复一次。我该怎么做

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

如果使用dateByAddingComponents:toDate:options:,代码会简单得多

(dateByAddingTimeInterval:无法正确处理日历,例如时区更改。)


如果你想让它重复,我认为你必须添加几个,或者在你的应用程序下一次启动时重新添加它们。否则,每秒重复一次的通知将非常烦人。

请查看本教程:

如果您想在每天的特定时间设置通知。然后需要设置它的通知repeatIntervalproperty.iOS句柄;这是我们自己的通知。只需添加这一行…你错过了。否则,您的代码就可以了&可以工作了

notif.repeatInterval = NSDayCalendarUnit;

您可以尝试正确格式化您的代码。我们可以使用NSDayCalendarUnit中的随机时间吗??我想每天做一次随机的本地通知。。。请Help@shaqirsaiyed:您可能必须手动创建每个随机日的firetime,这不是正常的“重复间隔”。
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];