10

I am making an app in iOS where it will send local notifications on a timer. It works fine when the app is in background state, but does not work when it has been terminated and closed completely.

Is there anyway to still send local notifications when this has happened?

Thanks in advance.

Bharat Nakum
  • 647
  • 6
  • 18
Henry Brown
  • 2,219
  • 8
  • 31
  • 48

2 Answers2

13

Sure it is possible, if you schedule a local notification it will fire even if you terminate the app.

        UILocalNotification *_localNotification = [[UILocalNotification alloc] init];
        _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:_value];
        _localNotification.timeZone = [NSTimeZone defaultTimeZone];
        _localNotification.alertBody = @"Beep... Beep... Beep...";
        _localNotification.soundName = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication]scheduleLocalNotification:_localNotification];

... works like a charm for me.

Johannes
  • 325
  • 4
  • 11
  • 2
    Thanks, this is effectively what I do, I use a fire date which re-runs on a timer, so a local notification is fired every say 20 minutes. The problem is that if that app is terminated for whatever reason, the fire date will not work? – Henry Brown Aug 28 '15 at 13:04
  • Probably the repeat function does not work when the app is not running. Have you tried scheduling several notifications instead of repeating one notification? – Johannes Aug 28 '15 at 13:07
  • No but that's actually not a bad idea...Can i schedule multiple fire dates from one local notification object? – Henry Brown Aug 28 '15 at 13:08
  • I don't think so, but you could just create a bunch of notifications with the same content but different fire dates. The limit is 64 notifications scheduled for one app, as far as I know. – Johannes Aug 28 '15 at 13:12
  • Thanks, much appreciated. – Henry Brown Aug 28 '15 at 13:14
4

The local notification can be delivered when the app is terminated or closed, but they must be scheduled when the app is running. You can set the fireDate for a notification in the future, schedule it, and it will be delivered whether or not the app is running.

picciano
  • 22,341
  • 9
  • 69
  • 82