【问题标题】:NSUserNotificationCenter not showing notificationsNSUserNotificationCenter 不显示通知
【发布时间】:2013-03-31 13:16:23
【问题描述】:

我不确定我是否做错了,但从我能找到的示例中。我写了那个小小的helloworld。

#import <Foundation/Foundation.h>
#import <Foundation/NSUserNotification.h>


int main (int argc, const char * argv[])
{
    NSUserNotification *userNotification = [[NSUserNotification alloc] init];
    userNotification.title = @"Some title";
    userNotification.informativeText = @"Some text";

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];

    return 0;
}

我编译它:

cc -framework Foundation -o app main.m

它可以编译,我可以执行它,但它不会显示任何内容。由于某些原因,没有呈现任何内容。我读到如果应用程序是主要参与者,可能不会显示通知。如何让它显示通知?

【问题讨论】:

    标签: nsusernotification nsusernotificationcenter


    【解决方案1】:

    设置委托并覆盖

    - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center 
                                   shouldPresentNotification:(NSUserNotification *)notification {
      return YES;
    }
    

    【讨论】:

    • 您能否添加一个完整的示例,我不确定该放在哪里。
    • 非常感谢。有趣的是,如果你不设置委托并实现这个方法,通知在通知中心是可见的,但不会显示出来。
    • @Ants 如何在python中设置委托
    【解决方案2】:

    Objective-C 和 Swift 的完整示例:

    我们需要提供一个NSUserNotificationCenterDelegate,在最简单的情况下我们可以使用AppDelegate

    目标-C

    我们需要修改AppDelegate提供NSUserNotificationCenterDelegate方法。来自一个干净的AppDelegate(看起来像这样......)

    @interface AppDelegate ()
    

    我们将其修改为:

    @interface AppDelegate () <NSUserNotificationCenterDelegate>
    

    现在我们可以在@implementation AppDelegate中提供所需的委托方法

    - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center 
                                   shouldPresentNotification:(NSUserNotification *)notification {
      return YES;
    }
    

    斯威夫特 4

    您可以使用extensionAppDelegate 来提供NSUserNotificationCenterDelegate 方法:

    extension AppDelegate: NSUserNotificationCenterDelegate {
        func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
            return true
        }
    }
    

    【讨论】:

      【解决方案3】:

      确保为任何新通知分配一个新标识符。通知只会显示一次,除非使用了新的标识符或用户清除了他们的通知历史记录。

      【讨论】:

        【解决方案4】:

        我知道这是一个老问题 - 但由于可能有人在寻找如何在 Python 和 PyObjC 中执行此操作的答案,我将在此处发布正确的语法(因为它是谷歌搜索结果中最热门的结果之一)。由于我无法评论小鬼的评论,我将把它作为另一个答案发布。

        在 Python 中可以通过这种方式使用 pyobjc 执行相同的操作:

        def userNotificationCenter_shouldPresentNotification_(self, center, notification):
            return True
        

        完整的课程如下所示:

        from Cocoa import NSObject
        import objc
        
        class MountainLionNotification(NSObject):
            """ MacOS Notifications """
            # Based on http://stackoverflow.com/questions/12202983/working-with-mountain-lions-notification-center-using-pyobjc
        
            def userNotificationCenter_shouldPresentNotification_(self, center, notification):
                return True
        
            def init(self):
        
                self = super(MountainLionNotification, self).init()
                if self is None: return None
        
                # Get objc references to the classes we need.
                self.NSUserNotification = objc.lookUpClass('NSUserNotification')
                self.NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
        
                return self
        
            def clearNotifications(self):
                """Clear any displayed alerts we have posted. Requires Mavericks."""
        
                NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
                NSUserNotificationCenter.defaultUserNotificationCenter().removeAllDeliveredNotifications()
        
            def notify(self, title="", subtitle="", text="", url=""):
                """Create a user notification and display it."""
        
                notification = self.NSUserNotification.alloc().init()
                notification.setTitle_(str(title))
                notification.setSubtitle_(str(subtitle))
                notification.setInformativeText_(str(text))
                # notification.setSoundName_("NSUserNotificationDefaultSoundName")
                notification.setHasActionButton_(False)
                notification.setActionButtonTitle_("View")
                # notification.setUserInfo_({"action":"open_url", "value": url})
        
                self.NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self)
                self.NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
        
                # Note that the notification center saves a *copy* of our object.
                return notification
        

        【讨论】:

          猜你喜欢
          • 2012-08-20
          • 2013-03-12
          • 1970-01-01
          • 2020-07-13
          • 2018-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多