我正尝试在我的应用程序中使用新的Mountain Lion NSUserNotificationCenter (实际上这并不太难)。发布通知就像一种护身符,通过
NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.title = @"Some title";
userNotification.informativeText = @"Some text";
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];然而,一旦应用程序获得焦点,我想要关闭屏幕上的所有通知。例如,新的消息应用程序就是这样做的。当在后台接收到新消息时,将显示通知。当应用程序再次激活时,这些应用程序将自动关闭,并从屏幕和通知中心消失。
为了复制这一点,我在NSApplicationDidBecomeActiveNotification通知中注册了一个方法,该方法也会被成功调用。在那里我调用[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications]。
然而,这样做的效果是,已在通知中心收集的通知将被删除,而显示在右上角的相应“气泡”仍将显示。
迭代所有传递的通知并自行删除它们具有完全相同的效果,就像使用scheduleNotification而不是deliverNotification一样。
我是唯一经历过这种情况的人吗,或者我是否在编程上遗漏了一些东西,可以忽略通知的屏幕部分和通知中心部分?
发布于 2012-10-04 07:19:20
Messages应用程序可能正在使用私有NSUserNotificationCenter _removeAllDisplayedNotifications或_removeDisplayedNotification:方法。
您可以尝试使用这些方法来测试这是否是您正在寻找的。只需添加此类别接口来声明方法:
@interface NSUserNotificationCenter (Private)
- (void)_removeAllDisplayedNotifications;
- (void)_removeDisplayedNotification:(NSUserNotification *)notification;
@end不幸的是,由于这些都是未记录的方法,您不能在通过app Store分发的应用程序中使用它们。如果这确实是您正在寻找的,那么您应该file a bug并要求这些方法成为公共API的一部分。
发布于 2013-11-27 15:24:51
从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澄清)。
发布于 2016-01-23 06:44:39
removeDeliveredNotification正在删除为我显示的通知(在10.11),警告是通知上的identifier必须设置。
https://stackoverflow.com/questions/12113300
复制相似问题