【问题标题】:How to delete all local notifications when an application is deleted from an iPhone从 iPhone 中删除应用程序时如何删除所有本地通知
【发布时间】:2012-03-05 12:55:43
【问题描述】:

假设我为 iPhone 应用程序设置了 5 个本地通知,然后用户删除了该应用程序。如果再次安装该应用程序,它会显示以前的通知。

我知道以下代码会删除所有通知

[[UIApplication sharedApplication] cancelAllLocalNotifications];

但是我应该把代码放在哪里,以便在删除应用程序时执行它?

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

【问题讨论】:

  • 你不能在第一次启动时删除它吗?例如,如果 UserSettings 中的某个标志未设置,您将它们全部取消并设置标志?
  • 在这个应用程序中,我也使用数据库来存储和检索通知对象
  • 霓虹灯.. 这有帮助吗? stackoverflow.com/questions/12369900/…

标签: objective-c iphone xcode uilocalnotification


【解决方案1】:

转到您的 appDelegate.m,在函数 «didFinishLaunchingWithOptions» 中添加以下代码:

application.applicationIconBadgeNumber = 0;

【讨论】:

  • 我不想删除本地通知而不是徽章。
【解决方案2】:

正如其他人所提到的,我认为实现此目的的最佳方法是在应用程序委托的 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;
    }
{

【讨论】:

  • 如果删除应用程序,本地通知不会自动删除吗?我不明白为什么他们应该恢复全新的应用安装?
  • 我相信它们会保留一段时间,以防重新安装应用程序。如果我能找到它,我会发布文档。
  • 没有删除通知不会被清除。我创建了一个应用程序并安排了一些本地通知。然后我删除了该应用程序并再次安装,但通知不断出现(这次没有实现本地通知)。这就是为什么我相信它们会被保留,但我不确定这个(保留)持续时间
【解决方案3】:

您可以使用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"];
}

【讨论】:

    【解决方案4】:

    这就是我所做的:

    在您的 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;
    }
    

    希望对你有帮助!

    【讨论】:

      【解决方案5】:

      对于 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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-05-10
        • 2011-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-13
        • 1970-01-01
        • 2015-04-24
        相关资源
        最近更新 更多