如何从iPhone中删除应用程序时删除所有本地通知

问题描述:

假设我为iPhone应用程序设置了5个本地通知,则用户将删除该应用程序。如果应用程序再次安装,它会显示以前的通知。如何从iPhone中删除应用程序时删除所有本地通知

我知道下面的代码删除所有通知

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

但我在哪里把这些代码,这样,当应用程序被删除它执行?

或者任何其他方式来解决这个问题。

+4

你能不能删除它第一次启动呢?例如,如果UserSettings中的某个标志未设置,则全部取消它们并设置标志? – 2012-02-14 05:50:35

+0

在这个应用程序中,我使用数据库来存储和回收通知对象 – 2012-02-14 06:52:02

+0

Neon ..这是否有帮助? http://stackoverflow.com/questions/12369900/when-i-delete-my-ios-application-push-notification-state-remains – pordi 2012-10-29 17:55:56

正如其他人所提到的,我想做到这一点的最好办法是有一个标志didFinishLaunchingWithOptions在应用程序委托,检查它是否是第一次启动。

如果应用程序正在启动的第一次,你可以调用

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

这将取消现有的任何通知。

例如:

- (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

如果应用程序被删除,不会自动删除本地通知吗?我不明白为什么他们应该恢复一个新的应用程序安装? – Supertecnoboff 2016-03-28 17:11:11

+1

我相信在应用程序重新安装的情况下,它们会保留一段时间。如果我能找到它,我会发布文档。 – DerekH 2016-03-28 22:47:31

+0

删除时没有通知未被清除。我创建了一个应用程序,并安排了一些本地通知。然后,我删除了应用程序并重新安装,但通知不断发出(这次未实现本地通知)。这就是为什么我认为他们被保留,但我不确定这(保留)持续时间 – 2017-11-15 22:10:22

转到您的appDelegate.m,在功能«didFinishLaunchingWithOptions»添加以下代码:

application.applicationIconBadgeNumber = 0; 
+1

我不想删除本地通知,而不是徽章。 – 2012-11-05 04:47:24

您可以使用UIPasteboard存储已经提到的一个标志作为DerekH。
东西此链接:

- (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"]; 
} 

这里就是我所做的:

在你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; 
} 

希望它能帮助!

对于SWIFT 3.0,你可以使用

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

再加入此方法来检查,如果这是第一次运行

var isFirstLaunch: Bool { 
    get { 
     if  (NSUserDefaults.standardUserDefaults().objectForKey("firstLaunchDate") == nil) { 
      NSUserDefaults.standardUserDefaults().setObject(NSDate(),  forKey: "firstLaunchDate") 
      NSUserDefaults.standardUserDefaults().synchronize() 
      return true 
     } 
     return false 
    } 
} 
+0

请注意,此代码适用于> = iOS 10 – 2017-11-08 18:28:33