I'm working on a VoIP app and when a call comes in, the ringtone is not audible on at least a Samsung A20e and a Samsung A71 device running on Android 11. Unfortunately, this info came from a few users who say they experience the issue and the issue doesn't arise on all the phones I have access to, so I'm not able to look in the logs myself.
To get the ringtone URI, the following code is used:
val uri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE)
Then that URI is used on the notification channel:
val attributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
notificationChannel.setSound(uri, attributes)
And the URI is used once again in the method setSound of the NotificationCompat.Builder to generate the actual notification.
I tried to reproduce the issue on an emulator and changed the values in setUsage and setContentType a lot, but nothing seemed to have impact. I also reinstalled the app on every change, to make sure a new notification channel would be created.
Then I read somewhere on the internet that ringtones never work on an emulator. However, I do have an emulator on which the ringtone is actually audible, so I'm not so sure that's true, at least not for every configuration.
Lastly I should add that the ringtone also wasn't silent on my original emulator when I added the following line:
RingtoneManager.getRingtone(context, uri).play()
However, I'm not actually able to use that piece of code, since I need to attach the ringtone to a notification (channel).
Does anyone have experience with a same type of issue? Is there a problem with the settings of my notification or the notification channel? Is this just a Samsung issue? I hope someone can help me out.
I'll answer my own question. Turns out the client didn't describe the issue thorough enough and it turned out to be an easily solvable issue when he provided us with more information. The ringtone would be audible when the notification volume of the phone was on and the ringtone volume of the phone was off. However, when the notification volume was off and the ringtone volume was on, the ringtone wouldn't be audible. So the two of those got switched around, which got fixed by replacing AudioAttributes.USAGE_NOTIFICATION with AudioAttributes.USAGE_NOTIFICATION_RINGTONE.
Related
I have an app that can emit custom notification sounds triggered by calendar events. This worked perfectly well on my old phone, but on my new Galaxy S21, it emits the system default notification sound instead of the custom notification sound. I tried it on the emulator with an AVD running Android 11, which is the version that the S21 claims to be running, and it works correctly. The custom sound exists on the S21 and is playable using the music player.
Android Notification sound defaulting back instead of playing custom sound is a very old question reporting a similar problem and the answer there suggested rebooting the phone, which I tried and it didn't help.
The code which emits the notification looks like this:-
private void emitNotification(String smallText, String bigText, String path) {
RemoteViews layout = new RemoteViews(
"uk.co.yahoo.p1rpp.calendartrigger",
R.layout.notification);
layout.setTextViewText(R.id.notificationtext, bigText);
DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
layout.setTextViewText(R.id.notificationtime,
df.format(System.currentTimeMillis()));
Notification.Builder NBuilder
= new Notification.Builder(this)
.setSmallIcon(R.drawable.notif_icon)
.setContentTitle(smallText)
.setContent(layout);
if ((path != null) && !path.isEmpty())
{
Uri uri = new Uri.Builder().path(path).build();
AudioAttributes.Builder ABuilder
= new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION);
NBuilder.setSound(uri, ABuilder.build());
}
// Show notification
NotificationManager notifManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(notifyId++, NBuilder.build());
}
The entire source is available on GitHub at
rparkins999/CalendarTrigger branch fix3.3
I have included a built APK in the git tree if anyone would like to try it on another phone. You can also build from the sources: you will need a ../Keys/Keystore.properties file relative to the project root directory since I'm not publishing my private signing key.
To demonstrate a custom notification sound, run the program, give it all the permissions it wants, touch 'NEW EVENT CLASS', give the class a name, and touch Create: you should then see a screen consisting of buttons that invoke activities to define the characteristics of the class.
Touch 'Event starts action(s) for class ...' and you should see a screen offering various actions that the program can take (not all of which work). Touch 'Show a notification', then 'Play a sound', then 'Browse for a sound file': you should then see a file browser.
When you choose a file it will take you back to the previous screen, but 'Browse for a sound file' will have been replaced by the path to the sound file. Touch the back button to return to the screen of buttons, and then touch 'Immediate event of class ...'. It should play the sound.
On the emulator it does, but on the S21 it plays the default sound instead.
As set up, the class you created will emit a notification on every calendar event. You can stop that by deleting the class or setting some conditions for a calendar event to be in the class: there are buttons for both of these actions on the class definition screen.
I don't know whether this problem is a bug in the S21, or caused by some obscure Setting that I haven't been able to find. Any help would be appreciated, especially information as to whether the app does or doesn't work correctly on other phones running Android 11. Of course, a fix or a workaround would be better still!
I just installed the latest S21 update. Now the problem is worse! I don't get any sound at all from my notifications with custom sounds.
The problem in this particular case was the audio format in the file (.m4a). The music player on the S21 can play it, and the notification sound player in my old phone could play it, but the notification player on the S21 apparently couldn't play it.
I used FFmpeg on my PC to convert from .m4a to .mp3, which the S21 notification player can play, and it now works.
So it isn't safe to assume that if the phone can play a sound format with its music player, the Notification logic can also handle it.
I am using service workers. I need it for push notifications. What I want to achieve is that when I recieve notification, it should play a sound or vibrate at least.
self.registration.showNotification(title, {
body: body,
icon: './assets/images/icons/icon_144x144.png',
vibrate: [200, 100, 200, 100, 200, 100, 200],
});
I am having this piece of code upon receiving a notification.
Problem 1: No vibration happens at all. Then I went ahead of a little bit debugging.
https://googlechrome.github.io/samples/notifications/vibrate.html This DOESN'T vibrate my phone.
https://googlechrome.github.io/samples/vibration/index.html This DOES vibrate my phone.
Why doesn't it vibrate from the first link? I am testing all this on android chrome.
Problem 2: Looks like on firefox, it does have a default sound. Why doesn't google have the same sound? It just goes silent. No way I can play sound ? at least I want to vibrate it...
As shown here, the vibrate property of showNotification() is no longer supported on Android devices from Android O onward, regardless of the Chrome version, which is likely why the first link doesn't work for you.
However, you can use the vibrate() method (which is what the second link is doing), which is supported on Chrome versions after version 32. However note that after Chrome 60, this method will only work in response to a user gesture. (see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/vibrate) for more details.
Adding some more history here:
According to the Chromium devs:
Unfortunately we've had to deprecate this starting Android O a few years ago - introduction of notification channels means that attention drawers (vibration, sound and all) are now per channel rather than per notification, which Chrome doesn't enable developers to configure.
Addressing that has zero interest from other vendors unfortunately.
My code used to successfully silence incoming calls by simply using setRingerMode, but ever since Android Pie, it's just not working anymore. I had tested the built-in DND mode, and it seemed to not be working either. But if that's true, it's working now, but my code still isn't.
Is there something additional necessary for this to work now? Android Pie does keep a separate mode from DND for ring, vibrate, and silence for ringer sounds, but I haven't been able to find figure out why my code isn't working anymore.
Update:
I'm using the following code:
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Update (1-23-19):
My understanding is I can't change the notification channel of another app, like the texting or calling app. Does anybody know any differently?
Some users are reporting on Huawei EMUI 8.0 Oreo that the Notification Channel does not have the option to change tone "Sound" / "Notification Tone" from the app!
As of the official docs the developer can't change the settings anymore.
So how can I add an option for Huawei phones to change the "Notification Tone" again?
And does anyone know why the hell Huawei removed this feature?
I don't find official docs from Huawei how we can now let the user change the notification tone.
Offical docs I am referring:
https://developer.android.com/reference/android/app/NotificationChannel.html
https://developer.android.com/reference/android/app/NotificationChannel.html#setSound(android.net.Uri, android.media.AudioAttributes)
We ran into the same issue recently.
It is not a nice solution, but WhatsApp is doing the same.
Basically we show a ringtone selection inside our app and then delete and recreate the notification channel with a new channel id and the selected ringtone uri. You can copy most settings made to the channel to the new one except 'do not disturb'.
Like I said it is not a nice solution and I don't know what will happen if the channel is recreated a lot. But hopefully the ringtone is not changed too often.
Note: The notification settings screen displays the number of deleted channels, as a spam prevention mechanism.
Faced with same issue on chinese devices. Firstly, i have same solution like describe #Devenias. How it works in system: when you're defining a new channel, NotificationService save this channel in xml, after you changed it, it still contains in this xml. So on a new change of channel, NotificationService will check if it have a channel with a same name, and just retrieve it. So i make new channel with a new settings all the time, when user change vibration or ringtone in app. Also it works like a cache, just make unique channel name for pair<ringtone, vibration>. This solution is pretty hacky, since it works good on Honor's, Huawei's, Samsung's devices and Xiaomi Mi A1, but it have been crashing NotificationService with NullPointer in SystemUI on Xiaomi Mi Mix 2 (device make soft reboot, if SystemUI service crashed), so don't use this solution.
So nowadays a safe workaroud is to play sound and vibration manually.
In my app I have just built in a ringtone preference so that the user can select what type of sound they would like to receive when they get notifications, however the problem Im running into here is that the list that is presented gives both ringtone for phone calls and notification sounds for messengers etc, I want just the notification sounds for messengers, If a user selects a ringtone for a phone call my app could become very annoying very fast. Is there a way to filter the notification sounds from the ringtone sounds? I see other apps doing this but Im unable to find a way to do it online, any help would be massive thanks!
Filter the list by chossing only audio which is a notification sound. When you query for audio files, you would need to check it in the where clause that : MediaStore.Audio.AudioColumns.IS_NOTIFICATION is non-zero ( and all other fields such as IS_ALARM is zero). You will find more info about the table colums i am referring to here: http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html
Nevermind I figured it out, for ringtone type in the xml I had it set to all, I simply changed it to android:ringtoneType="notification" and that solved it, there also other ringtone types for alarm and all of that, hope this helps someone else who encounters the same problem!