,我相信我有一個類似的問題。當應用程序進入前臺時,我試圖清除過去的通知,以從通知托盤中刪除任何舊通知。
我做了這樣的事情搶老的通知並刪除它們:
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSArray *pastNotifications = [activeNotifications filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"firDate < %@", [NSDate date]]];
for (UILocalNotification *notification in pastNotifications) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
然而,似乎scheduledLocalNotifications
不包括其大火日期已經過去,即使他們仍然會出現在通知中心位置。
調用cancelAllLocalNotifications
似乎也刪除過去的通知。因此,我們可以抓取所有當前通知,取消所有內容,然後添加我們仍然感興趣的內容。
// Grab all the current notifications
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
// Clear all notifications to remove old notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Add back the still relevant notifications
for (UILocalNotification *notification in activeNotifications) {
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
此外,我們還可以做的通知的一些過濾加入他們回來,如果一些不再需要之前,我們可以抓住主動通告,當應用程序被激活,將它們存儲在一個實例變量,只有加他們回來時,應用程序移動到後臺
你的意思是通知中心? –
我是指在應用程序外部顯示的通知托盤! –