Mac OS X NSUserNotificationCenter通知获取关闭事件/回调 [英] Mac OS X NSUserNotificationCenter notification get dismiss event/callback

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

问题描述

在我们的应用中,我们以警报样式显示通知中心通知.

In our app we are displaying Notification Center notifications in alert style.

显示通知效果很好,当用户通过单击通知或单击操作"按钮与通知进行交互时,我们也会得到回调.

Displaying notification works fine, as well as we get callback when user interacts with the notification either by clicking on notification or by clicking on Action button.

但是,我们有兴趣在用户单击通知中的其他"按钮时获得回调或事件.我已经看到MAC OS在显示其可用更新对话框时执行此操作.

However, we are interested in getting a callback or event when user clicks on Other button in notification. I have seen MAC OS does this when it displays its updates available dialog.

有关该OS X更新可用警报的说明,请参考此图像:

Refer to this image for clarification about OS X update available alert:

我已经在Internet上进行了搜索,还浏览了通知中心的文档

I have searched this over the internet, as well as gone through Notification Center's documentation this and this as well.

是否有未公开的API?还是一些用于检测单击其他(关闭)按钮的自定义机制?

Is there any undocumented API? or some custom mechanism for detecting click on Other (close) button?

推荐答案

尽管另一个(关闭)按钮显然是用来关闭该通知的,但无论其自定义标题可能指示什么,都没有一种优雅的方法来在何时发出通知用户可以通过单击关闭按钮来关闭通知.

While the other (close) button is clearly meant to dismiss the notification, regardless of what its custom caption may indicate, there is no elegant way to get notified when the user dismisses the notification by clicking on the close button.

但是,您可以做的是监视默认用户通知中心的 deliveredNotifications 属性:只要尚未关闭通知,数组将包含该通知.取消通知后,数组将不再包含该通知.

What you could do, however, is to monitor the default user notification center's deliveredNotifications property: As long as the notification has not yet been dismissed, the array will contain the notification. Once the notification has been dismissed, the array will not contain it anymore.

这可以在NSUserNotificationCenter委托方法中实现,如下所示:

This could be implemented in a NSUserNotificationCenter delegate method like this:

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                   ^{
                       BOOL notificationStillPresent;
                       do {
                           notificationStillPresent = NO;
                           for (NSUserNotification *nox in [[NSUserNotificationCenter defaultUserNotificationCenter] deliveredNotifications]) {
                               if ([nox.identifier isEqualToString:notification.identifier]) notificationStillPresent = YES;
                           }
                           if (notificationStillPresent) [NSThread sleepForTimeInterval:0.20f];
                       } while (notificationStillPresent);
                       dispatch_async(dispatch_get_main_queue(), ^{
                           [self notificationHandlerForNotification:notification];
                       });
                   });
}

此代码将每200毫秒检查通知是否仍然存在.一旦消失,将在主线程上调用-notificationHandler:方法,这只是一个任意的回调方法.

This code will check if the notification is still there every 200 milliseconds. Once it is gone, the -notificationHandler: method will be called on the main thread, which is just an arbitrary callback method.

在此自定义-notificationHandler:方法中,您可以检查是否已为通知调用了NSUserNotificationCenter的didActivateNotification:委托方法.如果没有,则用户很可能单击了通知的关闭按钮.

In this custom -notificationHandler: method you could check whether NSUserNotificationCenter's didActivateNotification: delegate method has been called for the notification. If it hasn't, the user most likely clicked on the close button of the notification.

这不是安全的,因为用户可能还以其他方式取消了该通知,即未单击关闭按钮.

This is not failsafe, though, as the user may also have otherwise dismissed the notification, i.e. without clicking on the close button.

这篇关于Mac OS X NSUserNotificationCenter通知获取关闭事件/回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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