6

My app has an alarm function using UILocalNotification, and it works great. However, if the user uninstalls the app, then later REINSTALLS it, he would receive all the "in between" notifications at once.

I have tried to call:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

if it's the first time the app is launched, but it doesn't help, because the notification is received even before application:didFinishLaunchingWithOptions: is called.

This was worse in 4.0 when the alarm was repeated even if the user has deleted the app, but at least that bug was fixed by Apple in later release. However now I'm stuck with this. Anyone has an idea?

Enzo Tran
  • 5,750
  • 6
  • 31
  • 36

2 Answers2

15

According to Apple, this is not a bug (I filed a bug report). The system retains the UILocalNotifications for uninstalled apps for 24 hours just in case the user deleted the app by accident, and restores the said UILocalNotifications if the app is re-installed within that time frame.

The solution would be to remove all UILocalNotifications on first startup, like so:

- (BOOL)          application: (UIApplication*) application
didFinishLaunchingWithOptions: (NSDictionary*)  launchOptions
{
  if (self.isFirstRun)
  {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    self.firstRun = NO;
  }

  /* Other code here */
  ...
}

of course, implement your own firstRun setter and getter to fetch/save into persistent storage, like NSUserDefaults.

Kenn Cal
  • 3,659
  • 2
  • 17
  • 18
  • Is this "feature" documented anywhere? – race_carr Apr 07 '14 at 15:43
  • +1 This seems pretty stupid. The app doesn't own the notifications once they're handed over to the OS, which is clear when you try to schedule 65 notifications instead of the max of 64. The app should be responsible for storing its own representation of whatever the notification signifies--not rely on the OS. If such an app launches with no previous knowledge of these notifications then they're just garbage. – Andreas May 27 '15 at 19:51
6

This is actually a bug in iPhone. If you removed the application and install it later also, it will have same app id, so when the application is reinstalled all the past local notifications were fired even if you didn't open the app.

halfer
  • 19,824
  • 17
  • 99
  • 186
KingofBliss
  • 15,055
  • 6
  • 50
  • 72