NotificationChannel에서 사운드 비활성화
오늘 저는 알림 채널을 사용해야하는 API 26을 타겟팅하기 시작했습니다.
내 문제는 이제 새로운 알림 (업데이트 포함)마다 성가신 소리가 재생된다는 것입니다.
이 소리를 어떻게 비활성화 할 수 있습니까?
이 사운드를 사용자 지정 mp3 사운드로 교체 한 다음 무음이있는 mp3를 전달하려고 시도했지만 무시됩니다.
기본적으로 사용자가 앱과 상호 작용 한 후 몇 가지 작업을 수행 할 수있는 옵션을 제공하는 매우 낮은 우선 순위의 알림을 추가하고 있습니다. 시끄러울 이유가 없습니다. 사용자는 알림이 표시되도록하는 앱으로 특정 작업을 수행했기 때문에 알림을 참조 할 수 있음을 알게됩니다.
사용자는 그 소리에 정말로 짜증을 내기 시작할 것입니다.
채널의 중요성을 유지하고 싶다면 사운드를 제거하는 것만으로도 효과가있는 notificationChannel.setSound(null, null);
것 같습니다.
편집 : 기존 사용자에게 적용하려면 채널의 이름을 바꾸고 이전 채널을 삭제하십시오. (채널을 만들 수 있지만 앱에서 수정할 수는 없으며 사용자 만 할 수 있습니다.)
(업데이트 2019-05 : Android Q에서는 더 나 빠지고 NotificationManager.IMPORTANCE_LOW
, 적어도 에뮬레이터에서를 사용할 때 소리가납니다 ...)
해결책은 NotificationManager.IMPORTANCE_LOW
새로운 채널 을 사용 하고 만드는 것입니다. 채널이 생성되면 중요도를 변경할 수 없습니다 (음, 가능하지만 새로운 중요도는 무시 됨). 채널 정보는 시스템에 영구적으로 저장되는 것으로 보이며 생성 된 모든 채널은 앱을 제거 할 때만 삭제됩니다. [ 업데이트 : Ferran Negre 의 댓글 에 따르면 채널을 통해 채널을 삭제 nm.deleteNotificationChannel(nChannel.getId());
하고 다시 만들 수 nm.createNotificationChannel(nChannel);
있지만 삭제 된 채널과 동일한 ID로 채널을 생성 할 수없고 다른 적용이 가능할 것으로 예상하는 한계가 있습니다. 삭제되지 않은 채널에 대한 설정, acoder 의 답변 참조 ]
이전 Android 버전은 기본적으로 사운드를 재생하지 않았지만 Android O에서 변경되었지만 API 26을 대상으로하는 경우, 즉 알림 채널을 사용하는 경우에만 변경되었습니다. 이것은 불일치입니다. 사실, 이것은 버그입니다.
그 이유는 NotificationManager.IMPORTANCE_DEFAULT
(기본적으로 NotificationManager.IMPORTANCE_HIGH
소리가 나지 않음) 채널을 만들 때 Android가 실제로 "다소"로 등록 (기본적으로 소리 재생)하기 때문입니다.
알림 옵션 (알림 항목을 길게 누름)으로 이동하여이를 확인할 수 있으며, 여기서 유형임을 읽은 NotificationManager.IMPORTANCE_HIGH
다음 알림을 비활성화 한 다음 다시 활성화합니다. 이 과정 NotificationManager.IMPORTANCE_HIGH
에서 실제로 등록 된에서에서 소리가 나지 않는 것으로 "다운 그레이드"됩니다 NotificationManager.IMPORTANCE_DEFAULT
.
이 버그는 Android 문제 추적기에 제출되었으므로 별표를 표시 할 수 있습니다 (Google에서 "수정할 수 없음 (실행 불가능)"으로 표시했습니다.
BTW,에서 새로운 문서 https://developer.android.com/training/notify-user/channels은 기본 동작은 그 방법이 사용했다는 주장은, 그 기본은 안드로이드 8.0, 이전에 소리 재생 확실히 사실이 아니다을 . 이것은 그들의 목록입니다
User-visible importance level Importance Priority
(Android 8.0 and higher) (Android 7.1 and lower)
Urgent Makes a sound and appears as IMPORTANCE_HIGH PRIORITY_HIGH
a heads-up notification or PRIORITY_MAX
High Makes a sound IMPORTANCE_DEFAULT PRIORITY_DEFAULT
Medium No sound IMPORTANCE_LOW PRIORITY_LOW
Low No sound and does not appear IMPORTANCE_MIN PRIORITY_MIN
in the status bar
가시성 중요도 높음과 알림 중요도 높음 사이의 불일치도 확인할 수 있습니다. 왜 이런 일을하는지 모르겠습니다. 그들은 확실히 코드에 버그가 있습니다.
다음 줄 아래의 모든 것은 쓸모가 없지만 거기에 언급 된 버그는 여전히 유효합니다. 내 오류가 그 생각이 있었다 NotificationManager.IMPORTANCE_MIN
에서 다음으로 낮은 하나입니다 NotificationManager.IMPORTANCE_DEFAULT
,하지만 NotificationManager.IMPORTANCE_LOW
입니다.
그런 다음 길게 누르기 알림 및 모든 채널 버튼을 통해 앱의 알림 설정으로 이동하고 해당 채널의 스위치를 껐다가 다시 켜면 실제로 자동으로 설정 NotificationManager.IMPORTANCE_DEFAULT
되고 소리가 재생되지 않습니다. 또한 충돌 후 재설정되었습니다.NotificationManager.IMPORTANCE_HIGH
따라서 기본적으로 해결 방법은 NotificationManager.IMPORTANCE_MIN
. 그러나 이미 존재하는 채널을 만든 후에는 그 중요성을 변경할 수없는NotificationManager.IMPORTANCE_MIN
것처럼 보이기 때문에 이것이 적용 되도록 새 채널 을 만들어야합니다 .
업데이트 : 해결 방법 NotificationManager.IMPORTANCE_MIN
에는 단점이 있습니다.
해당 중요도 수준을 사용하면 알림이 더 이상 알림 창에 완전히 표시되지 않고 기본적으로 축소되는 새 알림 채널 그룹에 삽입됩니다 (창을 아래로 당길 때마다 다시 축소됨). 정말 안타깝 네요!
업데이트 2 : 조금 더 깊게 파헤쳐 보면 그것이 올바르게 등록 된 것처럼 NotificationManager.IMPORTANCE_DEFAULT
보이지만 NotificationManager.IMPORTANCE_HIGH
사용자가 명시 적으로 설정을 기본값에서 높음으로 변경하는 것처럼 어떻게 든 마술처럼으로 업그레이드되었습니다 . 또한 알림을 껐다가 다시 켜면 기본값으로 재설정됩니다.
내가 아는 한 API 26 (Oreo) 이후로 알림이 생성 된 후에는 알림 소리를 변경할 수 없습니다.
notificationManager.deleteNotificationChannel("channel_id"));
NotificationChannel notificationChannel = new NotificationChannel(
"channel_id", "channel_name",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);
Even deleting the channel before creation does not help.
Google documentation says:
android.app.NotificationManager public void deleteNotificationChannel(String channelId)
Deletes the given notification channel. If you create a new channel with this same id, the deleted channel will be un-deleted with all of the same settings it had before it was deleted.
NotificationChannel#setSound()
documentation states
Only modifiable before the channel is submitted to
NotificationManager#createNotificationChannel(NotificationChannel)
Too bad that notificationBuilder.setSound(defaultSoundUri)
does not work as well:
This method was deprecated in API level 26. Use NotificationChannel#setSound(Uri, AudioAttributes) instead.
Also using support library does not work. So sound is only settable once in the app and changing by the user is only possible in the settings of the notification. For me Ferran Negre's comment did not work. I do not understand why Google made this restriction. Too bad.
NotificationManager.IMPORTANCE_LOW
It produces no sound when notification is created as I need the same in my Music Application.
And yes if you have already created a notification channel then either you need to change the channel id or simply uninstall the previous application and install again.
For me the solution was to create group notification.
val builder = NotificationCompat.Builder(this)
.setGroupAlertBehavior(GROUP_ALERT_SUMMARY)
.setGroup("My Group")
.setGroupSummary(false)
.setDefaults(DEFAULT_ALL)
.setSound(null)
But in this case, if you send a new notification with a new ID, then it will be grouped with the previous notifications.
If the case is like mine, that I am forced to show a notification for background service and I don't really want to show any notification the solution that worked on 8.0 was:
.setPriority(NotificationManager.IMPORTANCE_NONE)
With this not only I didn't get the annoying sound every 5 minutes but also minimized the appearance of the notification itself.
On 8.1 I didn't have the problem with the sound with following:
.setPriority(NotificationManager.IMPORTANCE_MIN)
You can use 2 different notification channel to send notification depending on there priority to user.
If its a high priority notification the send it via
new NotificationChannel("Channel ID", "Channel Name", NotificationManager.IMPORTANCE_HIGH);
Your user will get sound and pop when they will receive a notification.
If you want to send less important notification then use this channel.
new NotificationChannel("Channel ID", "Channel Name", NotificationManager.IMPORTANCE_LOW);
Your user will get a notification with no sound and pop up.
check different priority from here - https://developer.android.com/reference/android/app/NotificationManager
First you need to set
importance = NotificationManager.IMPORTANCE_LOW;
then
Notification n = builder.setPriority(Notification.DEFAULT_ALL).build();
n.defaults = 0;
n.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager.IMPORTANCE_LOW
This will work but also collapsed your notification.
I faced the same issue in the 8.1 and made the below changes. Change the priority of the notification as LOW and it will disable the sound of notification.
NotificationManager.IMPORTANCE_LOW
The code is like following:
NotificationChannel chan1 = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_LOW);
NotificationManager.IMPORTANCE_LOW
will work for sure
참고URL : https://stackoverflow.com/questions/45919392/disable-sound-from-notificationchannel
'developer tip' 카테고리의 다른 글
adb에서 패키지의 실행 가능한 활동 이름 가져 오기 (0) | 2020.11.13 |
---|---|
Pycharm : Python 파일의 일부만 실행 (0) | 2020.11.13 |
MySQL-한 쿼리에서 테이블 당 모든 행을 계산하는 방법 (0) | 2020.11.13 |
두 개의 json / javascript 배열을 하나의 배열로 병합 (0) | 2020.11.13 |
Django 뷰에서 HTTP 상태 코드 204를 반환하려면 어떻게해야합니까? (0) | 2020.11.13 |