2012-07-23 86 views
0

我有以下問題:停止本地通知

  • 當最小化按Home鍵應用背景,創造彈出每5分鐘一個本地通知。
  • 從後臺移除應用程序。 - >我的預期然後pup-up只在應用程序存在時顯示,當從後臺移除應用程序時它被丟棄。

我的問題是,本地通知仍然處於活動狀態,並且在將它從後臺移除後每隔5分鐘仍顯示彈出窗口。

我該如何阻止它? 請幫幫我! 謝謝先進。

+0

爲什麼當你只希望當你的應用程序運行時出現本地通知?本地通知的重點在於它們出現在指定的時間,無論您的應用程序是否在運行。 – 2012-07-23 09:04:07

+0

當我想從背景彈出時,你有任何建議嗎?在我的示例中,它只有5分鐘,可能是10分鐘或20分鐘。我們也知道在後臺使用應用程序時對定時器的限制。 – 2012-07-23 09:26:17

+0

我很抱歉,但我認爲你正試圖解決錯誤的問題。通常,用戶不知道哪些應用程序在內存中,哪些已被終止。僅僅因爲應用程序被列在「任務管理器」中並不意味着它正在運行或者在內存中。我說的是,你不應該試圖根據應用程序是否在內存中來調整行爲。對用戶來說,背景(在內存中)和殺死的應該看起來完全一樣。 – 2012-07-23 10:03:42

回答

2

把它放在應用程序委託中。它會在應用程序進入後臺時刪除所有本地通知。

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
} 
+0

我也在使用[[UIApplication sharedApplication] cancelAllLocalNotifications];在applicationDidEnterBackground和didFinishLaunchingWithOptions中,但它仍然不起作用。 – 2012-07-23 07:39:13

+0

對不起我的壞。更新了問題。 – 2012-07-23 07:52:33

1

如果您不想取消所有通知...我已經設置了存儲在通知的userInfo字典中的唯一標識符。當我想刪除時,我通過所有通知快速枚舉並選擇正確的刪除。

我在這裏的絆腳石是記住存儲我爲通知創建的UUID,還記得在快速枚舉中使用isEqualToString。我想我也可以使用特定的名稱字符串,而不是唯一的標識符。如果任何人都可以讓我知道比快速列舉更好的方法,請讓我知道。

@interface myApp() { 
    NSString *storedUUIDString; 
} 

- (void)viewDidLoad { 
    // create a unique identifier - place this anywhere but don't forget it! You need it to identify the local notification later 
    storedUUIDString = [self createUUID]; // see method lower down 
} 

// Create the local notification 
- (void)createLocalNotification { 
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    if (localNotif == nil) return; 
    localNotif.fireDate = [self.timerPrototype fireDate]; 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotif.alertBody = @"Hello world"; 
    localNotif.alertAction = @"View"; // Set the action button 
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:storedUUIDString forKey:@"UUID"]; 
    localNotif.userInfo = infoDict; 

    // Schedule the notification and start the timer 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
} 

// Delete the specific local notification 
- (void) deleteLocalNotification { 
// Fast enumerate to pick out the local notification with the correct UUID 
    for (UILocalNotification *localNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {   
    if ([[localNotification.userInfo valueForKey:@"UUID"] isEqualToString: storedUUIDString]) { 
     [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system    
     } 
    } 
} 

// Create a unique identifier to allow the local notification to be identified 
- (NSString *)createUUID { 
    CFUUIDRef theUUID = CFUUIDCreate(NULL); 
    CFStringRef string = CFUUIDCreateString(NULL, theUUID); 
    CFRelease(theUUID); 
    return (__bridge NSString *)string; 
} 

以上大部分內容可能在過去的6個月內從StackOverflow中解除。希望這會有所幫助

+0

我認爲你的方式工作,如果應用程序仍然活着(專注或最小化)。但是,創建通知時會發生什麼 - >最小化 - >從後臺刪除應用程序。你的應用程序是否仍然顯示通知? – 2012-07-23 09:31:27

+0

對不起,這似乎不可能截至2011年3月:linkhttp://stackoverflow.com/questions/5398191/cancelalllocalnotifications-in-applicationwillterminate – Steve 2012-07-23 09:45:10

+0

- 現在可能會有所不同。解決方法建議:限制通知的數量而不是不斷重複,確保用戶在這些計劃通知期間的某個時間點返回到應用程序,並且如果通知要繼續重置它們,那麼。恐怕結束了我的經驗:)現在可能會有所不同。解決方法建議:限制通知的數量而不是不斷重複,確保用戶在這些計劃通知期間的某個時間點返回到應用程序,並且如果通知要繼續重置它們,那麼。恐怕結束了我的經驗:) – Steve 2012-07-23 10:02:51