Android Oreo Does not Play Custom Sound for Notification

Multi tool use
I am trying to add a custom sound to notification for API > 26. Below is the code
NotificationChannel notificationChannel = new NotificationChannel("channel id","channel name",NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(notificationChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
notificationChannel.setSound(Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/raw/beep"),audioAttributes);
The problem here is that it, plays default piano sound of device rather than playing beep sound from assets. I am not allowed to use ringtone manager but common sense stats that notification sound should be that which is specified rather than default.
It works fine for API <= 26

add a comment |
I am trying to add a custom sound to notification for API > 26. Below is the code
NotificationChannel notificationChannel = new NotificationChannel("channel id","channel name",NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(notificationChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
notificationChannel.setSound(Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/raw/beep"),audioAttributes);
The problem here is that it, plays default piano sound of device rather than playing beep sound from assets. I am not allowed to use ringtone manager but common sense stats that notification sound should be that which is specified rather than default.
It works fine for API <= 26

Does it work for API below 26?
– Pankaj Kumar
Apr 27 '18 at 12:48
Look into stackoverflow.com/questions/46019496/…
– Pankaj Kumar
Apr 27 '18 at 12:52
Dear Pankaj, its using ringtone manager which I dont wanna use because of requirements.
– Lion Heart
Apr 27 '18 at 12:55
Check this
– Pravin Divraniya
Apr 27 '18 at 13:01
No that didnt help as well. My OS version is 8.1.0
– Lion Heart
Apr 30 '18 at 7:08
add a comment |
I am trying to add a custom sound to notification for API > 26. Below is the code
NotificationChannel notificationChannel = new NotificationChannel("channel id","channel name",NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(notificationChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
notificationChannel.setSound(Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/raw/beep"),audioAttributes);
The problem here is that it, plays default piano sound of device rather than playing beep sound from assets. I am not allowed to use ringtone manager but common sense stats that notification sound should be that which is specified rather than default.
It works fine for API <= 26

I am trying to add a custom sound to notification for API > 26. Below is the code
NotificationChannel notificationChannel = new NotificationChannel("channel id","channel name",NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(notificationChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
notificationChannel.setSound(Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/raw/beep"),audioAttributes);
The problem here is that it, plays default piano sound of device rather than playing beep sound from assets. I am not allowed to use ringtone manager but common sense stats that notification sound should be that which is specified rather than default.
It works fine for API <= 26


edited Apr 30 '18 at 10:00
Lion Heart
asked Apr 27 '18 at 12:37


Lion HeartLion Heart
4041617
4041617
Does it work for API below 26?
– Pankaj Kumar
Apr 27 '18 at 12:48
Look into stackoverflow.com/questions/46019496/…
– Pankaj Kumar
Apr 27 '18 at 12:52
Dear Pankaj, its using ringtone manager which I dont wanna use because of requirements.
– Lion Heart
Apr 27 '18 at 12:55
Check this
– Pravin Divraniya
Apr 27 '18 at 13:01
No that didnt help as well. My OS version is 8.1.0
– Lion Heart
Apr 30 '18 at 7:08
add a comment |
Does it work for API below 26?
– Pankaj Kumar
Apr 27 '18 at 12:48
Look into stackoverflow.com/questions/46019496/…
– Pankaj Kumar
Apr 27 '18 at 12:52
Dear Pankaj, its using ringtone manager which I dont wanna use because of requirements.
– Lion Heart
Apr 27 '18 at 12:55
Check this
– Pravin Divraniya
Apr 27 '18 at 13:01
No that didnt help as well. My OS version is 8.1.0
– Lion Heart
Apr 30 '18 at 7:08
Does it work for API below 26?
– Pankaj Kumar
Apr 27 '18 at 12:48
Does it work for API below 26?
– Pankaj Kumar
Apr 27 '18 at 12:48
Look into stackoverflow.com/questions/46019496/…
– Pankaj Kumar
Apr 27 '18 at 12:52
Look into stackoverflow.com/questions/46019496/…
– Pankaj Kumar
Apr 27 '18 at 12:52
Dear Pankaj, its using ringtone manager which I dont wanna use because of requirements.
– Lion Heart
Apr 27 '18 at 12:55
Dear Pankaj, its using ringtone manager which I dont wanna use because of requirements.
– Lion Heart
Apr 27 '18 at 12:55
Check this
– Pravin Divraniya
Apr 27 '18 at 13:01
Check this
– Pravin Divraniya
Apr 27 '18 at 13:01
No that didnt help as well. My OS version is 8.1.0
– Lion Heart
Apr 30 '18 at 7:08
No that didnt help as well. My OS version is 8.1.0
– Lion Heart
Apr 30 '18 at 7:08
add a comment |
1 Answer
1
active
oldest
votes
Finally I managed to find a solution on my own. Below is the code
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(notificationSoundUri != null){
// Changing Default mode of notification
notificationCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
// Creating an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.channel_id_prayers),context.getString(R.string.channel_name_prayers),NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(notificationSoundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationCompatBuilder.build());
Yes, but once the channel is created the only way to change its sound or vibration is through the android settings
– Lluis Felisart
May 1 '18 at 10:16
That is up to user who wants to change or mute sound but pro-grammatically I was require to set custom sound
– Lion Heart
May 1 '18 at 10:43
Thank you! Setting the sound on the notification channel worked for me as well in Oreo. I'm getting my sounds from android resources.
– efeder
May 2 '18 at 16:28
OK, it means that you have tocreateNotificationChannel
withaudioAttributes
then it would work.
– John_J
May 22 '18 at 12:37
where have u defined notificationSoundUri
– Panache
May 23 '18 at 7:16
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50062576%2fandroid-oreo-does-not-play-custom-sound-for-notification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Finally I managed to find a solution on my own. Below is the code
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(notificationSoundUri != null){
// Changing Default mode of notification
notificationCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
// Creating an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.channel_id_prayers),context.getString(R.string.channel_name_prayers),NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(notificationSoundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationCompatBuilder.build());
Yes, but once the channel is created the only way to change its sound or vibration is through the android settings
– Lluis Felisart
May 1 '18 at 10:16
That is up to user who wants to change or mute sound but pro-grammatically I was require to set custom sound
– Lion Heart
May 1 '18 at 10:43
Thank you! Setting the sound on the notification channel worked for me as well in Oreo. I'm getting my sounds from android resources.
– efeder
May 2 '18 at 16:28
OK, it means that you have tocreateNotificationChannel
withaudioAttributes
then it would work.
– John_J
May 22 '18 at 12:37
where have u defined notificationSoundUri
– Panache
May 23 '18 at 7:16
|
show 4 more comments
Finally I managed to find a solution on my own. Below is the code
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(notificationSoundUri != null){
// Changing Default mode of notification
notificationCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
// Creating an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.channel_id_prayers),context.getString(R.string.channel_name_prayers),NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(notificationSoundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationCompatBuilder.build());
Yes, but once the channel is created the only way to change its sound or vibration is through the android settings
– Lluis Felisart
May 1 '18 at 10:16
That is up to user who wants to change or mute sound but pro-grammatically I was require to set custom sound
– Lion Heart
May 1 '18 at 10:43
Thank you! Setting the sound on the notification channel worked for me as well in Oreo. I'm getting my sounds from android resources.
– efeder
May 2 '18 at 16:28
OK, it means that you have tocreateNotificationChannel
withaudioAttributes
then it would work.
– John_J
May 22 '18 at 12:37
where have u defined notificationSoundUri
– Panache
May 23 '18 at 7:16
|
show 4 more comments
Finally I managed to find a solution on my own. Below is the code
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(notificationSoundUri != null){
// Changing Default mode of notification
notificationCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
// Creating an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.channel_id_prayers),context.getString(R.string.channel_name_prayers),NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(notificationSoundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationCompatBuilder.build());
Finally I managed to find a solution on my own. Below is the code
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(notificationSoundUri != null){
// Changing Default mode of notification
notificationCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
// Creating an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.channel_id_prayers),context.getString(R.string.channel_name_prayers),NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(notificationSoundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationCompatBuilder.build());
answered May 1 '18 at 10:12


Lion HeartLion Heart
4041617
4041617
Yes, but once the channel is created the only way to change its sound or vibration is through the android settings
– Lluis Felisart
May 1 '18 at 10:16
That is up to user who wants to change or mute sound but pro-grammatically I was require to set custom sound
– Lion Heart
May 1 '18 at 10:43
Thank you! Setting the sound on the notification channel worked for me as well in Oreo. I'm getting my sounds from android resources.
– efeder
May 2 '18 at 16:28
OK, it means that you have tocreateNotificationChannel
withaudioAttributes
then it would work.
– John_J
May 22 '18 at 12:37
where have u defined notificationSoundUri
– Panache
May 23 '18 at 7:16
|
show 4 more comments
Yes, but once the channel is created the only way to change its sound or vibration is through the android settings
– Lluis Felisart
May 1 '18 at 10:16
That is up to user who wants to change or mute sound but pro-grammatically I was require to set custom sound
– Lion Heart
May 1 '18 at 10:43
Thank you! Setting the sound on the notification channel worked for me as well in Oreo. I'm getting my sounds from android resources.
– efeder
May 2 '18 at 16:28
OK, it means that you have tocreateNotificationChannel
withaudioAttributes
then it would work.
– John_J
May 22 '18 at 12:37
where have u defined notificationSoundUri
– Panache
May 23 '18 at 7:16
Yes, but once the channel is created the only way to change its sound or vibration is through the android settings
– Lluis Felisart
May 1 '18 at 10:16
Yes, but once the channel is created the only way to change its sound or vibration is through the android settings
– Lluis Felisart
May 1 '18 at 10:16
That is up to user who wants to change or mute sound but pro-grammatically I was require to set custom sound
– Lion Heart
May 1 '18 at 10:43
That is up to user who wants to change or mute sound but pro-grammatically I was require to set custom sound
– Lion Heart
May 1 '18 at 10:43
Thank you! Setting the sound on the notification channel worked for me as well in Oreo. I'm getting my sounds from android resources.
– efeder
May 2 '18 at 16:28
Thank you! Setting the sound on the notification channel worked for me as well in Oreo. I'm getting my sounds from android resources.
– efeder
May 2 '18 at 16:28
OK, it means that you have to
createNotificationChannel
with audioAttributes
then it would work.– John_J
May 22 '18 at 12:37
OK, it means that you have to
createNotificationChannel
with audioAttributes
then it would work.– John_J
May 22 '18 at 12:37
where have u defined notificationSoundUri
– Panache
May 23 '18 at 7:16
where have u defined notificationSoundUri
– Panache
May 23 '18 at 7:16
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50062576%2fandroid-oreo-does-not-play-custom-sound-for-notification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4UQ XffYwnzu18rXt0,7dnBQcAXninL8TbLnkH,XWp34tl4wVNa86VgzDI8syy nJXLI Y6uAeTGx XQKtfG7r6E K2jxTu
Does it work for API below 26?
– Pankaj Kumar
Apr 27 '18 at 12:48
Look into stackoverflow.com/questions/46019496/…
– Pankaj Kumar
Apr 27 '18 at 12:52
Dear Pankaj, its using ringtone manager which I dont wanna use because of requirements.
– Lion Heart
Apr 27 '18 at 12:55
Check this
– Pravin Divraniya
Apr 27 '18 at 13:01
No that didnt help as well. My OS version is 8.1.0
– Lion Heart
Apr 30 '18 at 7:08