라떼는말이야

안드로이드 NotificationChannel의 VibrationPattern 설정하기 (진동 패턴) with Kotlin 본문

안드로이드

안드로이드 NotificationChannel의 VibrationPattern 설정하기 (진동 패턴) with Kotlin

MangBaam 2021. 11. 5. 14:58
반응형

fun createNotificationChannel(context: Context) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    val notificationManager =
                        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                    val notificationChannel = NotificationChannel(
                        NOTIFICATION_CHANNEL_ID,
                        "수업시작 알림",
                        NotificationManager.IMPORTANCE_HIGH
                    )

                    notificationChannel.description = "수업 시작 전 알림을 띄웁니다."
                    notificationChannel.enableLights(true)
                    notificationChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
                    notificationManager.createNotificationChannel(notificationChannel)
                }
            } catch (nullException: NullPointerException) {
                Toast.makeText(
                    context,
                    "푸시 알림 채널 생성에 실패했습니다. 앱을 재실행하거나 재설치해주세요.",
                    Toast.LENGTH_SHORT
                ).show()
                nullException.printStackTrace()
            }
        }

진동 패턴을 커스텀하고 싶은데 그냥 longArray를 사용하라고만 되어있지 어떻게 동작하는지는 잘 나와있지 않아서 이것 저것 해보다가 알게된 사실을 기록한다.

longArray에 들어가는 인자 값은 (울리지 않는 시간 , 울리는 시간) 으로 짝지어진다.

즉, 위의 코드처럼 longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)의 경우

(100, 200), (300, 400), (500, 400), (300, 200), (400, null)로 묶어진다.

결과적으로 진동은 4번 울리게 된다. (진하게 된 부분이 진동이 울리는 부분이다)

참고로 숫자의 단위는 ms이다 -> 0.2초, 0.4초, 0.4초, 0.2초 만큼 울리게 되는 것이다.

100, 300, 500, 300, 400은 진동이 울리지 않는 시간이므로 진동 간의 텀이 0.1초, 0.3초, 0.5초, 0.3초, 0.4초라는 뜻이다.

물론 마지막 진동이 없어서 맨 끝의 400은 의미 없는 수이다.

 

나는 톡톡톡' 하며 가볍게 진동을 울려주길 원해서 아래와 같이 설정하였다.

notificationChannel.vibrationPattern = longArrayOf(0, 100, 100, 100, 100, 100)

 

참고로 진동 패턴은 NotificationChannel이 생성될 때 같이 만들어지며 NotificationChannel은 한 번 만들어지면 그 다음부터는 사용자의 몫이기 때문에 진동 패턴을 변경했다면 데이터를 삭제하거나 앱을 재설치하여야 변경된 결과를 확인할 수 있다.

 

 

반응형
Comments