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

前端 未结 4 872
眼角桃花
眼角桃花 2020-12-16 08:45

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

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

 /         


        
相关标签:
4条回答
  • 2020-12-16 08:47
    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];
    
    0 讨论(0)
  • 2020-12-16 08:52

    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;
    
    0 讨论(0)
  • 2020-12-16 08:53

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

    0 讨论(0)
  • 2020-12-16 09:00

    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.

    0 讨论(0)
提交回复
热议问题