NSUserNotificationCenter关闭通知 [英] NSUserNotificationCenter dismiss notification

查看:632
本文介绍了NSUserNotificationCenter关闭通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的应用程序使用新的Mountain Lion NSUserNotificationCenter(实际上并不太难)。发布通知的作用类似于charm通过

  NSUserNotification * userNotification = [[NSUserNotification alloc] init]; 
userNotification.title = @一些标题;
userNotification.informativeText = @Some text;

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];

但是,我想在应用获得焦点后关闭屏幕上的所有通知。例如。像新的Messages应用程序。当在后台接收到新消息时,显示通知。当应用再次变为活动状态时,这些应用会自动关闭,并从屏幕和通知中心消失。



要复制这个,我已经注册了一个方法到 NSApplicationDidBecomeActiveNotification 通知也被成功调用。在这里我调用 [NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications]



已被收集在通知中心中的图像被移除,而仍然显示在右上角显示的相应的气泡。



如果使用 scheduleNotification 来替代所有已传送的通知并自行移除通知,效果完全相同 deliverNotification



我是唯一遇到此问题的人,部分和通知中心部分?

解决方案

Messages应用程序可能使用私有NSUserNotificationCenter _removeAllDisplayedNotifications _removeDisplayedNotification:方法。



您可以尝试使用这些方法测试这是否是你要找的。只需添加此类别界面来声明方法:

  @interface NSUserNotificationCenter(Private)
- (void)_removeAllDisplayedNotifications;
- (void)_removeDisplayedNotification:(NSUserNotification *)notification;不幸的是,因为这些都是未记录的方法,所以你不能在一个方法中使用它们。
@end


$ b <应用程序通过App Store分发。如果这确实是您要查找的内容,那么您应该提交错误,并要求这些方法成为公共API的一部分。 / p>

I'm trying to use the new Mountain Lion NSUserNotificationCenter for my application (which isn't too hard actually). Posting notifications works like a charm via

NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.title = @"Some title";
userNotification.informativeText = @"Some text";

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];

However, i'd like to dismiss all notifications that are on the screen once the app gains focus. E.g. like the new Messages app does it. When new messages are received in the background, notifications are shown. When the app becomes active again, these are dismissed automatically and vanish from the screen and from the Notification Center.

To replicate this, I've registered a method to the NSApplicationDidBecomeActiveNotification notification which also gets called succesfully. In there I call [NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications].

This, however, has the effect that notifications that have been collected in the Notification Center are removed while the corresponding "bubbles" that are displayed in the top right corner are still displayed.

Iterating all delivered notifications and removing them each on their own has the exactly same effect, as has using scheduleNotification instead of deliverNotification.

Am I the only one experiencing this, or am I missing something to dismiss the on-screen part and the Notification Center part of a notification programatically?

解决方案

The Messages app is probably using the private NSUserNotificationCenter _removeAllDisplayedNotifications or _removeDisplayedNotification: method.

You can try to use these methods to test if this is what you are looking for. Just add this category interface to declare the methods:

@interface NSUserNotificationCenter (Private)
- (void)_removeAllDisplayedNotifications;
- (void)_removeDisplayedNotification:(NSUserNotification *)notification;
@end

Unfortunately, since these are undocumented methods, you can not use them in an app distributed through the App Store. If this is indeed what you are looking for, then you should file a bug and ask for these methods to become part of the public API.

这篇关于NSUserNotificationCenter关闭通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆