Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- HTTP
- sqlite
- AppBarLayout
- Algorithm
- Coroutine
- View
- 알림
- Android
- onMeasure
- CollapsingToolbarLayout
- recyclerview
- 알고리즘
- 안드로이드
- CustomView
- kotlin
- lifecycle
- hilt
- ViewModel
- room
- BOJ
- DataBinding
- notification
- onLayout
- 코틀린
- 백준
- Navigation
- Behavior
- activity
- CoordinatorLayout
- LiveData
Archives
- Today
- Total
개발일지
Android in A..Z - Notification (Custom) 본문
Notification Custom
사용자가 직접 Layout을 구성하여 RemoteView를 통해 Nofitication 템플릿을 만들 수 있습니다.
* 보통 축소된 Notification은 64dp, 확장된 Notification은 256dp로 제한됩니다.
RemoteViews를 만들고setCustomContentView를 통해 적용할 수 있다.
* 확장된 CustomContentView는 setCustomBigContentView를 통해 적용할 수 있다.
private fun createCustomContentView(): RemoteViews {
return RemoteViews(context.packageName, R.layout.notification_content_view).apply {
setOnClickPendingIntent(
R.id.lock,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_LOCK
}
, 0
)
)
setOnClickPendingIntent(
R.id.lock_open,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_LOCK_OPEN
}
, 0
)
)
setOnClickPendingIntent(
R.id.rotation,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_ROTATION
}
, 0
)
)
setOnClickPendingIntent(
R.id.portrait,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_PORTRAIT
}
, 0
)
)
setOnClickPendingIntent(
R.id.landscape,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_LANDSCAPE
}
, 0
)
)
}
}
fun notify(message: Message) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel()
}
val notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_android)
.setShowWhen(false)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(createCustomContentView())
.build()
manager.notify(message.id, notification)
}
코드
class CustomNotificationManager @Inject constructor(
@ApplicationContext
private val context: Context
) {
companion object {
private const val CHANNEL_ID = "com.taetae98.notification.CUSTOM"
private const val CHANNEL_NAME = "Custom"
private const val CHANNEL_DESCRIPTION = "This is Custom Notification"
}
private val manager by lazy {
NotificationManagerCompat.from(context)
}
fun notify(message: Message) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel()
}
val notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_android)
.setShowWhen(false)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(createCustomContentView())
.build()
manager.notify(message.id, notification)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel() {
val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH).apply {
description = CHANNEL_DESCRIPTION
}
manager.createNotificationChannel(channel)
}
private fun createCustomContentView(): RemoteViews {
return RemoteViews(context.packageName, R.layout.notification_content_view).apply {
setOnClickPendingIntent(
R.id.lock,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_LOCK
}
, 0
)
)
setOnClickPendingIntent(
R.id.lock_open,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_LOCK_OPEN
}
, 0
)
)
setOnClickPendingIntent(
R.id.rotation,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_ROTATION
}
, 0
)
)
setOnClickPendingIntent(
R.id.portrait,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_PORTRAIT
}
, 0
)
)
setOnClickPendingIntent(
R.id.landscape,
PendingIntent.getBroadcast(
context,
1000,
Intent(context, CustomNotificationReceiver::class.java)
.apply {
action = CustomNotificationReceiver.ACTION_LANDSCAPE
}
, 0
)
)
}
}
}
Git
https://github.com/KangTaeJong98/Example/tree/main/Android/Notification
KangTaeJong98/Example
My Example Code. Contribute to KangTaeJong98/Example development by creating an account on GitHub.
github.com
'Android (안드로이드) > Notification' 카테고리의 다른 글
Android in A..Z - Notification (Group) (0) | 2021.07.15 |
---|---|
Android in A..Z - Notification (Extend) (0) | 2021.07.15 |
Android in A..Z - Notification (Action) (0) | 2021.07.15 |
Android in A..Z - Notification (기본) (0) | 2021.07.15 |
Comments