How to delete all local notifications when an application is deleted from an iPhone

前端 未结 5 1897
既然无缘
既然无缘 2021-02-03 21:18

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 kno

相关标签:
5条回答
  • 2021-02-03 21:31

    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
        }
    }
    
    0 讨论(0)
  • 2021-02-03 21: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;
        }
    {
    
    0 讨论(0)
  • 2021-02-03 21:44

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

    application.applicationIconBadgeNumber = 0;
    
    0 讨论(0)
  • 2021-02-03 21:47

    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"];
    }
    
    0 讨论(0)
  • 2021-02-03 21:48

    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!

    0 讨论(0)
提交回复
热议问题