我一直在计划和删除通知的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)