问题描述
基本上,我只是想打印我的应用程序已发送的通知,但是要做的是:
Basically, I'm simply trying to print the notifications that my app has delivered, but doing something like:
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
print(notifications)
}
即使通知中心包含多个已传递的通知,
也只会在Xcode控制台中显示一个空数组.( print(notifications.count)
返回0)
just displays an empty array in the Xcode console, even when the notification center contains several delivered notifications. (print(notifications.count)
returns 0)
我可以使用 getPendingNotificationRequests
成功地获取已调度但尚未发送的通知,进行更新,甚至使用 removePendingNotificationRequests(withIdentifiers:)
删除它们.但是,任何试图访问/修改/删除已交付内容的尝试均不起作用.
I can use getPendingNotificationRequests
to successfully grab the notifications that are scheduled yet undelivered, update them, and even remove them with removePendingNotificationRequests(withIdentifiers:)
. But any attempt to access/modify/remove anything that's been delivered does not work.
我在这里错过了一些简单的东西吗?iOS 10+访问传递的通知的方式最近是否有所改变?
Am I missing something simple here? Did something change recently with the way iOS 10+ accesses the delivered notifications?
推荐答案
我确实遇到了与 getDeliveredNotification
相同的问题,通知数组始终为空.
I did face the same problem with getDeliveredNotification
, the array of notifications were always empty.
因此,我意识到我正在将 [UIApplication sharedApplication] .applicationBadgeNumber
设置为 0
,从而清除了来自通知中心的所有远程通知.
So, I realized that I was setting [UIApplication sharedApplication].applicationBadgeNumber
to 0
, what clear all remote notifications from notification center.
现在,仅在收到所有待处理的通知后,我才将 applicationBadgeNumber
设置为 0
.
Now I set applicationBadgeNumber
to 0
only after I get all pending notifications.
这篇关于UNUserNotificationCenter.current().getDeliveredNotifications仅返回一个空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!