본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] Direct Reply Tutorial

by 돼지왕 왕돼지 2018. 2. 28.
반응형


https://blog.stylingandroid.com/nougat-direct-reply/

addaction, addRemoteInput, Android, Android N, android n nougat, app launch, Builder, direct reply, example code, getResultsFromIntent, getservice, message app, multiple choice input, notification, Notification.Action, notificationCompat, Notify, PendingIntent, remoteinput, setAllowFreeFormInput, setAllowGeneratedReplies, setChoice, setLabel, setsmallicon, setstyle, tutorial, unique key, [android] Direct Reply Tutorial


-

Direct Reply 는 Notification 으로부터 App Launch 없이 바로 답장을 보내는 기능이다.

Message App 에 한정되지 않고 모든 앱이 사용 가능하다.


direct_reply



-

Direct Reply 는 Android N Nougat 버전에 한해 사용 가능하다.



-

Direct Reply 의 키는, Notification 을 만들 때 remoteInput 을 추가하는 것이다.

// notification 생성

private static final String REPLY_KEY = “reply”;

private static final String REPLY_LABEL = “Input reply”; // Action 에 표시되는 Label


RemoteInput remoteInput = new RemoteInput.Builder(REPLY_KEY)

    .setLabel(REPLY_LABEL)

    .build();


PendingIntent replyPendingIntent = PendingIntent.getService(context, 0 , REPLY_INTENT, PendingIntent.FLAG_CANCEL_CURRENT);


Notification.Action notiAction = new NotificationCompat.Action.Builder(R.drawable.ic_replay, REPLY_LABEL, replayPendingIntent)

            .addRemoteInput(remoteInput)

            .setAllowGeneratedReplies(true)

            .build()



-

Remote Input 에 입력된 값을 가져올 때는..

void reply(Intent intent){

    Bundle bundle = RemoteInput.getResultsFromIntent(intent);

    if(bundle != null){

        String messageText = bundle.getString(REPLY_KEY);

        // do something with messageText

    }

}



-

Notification 에 RemoteInput 을 여러 개 붙일 수 있다.

그러나 User 가 꼭 필요한 것만 쉽게 하기 위해 너무 많이 붙이는 것은 좋지 않다.

그래서 갯수를 minimum 으로 유지하는 것이 중요하다.


RemoteInput 은 unique 한 key 를 가지고 있어야 하며, 이 키를 이용해 나중에 값을 가져올 수 있다.



-

RemoteInput.Builder 의

setChoice(CharSequence[]) 와 setAllowFreeFormInput(false) 를 통해 mutiple choice input 을 제공할 수도 있다.



-

위와 같이 Notification.Action 을 만들었다면, 이제 Notification 을 등록해주어야 한다.

Notification.Action replyAction = buildReplyAction();

Notification noti = new NotificationCompat.Builder(context)

    .setStyle(messagingStyle)

    .setSmallIcon(R.drawable.ic_message)

    .addAction(replyAction)

    .build();

notificationManager.notify(NOTI_ID, notification);






반응형

댓글