42

I just downloaded xcode and trying to make local notification example. The question is if local notification works in simulator?

thank you

user349302
  • 3,491
  • 7
  • 27
  • 31

5 Answers5

72

Yes, local notifications work with the simulator. However, make sure you are implementing application:didreceiveLocalNotification in your app delegate if you want to see the notification while your app is in the foreground:

- (void)application:(UIApplication *)application
    didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MyAlertView"
        message:notification.alertBody
        delegate:self cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
    [alertView show];
    if (alertView) {
        [alertView release];
    }
}

Otherwise, make sure you schedule your notification for some time in the future, then close the application, in order to see the Apple sample work:

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Alert!";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

It's easy to think you're not implementing the test code correctly, and you just aren't handling the event while the app is running.

mbuechmann
  • 5,413
  • 5
  • 27
  • 40
bojolais
  • 1,491
  • 12
  • 13
20

Another gotcha you might find, for anyone who stumbles on this older question: iOS 8 introduced new notification permissions; and your app has to explicitly ask for them.

In your AppDeligate.m:

- (BOOL)application:(UIApplication *)application 
          didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //register local notifications
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }

    //the rest of your normal code

    return YES;
}

If you don't, your notification will never fire, and you'll get a wonderful message like this in your logs: "Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7ae51b10>{... alert details ...} with an alert but haven't received permission from the user to display alerts"

mix3d
  • 4,122
  • 2
  • 25
  • 47
7

local notifications work on simulator, push notifications do not

Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
2

To test local notifications in iPhone simulator, follow these steps:

  1. As simulator time is exactly the one in your machine, change the time of your machine to the 1-minute previous of your desired time (when you are expecting your local notification to fire)
  2. Restart simulator(this is awkward, but sometimes it seems iPhone simulator fails to get updated time instantly)
  3. Run the simulator once again(maybe by running your app from xcode, in which case you must press the home button to send your app to background). Once the time is reached you should see the notification

These steps helped me always to get successful local notifications.

Munim
  • 2,626
  • 1
  • 19
  • 28
0

App Settings Notification Section not showing

I ran into this issue as well. What seems to happen is the following,

  1. Your app runs / is installed on simulator (might have to reset the simulator state of your app, see below).
  2. Your app has not asked for permission to show notifications.
  3. Therefore your app settings (Settings > App Name) won't show the notification section,

App Settings showing Siri and Search and Background App Refresh but no Notifications listing

  1. You have to request notifications in the app (using UNUserNotificationCenter.current().requestAuthorization etc).

Allow notifications request alert

  1. Then your app will have a section for notifications (even if you deny the allow notification prompt)!

App settings now showing notifications section along with Siri and Search and Background App Refresh

Resetting App State

After this I launched the app again and the notification setting still showed (and showed as Off since I denied permission).

To reset this and reproduce the original problem I had to do the following,

  1. Enable simctl XCode command line utility (See How do I fix the xcrun unable to find simctl error? )

XCode Preferences > Locations > Command Line Tools

  1. Run in terminal xcrun simctl uninstall booted com.app.bundlename (see How to run a fresh install of the application every time unit tests are run?)
Zimm3r
  • 3,369
  • 5
  • 35
  • 53