우선 가장 기본적인 텍스트만 던져주는 알람
val channelID = "CHANNELID"
val notificationChannel = NotificationChannel(
channelID,
"TestChannelName",
NotificationManager.IMPORTANCE_DEFAULT
)
val builder = NotificationCompat.Builder(this, channelID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Title")
.setContentText("ContentText")
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
notificationManager.notify(
1, // 해당 알림의 고유 ID
builder // 표시할 알림
.setContentTitle("title")
.setContentText("content")
.build()
)
알람을 눌렀을 때 특정 Activity로 이동 및 알람 삭제
val intent = Intent(this, NextActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK // 앱 스택 관련처리 부분 굳이 없어도 작동은 함
}
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
notificationManager.notify(
1, // 해당 알림의 고유 ID
builder // 표시할 알림
.setContentTitle("title")
.setContentIntent(pendingIntent)
.setContentText("content")
.setAutoCancel(true)
.build()
)
알람에 버튼 추가하기
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val action = NotificationCompat.Action.Builder(R.drawable.ic_launcher_foreground, "TestAction", pendingIntent).build()
notificationManager.notify(
1, // 해당 알림의 고유 ID
builder // 표시할 알림
.setContentTitle("title")
.addAction(action)
.setContentText("content")
.setAutoCancel(true)
.build()
)