Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Daily Local Push Notifications

I want to execute a UILocalNotification at 9:00 AM every day, forever, as long as the app is open. The closest thing I've found is this:

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24];
notification.alertBody = @"It's been 24 hours.";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

However, this code only executes a UILocalNotification once in 24 hours, not at a specified time. I've been looking into utilizing NSDate somehow, but have been getting no where.

The code will be executing in the AppDelegate in the application didFinishLaunchingWithOptions method. If someone were to open the application and place it in the background at 8:59 AM, the UILocalNotification would still execute at 9:00 AM.

An NSDateComponent won't work with this because I would have to declare a year, month, and day, but I want to execute this UILocalNotification every day without having to edit the code.

like image 876
Michael Avatar asked May 19 '14 22:05

Michael


People also ask

Can I schedule when I get notifications on iPhone?

Schedule a notification summaryGo to Settings > Notifications > Scheduled Summary, then turn on Scheduled Summary. Under Apps in Summary, select the apps that you want to include in your notification summary. Under Schedule, tap the Add button to add a new schedule or the Remove button to remove a schedule.

How many local notifications can be scheduled iOS?

Local notification volume is limited to 64 notifications per iOS app per day, which can be constraining if notifications are vital to your app's functionality and you have a large user base.

How do I view push notifications history on iPhone?

Touch your locked screen, hold, and scroll up. If you have notifications turned on, the history should appear.


2 Answers

You need to find the NEXT time that 9am occurs, and set the local notification to fire at that time:

NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:9];
// Gives us today's date but at 9am
NSDate *next9am = [calendar dateFromComponents:components];
if ([next9am timeIntervalSinceNow] < 0) {
    // If today's 9am already occurred, add 24hours to get to tomorrow's
    next9am = [next9am dateByAddingTimeInterval:60*60*24];
}

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = next9am;
notification.alertBody = @"It's been 24 hours.";
// Set a repeat interval to daily
notification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
like image 93
Sacremento Bill Avatar answered Sep 28 '22 08:09

Sacremento Bill


Your question has two problems:

  1. Obtain the right NSDate for the next fire date.
  2. Setup the local notification in order to fire every 24 hours

Here is a snippet:

// 1st: find next fire date, using NSDateComponents

NSDate * date = [NSDate date];
NSDateComponents * components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:date];

// Components will now contain information about current hour and minute, day, month and year.

// Do your calculation in order to setup the right date. Note that components reflect user timezone.
// For example, skip to the next day if current time is after 9:00:
if (components.hour >= 9) {
    components.day += 1;
}

// Now fix the components for firing time, for example 9:00.
components.hour = 9;
components.minute = 0;

NSDate * fireDate = [[NSCalendar currentCalendar] dateFromComponents:components];

NSLog(@"Notification will fire at: %@", fireDate);


// 2nd: Schedule local notification with repetitions:

UILocalNotification * notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.repeatInterval = NSDayCalendarUnit; // Here is the trick

notification.alertBody = @"It's been 24 hours.";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
like image 39
Alessandro Orrù Avatar answered Sep 28 '22 10:09

Alessandro Orrù