본문 바로가기
개발/Android

[Kotlin] 실제 휴대폰 Push 알림(Notification) 확인하기

by jeomn 2022. 3. 10.

0. 실제 휴대폰에 깔아서 알림 저장해보기

  • 사용하고 있는 갤럭시 휴대폰을 USB 디버깅 연결해서 확인
  • 실제 알림들이 올 때 Category가 어떻게 오는 지 궁금했어서, 그 부분도 저장할 수 있게끔 바꾼다.
    • 머신러닝을 통해 데이터를 분류할 때 카테고리 정보도 필요하지 않을까..?
  • 알림 저장은 잘 되는데...?
  • 실제로 받아보니 확인해봐야할 데이터들이 보인다....

 

1. Database 변경

  • Entity 수정
@Entity
data class NotiData(
    ...
    @ColumnInfo(name = "category") val category : String?
){
    constructor(title: String?, content: String?, category: String?): this( null, title, content, category)
}
 
  • Room을 사용했기 때문에 자동 이전 가능
    • Room version 2.4.0-alpha01이상만 자동 이전 가능
    • version을 올리고,
    • AutoMigration 옵션 추가
/*
//이전 버전
@Database(entities = [NotiData::class], version = 1)
abstract class NotiDatabase : RoomDatabase() {
    ...
}
*/

@Database(entities = [NotiData::class],
    version = 2,
    autoMigrations = [AutoMigration(from=1, to=2)])
abstract class NotiDatabase : RoomDatabase(){
    ...
}
  • 에러
Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation annotation processor argument OR set exportSchema to false.
    • schemaLocation을 설정해줘! 하는 말이 뭐지...하고 살펴봤더니,
    • AutoMigration을 진행할 때에는 json으로 DB 정보가 저장된다.
    • 그래서 exportSchema 설정이 반드시 true여야 하고(false로 할 시 Auto Migration을 할 수 없다는 에러가 뜸)
    • 그 json 파일이 저장될 경로를 지정해줘야 한다.
    • 변경 후에는 version 1의 DB를 가지고 빌드를 한 번 해줘야 한다. 바로 version 2를 실행할 시, 이전 버전의 DB가 없다는 에러가 떴다.
    • build.gradle 수정

 

 

android {
        ...
        defaultConfig {
            ...
            kapt{
                arguments {
                    arg("room.schemaLocation", "$projectDir/schemas")
                }
            }
        }
        ...
    }

 

2. Category 추가

  • 실제 알람들에서 category 값이 어떻게 올 지도 궁금해서, 확인을 위해 category를 설정하고 알림을 송신, 수신해봤다.
  • 알림 송신 App 수정
    • setCategory로 카테고리를 설정해줄 수 있었다.
    • MainActivity.kt
private fun createNotification(intent: PendingIntent, id:Int, channelId: String, title: String, text : String){
    ...
    var builder = NotificationCompat.Builder(this, channelId)
        ...
        //.setCategory(Notification.CATEGORY_MESSAGE)   //메세지 카테고리
        .setCategory(Notification.CATEGORY_PROMO)      //promotion 카테고리
    ...
}
  • 알림 저장 App 수정
    • 알림의 category를 받아 DB에 저장하도록 수정
    •  
override fun onNotificationPosted(sbn: StatusBarNotification?) {
    ...
    val category = notification?.category
    ...
    notiDb.notiDataDao().insert(NotiData(title, content, category))
}

 

3. 결과

  • Virtual Machine 동작(Custom Notification)

저장된 알림 데이터 확인

  • 실제 휴대폰에서 저장한 알림 DB 결과
    • 저장은 잘 되는데...
    • 카카오톡 알림의 경우, 저렇게 null null 데이터가 온 이후에 알림 내용이 추가되었다.
    • 더 뜯어봐야할 것같다...!

noti 데이터베이스 실제 데이터 저장 확인

 

4. 참조

댓글