Jay Baird
Jay Baird

Reputation: 120

iOS UILocalNotification not being displayed while app in background

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.

Upvotes: 3

Views: 3234

Answers (3)

Jay Baird
Jay Baird

Reputation: 120

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

Upvotes: 0

hanleyp
hanleyp

Reputation: 811

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).

Upvotes: 1

Alexsander Akers
Alexsander Akers

Reputation: 16024

Have you tried wrapping the code in a background task?

Upvotes: 0

Related Questions