Detect Other Button click (NSUserNotification)

天涯浪子 提交于 2019-12-01 04:20:29

问题


I've searched all over the internet but the only method found was by looping through

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification

just as shown in: https://stackoverflow.com/a/21365269/1795083

However, my app sometimes displays multiple notifications at a short interval, so this isn't really efficient nor effective. How does the mail app handle it when the trash button is pressed. There has to be at least a private method that's called. Has anyone ever came across it? Thanks in advance


回答1:


After... uhm.... "checking" out the mail app's code... I found a suspicious function in their notification controller class...

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDismissAlert:(NSUserNotification *)alert

I have no idea why apple don't want to add this function to the NSUserNotificationDelegate... but I tested this out in my app and it works!!!




回答2:


Swift >= 4 version:

@objc
func userNotificationCenter(_ center: NSUserNotificationCenter, didDismissAlert notification: NSUserNotification)

Make sure you add the @objc annotation, otherwise the objective-C runtime won't be able to see your delegate method.



来源:https://stackoverflow.com/questions/38192389/detect-other-button-click-nsusernotification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!