31

Let's say I set 5 local notification for an iPhone application, then the user deletes the app. If the app is installed again, it shows the previous notifications.

I know the following code deletes all notifications

[[UIApplication sharedApplication] cancelAllLocalNotifications];

But where do I put that code so that it executes when the application is deleted?

Or any other way to solve this problem.

Bradley Thomas
  • 4,060
  • 6
  • 33
  • 55
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
  • 4
    Could you not delete it on first launch instead? For example if a certain flag in UserSettings is not set, you cancel them all and set the flag? – Joachim Isaksson Feb 14 '12 at 05:50
  • In this application i use the database for the storing and retriveing the notification object also – Nimit Parekh Feb 14 '12 at 06:52
  • Neon.. does this helps? http://stackoverflow.com/questions/12369900/when-i-delete-my-ios-application-push-notification-state-remains – Pradip Oct 29 '12 at 17:55

5 Answers5

32

As others have mentioned, I think the best way to accomplish this is to have a flag in didFinishLaunchingWithOptions in the application delegate that checks if it is the first launch or not.

If the app is being launched for the first time, you can call

[[UIApplication sharedApplication] cancelAllLocalNotifications];

This will cancel any existing notifications.

For example:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    // this flag will need to be stored somewhere non-volatile such as using CoreData 
    // or user defaults
    if(flag == nil || [flag count] ==0){ 

       [[UIApplication sharedApplication] cancelAllLocalNotifications];

       // update your flag so that it fails this check on any subsequent launches
       flag = 1;
    }
{
Pochi
  • 13,391
  • 3
  • 64
  • 104
Derek Hewitt
  • 775
  • 8
  • 16
  • Don't local notifications get deleted automatically if an app is deleted? I don't see why they should be reinstated of a fresh app install? – Supertecnoboff Mar 28 '16 at 17:11
  • 1
    I believe they are retained for a certain period of time in case the application is reinstalled. I will post the documentation if I can find it. – Derek Hewitt Mar 28 '16 at 22:47
  • No on deletion Notifications are not cleared. I created an app and I schedule some local notifications. Then I deleted the app and installed again but the notifications kept coming(this time around no implementation for local notifications). This is why I believe they are retained but I am not sure about this(retain) duration – Muhammad Nayab Nov 15 '17 at 22:10
3

Here's what I did:

In Your AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //Check if its first time
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"is_first_time"]) {
        [application cancelAllLocalNotifications]; // Restart the Local Notifications list
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"is_first_time"];
    }

    return YES;
}

Hope it helps!

MontiRabbit
  • 1,020
  • 12
  • 17
3

For swift 3.0 you can use

if (isFirstLaunch){  
  UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}

Then add this method to check if this is the first run

var isFirstLaunch: Bool {
    get {
        if     (NSUserDefaults.standardUserDefaults().objectForKey("firstLaunchDate") == nil) {
            NSUserDefaults.standardUserDefaults().setObject(NSDate(),     forKey: "firstLaunchDate")
            NSUserDefaults.standardUserDefaults().synchronize()
            return true
        }
        return false
    }
}
Igor Kovryzhkin
  • 2,195
  • 1
  • 27
  • 22
0

You can use UIPasteboard to store a flag as DerekH already mentioned.
Something link this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([self mustCancelNotifications]) {
        [application cancelAllLocalNotifications];
        [self removeTag];
    } else {
        // Set your local notifications
        [self setFlag];
    }

    //...   

    // Override point for customization after application launch.
    return YES;
}

- (BOOL)mustCancelNotifications
{
    UIPasteboard *past = [UIPasteboard generalPasteboard];
    NSString *s = [past valueForPasteboardType:@"my_app_local_notifications"];
    if (s) {
        return YES;
    }
    return NO;
}

- (void)setFlag
{
    UIPasteboard *past = [UIPasteboard generalPasteboard];
    [past setValue:@"dummy" forPasteboardType:@"my_app_local_notifications"];
}

- (void)removeFlag
{
    UIPasteboard *past = [UIPasteboard generalPasteboard];
    [past setData:nil forPasteboardType:@"my_app_local_notifications"];
}
arturdev
  • 10,884
  • 2
  • 39
  • 67
-7

Go to your appDelegate.m, in function «didFinishLaunchingWithOptions» add this code:

application.applicationIconBadgeNumber = 0;
Solidus
  • 251
  • 2
  • 10