Sahil Khanna
Sahil Khanna

Reputation: 4382

UILocalNotification when iPhone switched off

What happens when a UILocalNotofication is scheduled when a device is switched off.

Eg. I schedule a UILocalNotification at 3pm everyday. But the device is switched off from 3:00pm to 4:00pm. I guess any one of the following conditions will be true.

I do not have a device and could not test it on a simulator.

Note: By switch off I mean that the device is turned off, and not sleep/stand by mode

Upvotes: 3

Views: 2073

Answers (2)

Klaas
Klaas

Reputation: 22763

Local Notifications will be triggered after turning your device off and on.

I wrote a tiny test app, that verifies this behaviour:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSDate *nowDate = NSDate.date;

    for (int i = 1; i <= 50; i++) {
        UILocalNotification *n = [[UILocalNotification alloc]init];

        n.fireDate = [nowDate dateByAddingTimeInterval:60 * i ];
        n.applicationIconBadgeNumber = 0;

        n.alertAction = @"Open";
        n.alertBody = [NSString stringWithFormat:@"ln %@ %@", @(i), n.fireDate];
        [[UIApplication sharedApplication] scheduleLocalNotification:n];
    }
    return YES;
}

Upvotes: 4

WrightsCS
WrightsCS

Reputation: 50697

When you turn off your device, the notification becomes non-existant, so when you turn your device back on, nothing is going to happen without creating that notification again.

So if you schedule an event for 3PM and your device is turned off at 2:59PM, then back on at 3:59PM, the notification will not fire because it has to be recreated.

Upvotes: -3

Related Questions