2012-08-05 5 views

답변

148

Mountain Lion의 알림은 두 가지 클래스로 처리됩니다. NSUserNotificationNSUserNotificationCenter. NSUserNotification은 귀하의 실제 알림이며, 속성을 통해 설정할 수있는 제목, 메시지 등이 있습니다. 작성한 알림을 전달하려면 NSUserNotificationCenter에서 사용할 수있는 deliverNotification: 메소드를 사용할 수 있습니다. 애플의 워드 프로세서 NSUserNotification & NSUserNotificationCenter에 대한 정보를 자세히 설명했지만 알림을 게시 할 기본 코드는 다음과 같습니다 : 그건 제목 알림을 생성합니다

- (IBAction)showNotification:(id)sender{ 
    NSUserNotification *notification = [[NSUserNotification alloc] init]; 
    notification.title = @"Hello, World!"; 
    notification.informativeText = @"A notification"; 
    notification.soundName = NSUserNotificationDefaultSoundName; 

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; 
} 

메시지와 그 때의 기본 사운드를 재생합니다 표시됩니다. 이 기능을 사용하는 것보다 알림을 사용하여 할 수있는 작업이 훨씬 많습니다 (예 : 알림 예약). 그 작업은 모두 내가 링크 된 설명서에 자세히 설명되어 있습니다.

하나의 작은 점으로, 응용 프로그램이 핵심 응용 프로그램 일 때만 알림이 표시됩니다. 응용 프로그램이 중요한지 여부에 관계없이 알림을 표시하려면 NSUserNotificationCenter에 대한 대리자를 지정하고 위임 메서드 userNotificationCenter:shouldPresentNotification:을 재정 의하여 YES를 반환해야합니다. NSUserNotificationCenterDelegate에 대한 설명서는 다음에서 찾을 수 있습니다 here

다음은 NSUserNotificationCenter에 대리인을 제공 한 다음 응용 프로그램이 중요한지 여부에 관계없이 강제로 알림을 표시하는 예입니다. 응용 프로그램의 AppDelegate.m 파일에서 다음과 같이 편집 :

@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate> 
+0

당신이 userNotificationCenter를 오버라이드 (override)하는 방법에 대한 정교한 :

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; } - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{ return YES; } 

그리고 AppDelegate.h에서

는, 클래스가 NSUserNotificationCenterDelegate 프로토콜을 준수 함을 선언합니다 ? (죄송합니다. 정말 새로워 :) :) – haseo98

+3

@ haseo98 그래, 방금 예를 들어 답을 추가했습니다. – alexjohnj

+0

Im 메서드의 applicationdidfinishlaunching 섹션 옆에 오류가 발생합니다. 호환되지 않는 'id '매개 변수에 'AppDelegate * const __strong'을 보냅니다. 어떤 아이디어? – haseo98

관련 문제