2013-04-09 82 views
0

我不确定自己是否遇到了问题,但是可以找到我可以找到的示例。我写了那个小小的世界。NSUserNotificationCenter未显示通知

#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 

它编译,我可以执行它,但它不会显示任何东西。由于某些原因,什么都不会呈现。我读过如果应用程序是主要演员,通知可能不会显示。我怎样才能让它显示通知?

回答

4

设置为代表,并覆盖

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

您可以加入一个完整的样本,我不知道到明白该放哪一行。 – 2013-05-19 09:10:35

+0

非常感谢。有趣的是,如果您不设置委托并实施此方法,通知在通知中心中可见,但不会显示。 – Ants 2013-06-26 02:48:43

+0

@Ants如何在python中设置委托 – imp 2013-11-18 04:42:58

1

我知道这是一个老问题 - 但可能还有人在寻找一个答案如何在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