本文介绍了每天在特定时间安排UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using below piece of code to schedule a localnotification at specific time. But the notification is repeating for every minute instead of after one day. Whether i have missed any notification settings. My timezone is (Asia/Calcutta (IST) offset 19800) and using 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];
}

这篇关于每天在特定时间安排UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 00:20