UILocalNotification when iPhone switched off

痞子三分冷 提交于 2019-11-28 12:45:49

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.

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