Я работаю над проектом iOS, который планирует и удаляет уведомления.Недавно я пытался проверить, что вызов
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
удаляет нужные уведомления.Я проверил, и я передаю правильные идентификаторы, которые должны удалить уведомления.
Однако, когда я звоню
UNUserNotificationCenter.current().getPendingNotificationRequests
сразу после моего звонка на removePendingNotificationRequests
, это не показывает, что какие-либо уведомления были отменены.Есть ли способ убедиться, что асинхронный код в removePendingNotificationRequests
был вызван?Или невозможно последовательно протестировать эту функцию?
Я замечаю, что когда я звоню UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
, запросы отменяются правильно.
Соответствующий код следующий в моем XCTestCase:
UNUserNotificationCenter.current().
getPendingNotificationRequests(completionHandler: {
(notifications) in
var identifiers: [String] = []
for notification in notifications {
if notification.identifier.hasPrefix(self.getEventNotificationPrefix()) {
identifiers.append(notification.identifier)
}
}
// The correct identifiers are stored when this call is made
UNUserNotificationCenter.current()
.removePendingNotificationRequests(withIdentifiers: identifiers)
})
UNUserNotificationCenter.current().
getPendingNotificationRequests(completionHandler: { (notifications) in
// notifications.count is still 1 here, so this incorrectly fails.
XCTAssertEqual(notifications.count, 0)
rescheduleExpectation.fulfill()
})
waitForExpectations(timeout: 5, handler: nil)