如何在swift中的数组中存储挂起的通知



我正在尝试编程一个函数,该函数返回正在重复的挂起通知的所有标识符。

func getRepeatingNotificationsIds () -> [String] {

var repatingNotification:[String] = []
UNUserNotificationCenter.current().getPendingNotificationRequests {
(requests)  in
for request in requests{
if (request.trigger?.repeats == true)
{
repatingNotification.append(request.identifier)
}

}
}

return repatingNotification

}

但是,repatingNotification数组在返回时保持为空。是否可以通过引用或其他方式调用repatingNotification?

不要试图返回值,而是通过向函数发送一个完成块来告诉它在收集请求后希望它做什么。正如在评论中提到的,由于时间的原因,你所拥有的不会起作用。

这是一个微不足道的操场例子,应该会给你这个想法。

import UIKit
func getRepeatingNotificationsIds (completion: @escaping ([String])->()) {

var repatingNotification:[String] = []
UNUserNotificationCenter.current().getPendingNotificationRequests {
(requests)  in
for request in requests {
if (request.trigger?.repeats == true) {
repatingNotification.append(request.identifier)
}
}
completion(repatingNotification)
}
}
getRepeatingNotificationsIds { notifications in
print(notifications)
}

最新更新