Programming/Mac & iOS

[iOS] Interactive Notification with Text Input (iOS9 or later)

MB Brad KWON 2015. 12. 1. 09:45

    우리는 일전에 iOS8에서 소개되었던 Interactive Notification이라는 기능에 대해 배운바가 있다. 그런데 필자가 다시 Interactive Notification이라는 물건을 다시 꺼내 들어 설명하는 이유는 무엇인가. 그토록 기다려왔던 TextField가 Interactive Notification에 추가되어 API로 공개되었기 때문이다. 아래의 이미지는 페이스북 메신저에서 선 구현한 Interactive Notification에 TextField가 추가된 모습이다. 열심히 구글링을 한 결과, iOS9에서 새롭게 추가된 프로퍼티의 영향임이 확인되었다.






    UIUserNotificationAction이라는 것을 이용하여 Interactive Notification에 사용자의 action을 활용할 수 있음을 이미 배웠다. UIUserNotificationAction에서 우리가 주목해야 할것은 behavior라는 프로퍼티이다. behavior는 iOS9에서 새롭게 추가된 프로퍼티로 'Default'와 'TextInput'이라는 2개의 상수를 택할 수 있다. 이중 TextInput을 behavior에 set하면 Notification을 처리할 때, 'UIUserNotificationActionResponseTypedTextKey'라는 키를 이용하여 NSString 객체를 획득할 수 있다. 이를 이용하여 NSString 객체를 이용한 사용자 서비스를 추가할 수 있다. 위의 이미지에서 보듯이 'Quick Reply'같은 기능을 말이다.


    아래는 StackOverflow에서 찾아낸 예제 코드이니 참조하도록 하자.


 //creating the inline reply notification action
   let replyAction = UIMutableUserNotificationAction()
   replyAction.title = "Say Something"
   replyAction.identifier = "inline-reply"
   replyAction.activationMode = .Background
   replyAction.authenticationRequired = false
   replyAction.behavior = .TextInput

 //creating a category
   let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
   notificationCategory.identifier = "INVITE_CATEGORY"
   notificationCategory .setActions([replyAction], forContext: UIUserNotificationActionContext.Default)

 //registerting for the notification.
      application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[ UIUserNotificationType.Sound, UIUserNotificationType.Alert,
            UIUserNotificationType.Badge], categories: NSSet(array:[notificationCategory]) as? Set<UIUserNotificationCategory>))