iOS UILocalNotification not being displayed while app in background

非 Y 不嫁゛ 提交于 2019-12-10 10:17:15

问题


FIXED — Ok, found what it was, there was an errant [[UIApplication sharedApplication] cancelAllLocalNotifications]; being fired when I wasn't expecting it.

Well there's your problem.

Thanks for the help everyone, sorry to have it turn out to just be dumb coder syndrome.

I've built out my local notification like so:

- (void)scheduleNotification {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {
        UILocalNotification *notif = [[cls alloc] init];
        NSLog(@"%@", [NSDate date]);
        notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

        notif.alertBody = NSLocalizedString(@"Hello.", nil);

        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        NSLog(@"Notification scheduled at %@", notif.fireDate);
        [notif release];
    }
}

As expected my debug log outputs the correct fireDate 10 seconds in the future. If I don't leave my app I get a successful application:didReceiveLocalNotification: callback.

The hiccup here is if I push the button to schedule this notification and hit the home button to put it in the background. If I do this, the notification never fires and I never get an alert view from the OS.

Did I miss something obvious here? I've looked up and down here and the Apple docs and feel I've missed something obvious.

Any help would be greatly appreciated. Thanks.


回答1:


See the example in Apple's docs: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1

Could it be that not setting timeZone to [NSTimeZone defaultTimeZone] is causing the problem? GMT is assumed if timeZone isn't set (nil default).




回答2:


Have you tried wrapping the code in a background task?




回答3:


Ok, found what it was, there was an errant [[UIApplication sharedApplication] cancelAllLocalNotifications]; being sent when entering the background.



来源:https://stackoverflow.com/questions/6313641/ios-uilocalnotification-not-being-displayed-while-app-in-background

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