Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

ios - UILocalNotification isn't working at all

I'm having some really irritating problems with UILocalNotification.

While finishing up an app that I've nearly completed, I noticed that I couldn't get local notifications to work, no matter what I tried.

So instead of wasting time, I decided to go back to basics and see if I could get them working at all.

I created a new XCode view-based application, and replaced -viewDidLoad with this:

- (void)viewDidLoad
{
    UILocalNotification * theNotification = [[UILocalNotification alloc] init];
    theNotification.alertBody = @"Alert text";
    theNotification.alertAction = @"Ok";
    theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

    [[UIApplication sharedApplication] scheduleLocalNotification:theNotification];
}

However, that also doesn't do anything at all.
I expected to see a notification 10 seconds after launching the app, but nothing appears.
Also, I tested this on both my iPhone and the simulator.

Am I missing something really crucial here? (I've searched through the Apple documentation and couldn't find anything as to why this is happening)

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

UILocalNotifications are only displayed automatically if the app is not running (or running in background). If the app is running and a local notification fires, UIApplicationDelegate’s - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification method gets called and the system doesn’t display anything (nor does it play a sound). If you want to display the notification, create an UIAlertView yourself in the delegate method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...