본문

170425(화) - Android 7.0 <Notification Enhancements>

Android 7.0 <Notification Enhancements>


- Replying to notifications

ㆍAPI level 24+ 에서는 directly respond 및 update date lists 가능 


- Adding inline reply actions

 RemoteInput.Builder


// Key for the string that's delivered in the action's intent.
private static final String KEY_TEXT_REPLY = "key_text_reply";
String replyLabel = getResources().getString(R.string.reply_label);
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
       
.setLabel(replyLabel)
       
.build();


// Create the reply action and add the remote input.
Notification.Action action =
       
new Notification.Action.Builder(R.drawable.ic_reply_icon,
                getString
(R.string.label), replyPendingIntent)
               
.addRemoteInput(remoteInput)
               
.build();


// Build the notification and add the action.
Notification newMessageNotification =
       
new Notification.Builder(mContext)
               
.setSmallIcon(R.drawable.ic_message)
               
.setContentTitle(getString(R.string.title))
               
.setContentText(getString(R.string.content))
               
.addAction(action))
               
.build();

// Issue the notification.
NotificationManager notificationManager =
       
NotificationManager.from(mContext);
notificationManager
.notify(notificationId, newMessageNotification);



- Retrieving user input from the inline reply

getResultsFromIntent()


Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);


// Obtain the intent that started this activity by calling
// Activity.getIntent() and pass it into this method to
// get the associated string.

private CharSequence getMessageText(Intent intent) {
   
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
   
if (remoteInput != null) {
       
return remoteInput.getCharSequence(KEY_TEXT_REPLY);
   
}
   
return null;
 
}


// Build a new notification, which informs the user that the system
// handled their interaction with the previous notification.
Notification repliedNotification =
       
new Notification.Builder(context)
               
.setSmallIcon(R.drawable.ic_message)
               
.setContentText(getString(R.string.replied))
               
.build();

// Issue the new notification.
NotificationManager notificationManager =
       
NotificationManager.from(context);
notificationManager
.notify(notificationId, repliedNotification);


setRemoteInputHistory()

- 위의 메소드를 사용하면 reply history를 사용자에게 보여줄 수 있다.



ㆍremote input을 받은 뒤에는 notification을 update 하거나 cancel하는 작업 필요하다.

ㆍdirect reply를 사용해서 회신한 경우 notification을 취소하지말고 회신내용을 update 하여 표시해야 한다.

ㆍ다른 여러가지 템플릿을 사용해서 remote-input history를 표시 가능



- Bundling notifications

ㆍAPI level 24+ 에서는 queue of notifications : bundled notification 을 제공한다.

ㆍAndroid Wear의 notification stacks 라고 생각하면 된다.

Builder.setGroup() 을 통해서 구현


ㆍparent notification : 상위

ㆍsummary notification : 하위들


ㆍ동일한 앱이 4개 이상의 notification을 보내고 그룹핑을 하지않으면 system이 자동적으로 그룹핑해버린다.


- When to use bundled notifications

ㆍ아래의 사항이 모두 해당되는 경우에만 사용해야 한다.

- child notification은 완전한 notification이므로 summary 그룹없이 단독으로 표시 가능한 경우

- child notification을 개별적으로 노출시킬 때 이점이 있다.

ㆍ각각의 child notification에 action이 존재

ㆍchild에 읽어야하는 정보가 더 많을 경우


ㆍ메시징 앱, e-mail app이 이를 사용하기에 적합하다.



- Notification Metadata

ㆍnotification은 다음에 의해서 정렬된다.


setCategory()

디바이스가 priority mode 일때 app 알림을 처리하는 방법을 system에 알려줌


setPriority()

PRIORITY_MAX or PRIORITY_HIGH

sound or vibration이 포함된 notification의 경우 우선순위 알림


addPerson()

notification에 사람 목록을 추가 가능


- Heads-up Notifications

ㆍAPI level 21+ 에서 적용

ㆍtrigger 조건

1. full screen mode

2. high priority + ringtones or vibrations



- Custom Notification layouts

RemoteViews 사용

ㆍheight는 notification view에 따라서 다름

- normal : 64dp

- expanded : 256dp


1. Create XML layout 

2. Define notification icon & text

3. Put this RemoteViews into your NotificationCompat.Builder. setContent()

4. Avoid setting a background, because unreadable.


ㆍ다른 디바이스에서 제대로 작동하는지 확인이 필요하다.

ㆍNotification bar가 매우 좁은 공간이기 때문에 특히 주의 필요

ㆍ구성을 너무 복잡하게 하지말고 많은 테스트를 해 볼것.



- Custom Views

API level 24+ 

ㆍnotification views

ㆍheaders

ㆍactions

ㆍexpandable layouts


DecoratedCustomViewStyle()


DecoratedMediaCustomViewStyle()


Notification notification = new Notification.Builder()
           
.setSmallIcon(R.drawable.ic_stat_player)
           
.setLargeIcon(albumArtBitmap))
           
.setCustomContentView(contentView);
           
.setStyle(new Notification.DecoratedCustomViewStyle())
           
.build();


- Messaging style

API level 24+


Notification notification = new Notification.Builder()
             
.setStyle(new Notification.MessagingStyle("Me")
                 
.setConversationTitle("Team lunch")
                 
.addMessage("Hi", timestamp1, null) // Pass in null for user.
                 
.addMessage("What's up?", timestamp2, "Coworker")
                 
.addMessage("Not much", timestamp3, null)
                 
.addMessage("How about lunch?", timestamp4, "Coworker"))
             
.build();


'Mobile > Android API' 카테고리의 다른 글

170613(화) - Android 6.0 Changes  (0) 2017.06.13
170612(월) - Android 6.0 APIs  (0) 2017.06.12
170424(월) - Android 7.0 <Multi-window>  (0) 2017.04.24
170418(화) - Java 8 언어 기능 사용  (0) 2017.04.18
170411(화) - System permission  (0) 2017.04.11

공유

댓글