1

I am trying to incorporate localnotifications that fire after a certain time interval, however, I can't seem to get the notification to display at any point when testing with the simulator.

I simply want to display the local notification 10 seconds after creation, however, when running the app in the simulator nothing ever displays regardless of whether the app is in the foreground or background.

Am I missing something? Could this have something do to with timezone?

//Notification setup

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"Testing";
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

//This is the didReceiveLocalNotification in my app delegate

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
HJordan35
  • 160
  • 2
  • 9

2 Answers2

7

1) Have you registered your up to use Notifications? You can see my post here on what I faced just by missing this simple step. It's in Swift, but you get the gist: UILocalNotification not doing anything

2) I don't think Notifications work when you are inside of the App, because then it defeats the purpose of Notifications. Notifications are supposed to alert you when you are not in the App i.e. Background Mode or Terminated. So when you are testing, make sure you are not in the app

3) In general, testing notifications is better done on a real device from my experience than the simulator.

Community
  • 1
  • 1
Lavvo
  • 1,024
  • 3
  • 16
  • 35
  • Thank you so much for this!! I need to start reading the documentation more instead of relying on tutorials :) Im surprised I couldn't find anything about registering for local notifications! – HJordan35 Oct 21 '15 at 12:47
  • Yup, not a single tutorial ever mentioned this, I don't get why. But like you said, it also helped force me to read the documentation in more detail. Thx for accepting the answer. – Lavvo Oct 21 '15 at 14:58
0

iOS simulator cannot get real location automatically.

You can set a fake location to keep your app going. try to use this to detect if it's a simulator, then set it.

#if (TARGET_IPHONE_SIMULATOR)
ronan
  • 1,611
  • 13
  • 20