2012-02-14 113 views
29

假設我爲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

回答

31

正如其他人所提到的,我想做到這一點的最好辦法是有一個標誌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

-7

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

application.applicationIconBadgeNumber = 0; 
+1

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

0

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

這裏就是我所做的:

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

希望它能幫助!

3

對於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

相關問題