Android Api 버전별로 다른 동작의 notification 을 만들려면 Action 을 이용함
NotificationCompat.Action(int icon, CharactorSequence title, PendingIntent intent)
Large icon 을 설정하면 버전별로 각각 다른 위치에 아이콘이 보이게 된다
단순구현
public Notification getNotification(Context context) {
return new NotificationCompat.Builder(context, getName())
.setSmallIcon(R.drawable.noti_status_icon)
.setColor(Color.GREEN)
.setContentTitle("content title area")
.setProgress(100, 0, true)
.setLargeIcon(getBitmapFromResource(context, R.drawable.badge_gift))
.setShowWhen(false)
.addAction(new NotificationCompat.Action(R.drawable.star, "star", null))
.addAction(new NotificationCompat.Action(R.drawable.check, "check", null))
.build();
}
OS 버전별로 다른 Notification 배경 색상
<TextView
style="@style/TextAppearance.Compat.Notification.Title"
... />
// 버전별로 분기해서 style 을 만들어 놓음
TextAppearance.Compat.Notification.Title(.../values/values.xml)
기본 icon 활용
android.R.drawable.stat_sys_download
android.R.drawable.stat_sys_download_done
Create a fully custom notification layout - Full custom layout 을 만드는 경우
- setStyle() 을 호출하면 안됨
- OS >= 7.0 인 경우 DecoratedCustomViewStyle() 적용
- OS >= 7.0 인 경우 Padding 이 자동으로 들어가는 점 고려
Grouping
.setGroupSummary(true)
Channel : 채널
Android O 이상부터는 채널 적용해야 Notification 정상 노출
개발자는 Notification 에 채널을 할당하고
사용자는 채널에 있는 Notification 들에 대해 개별 설정을 할 수 있음
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notificationManager.createNotificationChannel(new NotificationChannel( "channelId", "channelName" NotificationManager.IMPORTANCE_DEFAULT)); } notificationManager.notify(ordinal(), notification); public Notification getNotification(Context context) { return new NotificationCompat.Builder(context, getName()) .setSmallIcon(R.drawable.noti_status_icon) .setColor(Color.GREEN) .setGroup(GROUP_NAME) .setChannelId("channelId") .setContentTitle("channel test") .build(); }
'Android > Android Develop' 카테고리의 다른 글
WorkManager (0) | 2019.07.07 |
---|---|
Android Navigation (0) | 2019.07.07 |
Android Slice (0) | 2019.07.07 |
Instant App (0) | 2019.07.07 |