Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILocalNotification fires after reinstalling the app

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?

like image 940
Enzo Tran Avatar asked Feb 07 '11 15:02

Enzo Tran


2 Answers

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.

like image 108
Kenn Cal Avatar answered Oct 24 '22 19:10

Kenn Cal


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.

like image 6
KingofBliss Avatar answered Oct 24 '22 18:10

KingofBliss