1

根据苹果文档https://developer.apple.com/reference/foundation/nsusernotification/1416410-identifier

NSUserNotification 有一个名为 identifier 的属性,它假设当标识符与另一个通知相同时替换通知。

但是,当我测试此功能时,通知似乎并没有真正被替换,只是没有发送。

如何实现通知中心仅存在一种通知但最新调用的通知更新到顶部的效果?

延迟 1 分钟发送通知 A + 通知 B + 通知 A

这是在 mac 通知中心显示的内容

无标识符

NotificationA (now) 
NotificationB (1 minute ago) 
NotificationA (2 minute ago)

带标识符

NotificationB (1 minute ago) 
NotificationA (2 minute ago)

请注意,由于存在标识符,如何未调用通知 A(第二次)

想要的效果

NotificationA (now) 
NotificationB (1 minute ago)

在这种情况下,通知 A 再次发送,之前的通知 A 消失了

4

1 回答 1

2

notification您可以使用NSNotificationCentersremoveDeliveredNotification:方法删除现有的。只需删除并重新添加您的通知。

Objective-C

[[NSUserNotificationCenter defaultUserNotificationCenter] removeDeliveredNotification:userNotification];
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];  

迅速

NSUserNotificationCenter.default.removeDeliveredNotification(userNotification)
NSUserNotificationCenter.default.deliver(userNotification)

我成功地使用了这种技术来显示通知而不污染通知中心。

于 2016-07-20T09:43:41.923 回答