Programming/Mac & iOS

[iOS] Notification Action : Interactive Notification in iOS8

MB Brad KWON 2014. 9. 18. 15:24

    iOS8에서 추가된 새로운 기능 중에 알림에 대해 사용자가 즉각적으로 처리할 수 있도록 해주는 강력한 기능이 추가됬다. 'Interactive Notification'이라고 소개된 이 기능은 알림이 왔을 때, 사용자가 알림 창을 살짤 내리면 알림에 대한 처리가 바로 가능하다. 이는 아이폰의 상태바 알림 창 뿐만아니라 잠금 화면에서도 활용 가능하여 사용자의 편의를 극대화 시켜주는 기능이다. 개발할 때, 'notification action'으로 검색해보면 쉽게 찾을 수 있다.





    notification action을 구현하기 위해선 action을 UIUserNotificationAction을 이용하여 객체를 정의한다. 정의된 action을 UIUserNotificationCategory로 묶은 다음 'registerUserNotificationSettings'을 통해 notification을 등록할 때 'UIUserNotificationSettings(forTypes: categories:)'에 set해주면 된다.


    먼저 action을 정의하는것을 보게되면 action은 notification이 왔을 때 나타나는 버튼 하나라고 보면 된다. 위의 사진을 보면 버튼이 2개씩 나타나는 것을 볼 수 있다. 저 버튼하나에 action이 하나씩 할당되는 것이다. action은 UIUserNotificationAction으로 정의 하며 사용하기 쉽게 UIMutableUserNotifiactionAction을 사용한다. 정의할 땐 아래와 같이 해준다. 





    'identifier'는 appDelegate에서 notification action을 처리할 때 각 action의 구분을 위해서 사용된다. 'destructive'는 button을 빨간색으로 하이라이팅 할지 말지를 결정한다. 'title'은 button의 글을 정의하고 'activeMode'는 action을 처리할 때 앱의 상태를 정의 하는 곳으로 '.Foreground'와 '.Background'로 정의할 수 있다. 'authenticationRequired'는 사용자가 action을 하기 위해 PIN이나 touchID를 통해 인증이 필요한 기능의 접근에서 true를 설정하면 된다. 위와 같은 사항들을 설정하고 나면 notification action에 사용할 action이 정의된다.


    다음, 정의된 action들을 notification 등록하기 위한 category로 만든다. category는 UIUserMotificationCategory를 이용하며 사용하기 쉽게 하기 위해 UIMutableUserNotificationCategory를 사용한다. 





    category를 생성하면 category의 'identifier'를 먼저 설정해준다. 여기서 설정하는 identifier는 notification이 발생했을 때, 시스템에서 등록된 category에서 해당 notification에 맞는 것을 찾는 구분 기준이 되며 이것이 일치한 notification category를 찾지 못하면 action을 사용할 수 없다. 'setActions([], forContext:)'를 통하여 위에서 정의한 action들을 []에 넣어주면 된다. 여기서 알아둬야할 중요한 점은 forContext의 param이다. param으로 'UIUserNotificationActionContext'를 사용한다. action context에는 '.Default'와 '.Minimal'이 있다. 'Default'는 알림을 alert으로 표시할 경우, 사용되며 action을 최대 4개까지 추가할 수 있다. 'Minimal'은 상태 바의 알림창이나 잠금화면에 표시되는 것으로 action을 최대 2개까지만 추가할 수 있다. 구현할 때, 명심해두면 원활한 기능 구현이 될 수 있다. 아래의 스크린샷을 참고하면 이해가 빠를 것이다.


.Default




.Minimal





    우리는 category 설정까지 완료 했다. 이제는 실제로 notification을 등록하고 발생시킬 때 category를 이용해 action을 처리해보자. 가장 처음에 말한 봐와 같이 'UIApplication.sharedApplication.registerUserNotificationSettings(UIUserNotificationSettings)'를 이용해서 Notification을 등록이 가능하며 'UIUserNotificationSetting'는 'UIUserNotificationSetting(forTypes:, categories:)'를 통해 생성이 가능하다. 이 때 type은 notification의 type으로 'UIUserNotificationType'을 참조하여 사용한다. type은 '.None / .Badge / .Sound / .Alert' 등이 있다. 'categories'는 자신이 위처럼 정의한 category들을 NSSet을 이용하여 묶은 후에 사용한다. 이렇게 해서 생성된 UIUserNotificationSetting을 등록하면 아래와 같이 구현될 것이다.





    notification action을 등록했다. 이제 notification에 실제로 사용해보자. notification은 크게 2가지로 나뉜다. LocalNotification과 RemotreNotification으로 Local에서 사용할 때는 'UILocalNotification' 객체에 'category' 프로퍼티에 미리 등록해둔 category의 identifier를 사용하면 된다. Remote, 즉 APNS에서는 payload에 'category'라는 key를 추가한 후에 value를 미리 등록해둔 category의 identifier를 사용해주면 된다.


    위처럼 하면 notification이 발생했을 때, category와 context에 맞는 action들이 같이 등장하게 된다. action을 사용자가 선택했을 때는 AppDelegate의 'application(application:, handleActionWithIdentifier, forLocalNotification, completionHandler:)'과 'application(application:, handleActionWithIdentifier, forRemoteNotification, completionHandler:)'을 구현해 준다. 각 action의 구분은 action을 생성할 때 정의한 'identifier'로 구분해주면 된다. action의 처리가 완료되면 'completionHandler()'를 꼭 호출해준다. 이는 시스템에 action을 비동기적으로 process를 죽이지 말아 달라는 것을 의미한다.



예제 : https://github.com/ShinobiControls/iOS8-day-by-day/tree/master/25-notification-actions