2016-11-03 3 views
5

Я хочу удалить все предыдущие локальные уведомления из NotificationCenter при добавлении новых уведомлений. Но он работает в iOS9.0 и более низкой версии, но в iOS 10 он запускает несколько локальных уведомлений. Так что cancelAllLocalNotifications не очищает уведомления.cancelAllLocalNotifications не работает в iOS10

Код успешно скомпилирован в iOS10.

UIApplication.shared.cancelAllLocalNotifications() 
+0

для уведомлений iOS10, что вы используете? используете ли вы систему «UserNotifications»? – Wolverine

+0

Да, я использую структуру UNUserNotifications – technerd

+0

Интересно, 'cancelAllLocalNotifications()' работает для меня в iOS 10.2. – kelin

ответ

17

Для прошивки 10, Swift 3,0

cancelAllLocalNotifications осуждается от прошивки 10.

@available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]") 
open func cancelAllLocalNotifications() 

Вы должны добавить оператор импорта,

import UserNotifications 

Получить центр уведомлений. И выполните операцию, как показано ниже

let center = UNUserNotificationCenter.current() 
center.removeAllDeliveredNotifications() // To remove all delivered notifications 
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled. 

Если вы хотите удалить одно или несколько конкретных уведомлений, вы можете получить его методом ниже.

center.removeDeliveredNotifications(withIdentifiers: ["your notification identifier"]) 

Надеюсь, это поможет .. !!

+0

Я реализовал то же самое. Спасибо – technerd

4

Для прошивки 10, Objective C:

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
[center removeAllDeliveredNotifications]; 
[center removeAllPendingNotificationRequests]; 
+2

, если вы получите следующую ошибку [UNUserNotificationCenter '; вы имели в виду «NSNotificationCenter»?], пожалуйста, импортируйте UserNotifications в исходный файл, например: #import Delorean

-1

Для прошивки 10, Вы можете использовать этот способ для удаления все Local Notification. Я только что протестировал, он работает.

NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ; 
    for (UILocalNotification *localNotification in arrayOfLocalNotifications) { 
     [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; 
    } 
Смежные вопросы