2013-04-09 3 views
0

내가 잘못된 것을 찾았지만 찾을 수있는 예제가 있는지 확실하지 않습니다. 나는 그 작은 helloworld를 썼다.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

당신이 전체 샘플을 추가 할 수 있습니다를, 난을 잘 모르겠어요 그 줄을 어디에 둘 것인지 이해하십시오. –

+0

큰 감사드립니다. 흥미로운 것은 대리자를 설정하지 않고이 메서드를 구현하면 알림 센터에 알림이 표시되지만 표시되지 않는다는 것입니다. – Ants

+0

@Ants Python에서 델리게이트를 설정하는 방법 – imp

1

덮어 쓰기 - (하지만 파이썬 & PyObjC에서이 작업을 수행하는 방법에 대한 답변을 찾는 사람들이있을 수 있습니다 나는 여기에 올바른 구문을 게시 할 예정입니다 그것은 최고의 Google 검색 결과 중 하나입니다.) imp의 주석에 대해서는 언급 할 수 없기 때문에 이것을 다른 답변으로 게시 할 것입니다.

그것은 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