2014년 11월 19일 수요일

[OSX] 알림센터를 이용해 알림 띄우기

이번 글은 OS X의 알림을 표시하는 방법에 대한 것이다. 알림(Notification)이라 표현했는데, 이 알림 기능은 OS X Lion 에서 추가된 OS X 알림 센터를 의미한다.


정확한 용어를 써 보자. OS X 알림은 User Notification 이라 표현한다. 그러니 알림 센터는 User Notification Center 가 되겠다. 이를 담당하는 클래스는 그 이름 그대로 NSUserNotification 과 NSUserNotificationCenter 이다. (눈에 보이지 않는 메시지를 담당하는 NSNotification 류와 혼동하지 말자)

알림 띄우기

글 상단의 스크린샷과 같은 알림을 띄우는 방법은 간단한다:
# Swift Code:
import Cocoa

let notification = NSUserNotification()
notification.title = "Notification Test"
notification.informativeText = "Hello World!"
notification.soundName = NSUserNotificationDefaultSoundName

NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)
# Objective-C Code:
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = @"Notification Test";
notification.informativeText = @"Hello World!";
notification.soundName = NSUserNotificationDefaultSoundName

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notification];
NSUserNotification 오브젝트에 알림에 필요한 정보를 넣고 NSUserNotificationCenter 를 이용해 전송(deliver) 하면 끝이다.

이렇게 하면 알림이 데스크탑 우측 상단에 잠깐 표시되었다 사라진다. 알림 센터를 열어보면 당연히 이 알림 기록이 남아있다.


알림을 클릭했을 경우

알림을 마우스로 클릭했을 경우는 NSUserNotificationCenterDelegate의 didActivateNotification 위임메소드가 호출된다. 따라서 delegate를 등록하고 해당 메소드를 구현하면 된다.
# Swift Code:
class SomeNotificationClass: NSObject, NSUserNotificationCenterDelegate {
    ...
    func someInitialization() {
        ...
        NSUserNotificationCenter.defaultCenter().delegate = self
        ...
    }
    ...
    func userNotificationCenter(center: NSUserNotificationCenter, 
                                didActivateNotification notification: NSUserNotification) {
        println("Notification Clicked!")
    }
}
# Objective-C Code:
// Definition(.h)

@interface SomeNotificationClass: NSObject <NSUserNotificationCenterDelegate>
...
@end

// Implementation(.m)

@implement SomeNotificationClass
...
- (void)someInitialization
{
    ...
    [NSUserNotificationCenter defaultCenter].delegate = self
    ...
}

...

- (void)userNotificationCenter:(NSUserNotificationCenter *)center
        didDeliverNotification:(NSUserNotification *)notification
{
    NSLog(@"Notification Clicked!");
}
위임(Delegation) 받기 원하는 클래스는 NSUserNotificationCenterDelegate 프로토콜을 이용한다고 명시하고, delegate를 해당 클래스 오브젝트를 넘긴다. 그리고 마지막의 didActivateNotification 이름이 붙어있는 위임 메소드를 구현한다.

알림 스타일

알림은 배너(Banner)와 알림(Alert) 두 가지 타입이 있다. 이 두 가지가 어떤 모양인지는 아래에 있는 시스템 환경설정의 알림 스크린샷을 참고하자.

프로젝트에서 원하는 타입을 설정하려면 Info.plist 파일을 열고 NSUserNotificationAlertStyle 이라는 Row를 생성한 뒤 값을 'banner' 혹은 'alert' 로 넣으면 된다.


그런데 이렇게 .plist 를 이용해 설정한 것이 제대로 동작하는지는 확인하지 못 했다. 더구나, Lion 방식의 알림센터는 시스템 환경설정(Preferences) 에서 별도의 설정이 가능하기 때문에 과연 의미가 있는지를 잘 모르겠다.
[시스템 환경설정 - 알림] 에 들어가보면 위와 같이 자동으로 알림센터를 앱이 등록된다. 여기서 원하는 스타일로 변경 할 수 있다. 위의 경우 Swift 플레이그라운드 파일로 등록했더니 이름이 저렇게 등록되는데 원래는 앱 이름이 등록된다.

마무리

여기서는 기본 개념 및 사용법만을 소개하기에 언급하지는 않았지만, 알림에는 버튼을 박거나 스케쥴링을 하는 등 다양한 기능이 제공된다. 필요한 기능을 찾아보고 싶다면 레퍼런스 메뉴얼을 자세히 뒤져보자.

댓글 없음 :