objective-c - 每天在特定时间安排 UILocalNotification

标签 objective-c ios7 uilocalnotification nscalendar

我正在使用下面的代码在特定时间安排本地通知。但是通知每分钟重复一次,而不是一天后重复一次。我是否错过了任何通知设置。我的时区是(亚洲/加尔各答 (IST) 偏移量 19800) 并使用 iPhone 4s。

- (void)applicationDidEnterBackground:(UIApplication *)application
{     
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
    NSDate *now = [NSDate date];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now]; 
    [components setHour:12];
    [components setMinute:00];

    UILocalNotification *notification = [[UILocalNotification alloc]init];
    notification.fireDate = [calendar dateFromComponents:components];
    [notification setAlertBody:@"U got notification!!!"];
    // notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
}

最佳答案

您应该指定repeatInterval

- (void)applicationDidEnterBackground:(UIApplication *)application
{     
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
    NSDate *now = [NSDate date];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now]; 
    [components setHour:12];
    [components setMinute:00];

    UILocalNotification *notification = [[UILocalNotification alloc]init];
    notification.fireDate = [calendar dateFromComponents:components];
    notification.repeatInterval = NSDayCalendarUnit;
    [notification setAlertBody:@"U got notification!!!"];
    // notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
}

关于objective-c - 每天在特定时间安排 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25030060/

相关文章:

objective-c - *.m 文件中的@interface 关键字

objective-c - 如何应对点击速度过快的用户

ios - NSURLSession 失效并取消 : Bad Access

ios - 无法连接到 SBApplicationIcon 以在 iPhone 5、IOS 7.0.4 上进行调整

iphone - uilocalnotification 未在准确时间触发

objective-c - 在 iOS 4 中,只有点击按钮两次,UIActionSheet 才会起作用

iphone - 为 UIButton 设置一个子类来处理外观

iOS 多对等连接显示相同的设备名称两次

iOS 如何在前台显示本地通知?

ios - 定时器到时发送本地通知