Notification Sound on Android 8.0

WindSekirun (wind.seo)·2022년 4월 26일
0

이 글은 기존 운영했던 WordPress 블로그인 PyxisPub: Development Life (pyxispub.uzuki.live) 에서 가져온 글 입니다. 모든 글을 가져오지는 않으며, 작성 시점과 현재 시점에는 차이가 많이 존재합니다.

작성 시점: 2018-08-03

도입

Android 8.0 에 오면서부터 Notification Channel 라는 개념이 도입되면서, 알림을 각각 '채널' 로 분류하여 각 채널마다 중요도를 설정, 표시하는 기능이다.

평소대로 ID 를 만들어 채널을 설정하고 알림을 띄웠지만, NotificationCompat.setSound 에 소리를 설정해도 기본 알림 소리만 나왔다. 약 30분 동안 삽질한 결과, NotificationChannel 클래스 자체에 setSound 라는 메서드가 있었다.

해결 방법

Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.noti_sound);

if (Build.VERSION.SDK_INT >= 26) {
    CharSequence name = getString(R.string.app_name);
    String description = getString(R.string.app_name);
    int importance = NotificationManager.IMPORTANCE_HIGH;

    NotificationChannel mChannel = new NotificationChannel(id, name, importance);
    mChannel.setDescription(description);

    mChannel.enableVibration(true);
    AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
            .build();

    mChannel.setSound(soundUri, audioAttributes);

    manager.createNotificationChannel(mChannel);
}

단 주의할 점은 생성된 알림 채널은 앱을 지우지 않는 이상 지워지지 않는다는 것이다. 즉, 채널 id를 새로 생성하는 수 밖에 없다.

profile
Android Developer @kakaobank

0개의 댓글