UILocalNotification isn't working at all

12,703

Solution 1

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.

Solution 2

Just a comment from my personal adventures in stupidity...

I had the same issue, but my problem was that I had forgotten to assign a value to alertBody. If you don't set alertBody, the notification won't display.

Solution 3

  1. the fireDate must be future time.
  2. app must be running in backdrop, or is closed.
  3. one more thing, do not forget to show query whether to allow push, add below code to AppDelegate:

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
      if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
           UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil]; 
           [application registerUserNotificationSettings:settings]; 
      }
    }
    
Share:
12,703

Related videos on Youtube

JPEG_
Author by

JPEG_

Software engineer at Bloomberg. Primarily C++ and Python.

Updated on May 20, 2022

Comments

  • JPEG_
    JPEG_ about 2 years

    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

  • David
    David about 12 years
    I presume there isn't a way to change this behavior? The notification center is a great way to display information to my user for asynchronous communication events that occur while the app is running.
  • sradforth
    sradforth almost 12 years
    Just a note on another issue to watch out for... if you set the timezone parameter it effects when the alarm goes off as expected... however, if you set it explicitly to UTC to avoid timezones it may not do what you might expect as it still modifies the firedate passed in from UTC to UTC but different somehow. I found that the alert was never triggered when setting UTC timezone parameter so setting this back to nil and ensuring the firedate UTC time was correct fixed the problem.
  • Gerard
    Gerard about 10 years
    I have been hunting this "error" for a long time! Thank you! I would expect a warning at least, since it is surely a mistake to schedule an empty non-displayable notification.
  • Ramesh
    Ramesh about 10 years
    Restored happiness :)
  • James Perih
    James Perih over 8 years
    No where in the Apple Documentation, have I read that I first needed to register for UserNotificationSettings for an App that only has Local Notifications. This helped me, as simple as it sounds.
  • Arthur Gevorkyan
    Arthur Gevorkyan over 8 years
    OMG, @conarch, you saved my day :) I'm a guy of that kind as well :) Thanks for pointing it out .
  • Bill
    Bill over 7 years
    Huh! This isn't mentioned in the documentation, but it appears to be true
  • Matt Butler
    Matt Butler over 7 years
    Wow, I have been fighting this for hours. You are the real MVP :)