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

查看:22
本文介绍了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:

我在互联网上搜索过这个,也浏览了通知中心的文档this这个也是如此.

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天全站免登陆