2016-04-15 48 views
0

我需要以編程方式清除已通過通知中心顯示的本地通知。繼this link之後,我實現了建議的解決方案,但問題是該示例中的eventArray始終有0個元素。當我向下滑動顯示通知中心時,我會看到之前創建的4個通知。所以在這種情況下,我期望這個數組有4個元素,但它有0個。任何想法爲什麼這樣?我已經嘗試過iOS 8.3和9.2.1,並且它們都是0。以編程方式清除已顯示的本地通知

的iOS有2種方式呈現局部的通知:

  1. 從通知中心:
    • 你不能滑開的通知,由左到右。
    • 您可以從右向左滑動通知(從列表中刪除單個通知)。
    • 您可以點擊通知,之後您的應用程序將啓動,並通知將從通知中心(由iOS系統的處理)
  2. 從鎖定屏幕上消失:
    • 僅當您啓用此設置從iPhone/iPad設置:http://www.imore.com/how-disable-notification-center-lock-screen-your-iphone-and-ipad
    • 您可以從左向右滑動通知(您的應用程序將啓動,由iOS處理)。 在這種情況下,通知中心的通知不會被刪除(iOS不會刪除它,並且不允許在通知已經向用戶通知中心呈現通知後從代碼中刪除單個通知)
    • 您可以從右向左滑動通知(從列表中刪除單個通知,由iOS處理。通知中心通知也會被刪除,由iOS處理)。
    • 您無法點擊通知。

編輯: 這裏是代碼示例中,我是如何做到了:

UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.alertBody = @"1"; 
localNotification.alertTitle = @"1"; 
localNotification.userInfo = uniqueDictIdentifier1; 
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; 

UILocalNotification *localNotification2 = [[UILocalNotification alloc] init]; 
localNotification2.alertBody = @"2"; 
localNotification2.alertTitle = @"2"; 
localNotification2.userInfo = uniqueDictIdentifier2; 
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification2]; 

.... 
//2 more notifications are created like this 

再有就是代碼用於過濾所有通知:

NSArray *eventArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
for (int i=0; i<[eventArray count]; i++) { 
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i]; 
    NSDictionary *userInfoCurrent = oneEvent.userInfo; 
    if ([userInfoCurrent isEqualToDictionary:uniqueDictIdentifier1]) { 
     [[UIApplication sharedApplication] cancelLocalNotification:oneEvent]; 
     break; 
    } 
} 

回答

0

用於保存通知一個唯一的ID

NSDictionary * infoDict = @{ @"alarmUiqueId" : uID, 

            }; 
    NSLog(@"%@",infoDict); 
    NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSCalendarUnitSecond 
                  fromDate:fireDate]; 
    fireDate = [fireDate dateByAddingTimeInterval:-comp.second]; 
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    localNotif.fireDate = fireDate; 
    localNotif.timeZone = [NSTimeZone localTimeZone]; 
    localNotif.alertBody = desString; 
    localNotif.userInfo = infoDict; 
    localNotif.repeatInterval = NSCalendarUnitDay; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif] 

並刪除一個paritcular通知寫這段代碼。

NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 

    for(UILocalNotification *notification in notificationArray) 
    { 
     NSLog(@"%@",[notification.userInfo valueForKey:@"alarmUiqueId"]); 
     if ([[notification.userInfo valueForKey:@"alarmUiqueId"] isEqualToNumber: health.uniqueId]) 
     { 
      [[UIApplication sharedApplication] cancelLocalNotification:notification] ; 
     } 
    } 
+0

我編輯了我的問題來進一步解釋問題是什麼。粗體文本解釋了我需要刪除單個通知的情況。鏈接的stackoverflow問題有刪除單個通知的代碼。 –

+0

你想刪除一個特定的通知? –

+0

是的,我想刪除已經顯示在通知中心的單個通知。我剛剛下載了Gmail應用,並確認它與我的應用具有相同的問題。所以我想這是一個iOS限制,而不是我的應用程序中的一個錯誤。 –

0

我的理解是如下: 你不能scheduledNotifications已經得到通知LocalNotification的信息。

我的解決方案是隻在應用程序中將UILocalNotification實例保存在您的單例對象中,並且在您想要從通知中心刪除時使用實例調用cancelLocalNotification

這可能是你的幫忙嗎?