UILocalNotification when iPhone switched off

天大地大妈咪最大 提交于 2019-11-27 07:11:41

问题


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.

  • No notification is fired after device is restarted.
  • Notification is fired when the device is restarted i.e. at 4:00pm

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


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/8573013/uilocalnotification-when-iphone-switched-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!