![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/015.gif)
📱테스트 환경
Samsung Galaxy Android 13 • Android 10
😢 내가 겪은 문제
앱에서 알림을 보내고 싶은데 ... FCM 으로 보내는 방법의 설명은 참 많았는데 나는 FCM 을 통해서 보내는걸 원하지 않았다 ....!!
그래서 정보찾기가 정말 힘들었는데 .. 일단 나는 FCM 을 이용하지 않고 앱에서 알림을 보내기로 했으니
필요하다면 참고하기엔 괜찮을 것 같다 !!
Manifests.xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Android 13 이상 에서는 예외 없는 알림을 보내기 위한 새로운 런타임 권한 이 필요 하다고 말하니 추가 해준다.
sendNotification.kt
private val CHANNEL_ID = "채널 아이디"
private val NOTIFICATION_ID = 1
fun sendNotification(context: Context) {
val pendingIntent = PendingIntent.getActivity(
context,
0,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val color = ContextCompat.getColor(context, R.color.원하는색상)
val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("타이틀")
.setContentText("내용")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setColor(color)
.setAutoCancel(true)
val notificationManager = NotificationManagerCompat.from(context)
createNotificationChannel(notificationManager)
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build())
}
설명을 해보자면 , 먼저 나는 이걸 함수로 만들어서 사용했기 때문에 utils 폴더 안에 넣어놓고 사용했다. 그래서 호출하는 곳에서
sendNotification(this) 를 통해서 context 를 전달해줬다.
.setSmallIcon(R.drawable.notification_icon): 알림 아이콘을 설정합니다. 작은 아이콘은 알림 바에 표시
.setContentTitle("제목"): 알림의 제목을 설정
.setContentText("내용"): 알림의 내용을 설정
.setPriority(NotificationCompat.PRIORITY_DEFAULT): 알림의 우선순위를 설정
- PRIORITY_MIN: 최소 우선순위.
- PRIORITY_LOW: 낮은 우선순위. 중요하지 않은 알림
- PRIORITY_DEFAULT: 기본 우선순위. 대부분의 일반적인 알림
- PRIORITY_HIGH: 높은 우선순위. 중요한 알림으로 사용
- PRIORITY_MAX: 최대 우선순위. 매우 중요한 알림으로 사용
.setColor(color): 알림 아이콘의 배경색을 설정
.setAutoCancel(true): 사용자가 알림을 클릭하거나 스와이프하여 제거할 때, 자동으로 알림을 취소하도록 설정
이외에도 많은 커스텀 기능들이 있는데, 나에게 필요한 기능들은 이정도였던것 같다 : )
공식문서에 설명이 아주 잘 되어 있어 나도 따라할 수 있으니 다들 손쉽게 만들 수 있으면 좋겠다 !!
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint (0) | 2023.08.09 |
---|---|
[Kotlin] Activity 간에 데이터 주고받는 방법 (0) | 2023.08.09 |
[Kotlin] Activity 이동 시 Animation 제거하는 방법 (0) | 2023.08.09 |
[Kotlin] Splash Screen 만들기 (0) | 2023.08.09 |
[Kotlin] 앱 삭제 이후에도 일부 데이터 캐시 남아있을 때 (0) | 2023.08.09 |