从10.9开始,以下方法删除任何显示的通知:
// Clear a delivered notification from the notification center. If the // notification is not in the delivered list, nothing happens. - (void)removeDeliveredNotification:(NSUserNotification *)notification; // Clear all delivered notifications for this application from the // notification center. - (void)removeAllDeliveredNotifications;
自10.8以来,行为似乎已经改变,因为在调用这些方法时也会删除任何显示的通知(感谢@ 0xced以进行说明)。
removeDeliveredNotification 正在删除显示的通知(10.11),警告是 identifier 必须设置通知。
removeDeliveredNotification
identifier
Messages应用程序可能正在使用私有NSUserNotificationCenter _removeAllDisplayedNotifications 要么 _removeDisplayedNotification: 方法。
_removeAllDisplayedNotifications
_removeDisplayedNotification:
您可以尝试使用这些方法来测试这是否是您正在寻找的。只需添加此类别接口即可声明方法:
@interface NSUserNotificationCenter (Private) - (void)_removeAllDisplayedNotifications; - (void)_removeDisplayedNotification:(NSUserNotification *)notification; @end
不幸的是,由于这些是未记录的方法,因此您无法在通过App Store分发的应用程序中使用它们。如果这确实是你想要的,那么你应该 提交一个错误 并要求这些方法成为公共API的一部分。