I have created a notification channel and added sound attributes to it.
The issue I am facing is that I am not able to loop the notification sound until the notification has been cancelled. I am using the flag FLAG_INSISTENT, with the NotificationBuilder, which as per the documentation, does the following -
Bit set in the Notification flags field if the audio will be repeated until the notification is
cancelled or the notification window is opened.
This flag plays the sound in the loop but once notification window is opened(the notification panel is dragged down) the notification sound stops. Is there any alternative way to play the ringtone until the notification has been cancelled.
PS- I have explored other options such as MediaPlayer (which requires storage permission to play ringtone in external storage) and Ringtone object(this has the function of setLooping() which was added in API 28 and no alternative for lower SDKs)
Related
Notification sounds are getting suppressed due to Xiaomi sound settings. If Allow sound is toggled off notification sound will not be played. I want to show a popup to the user if this setting is disabled, but I am not able to fetch this setting using Notification manager or Android Manager. How can I fetch this os specific setting so that I can nudge the user?
I am able to play Remote Push Notification sound. But sound stops as soon as the sound file placed in raw folder plays completely.
I want to play sound in repeat mode till there is a user action.
Can't use FLAG_INSISTENT with notification builder as it is a remote notification.
I have one FCM based notification when I am receiving notification I am playing custom mp3 from row folder. But once notification comes ringtone manager started playing mp3 but it doesn't stop.
You can set the sound to the notification whilst building it rather than using RingtoneManager:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSound(soundUri); // Sound to play
When you want to stop your ringtone then Release your media player.
I have solved this issue. Once I am getting notification from FCM I set the sound to null and play custom notification through Ringtone manager. Please note size and length of notification should be small.
Introduction of notification channel allow many apps to remove their notification settings in app and redirect to device settings. Which is quite easy for developers as the framework itself will handle notification sounds.
Incoming call ringtones can be played via Ringtone manager or Media player. We cant play incoming call ringtone using notification because notification sound stops when the notification tray is opened.
Initially i created notification channel with IMPORTANCE_LOW so that there is no sound for incoming call notifications. There may be situation where user can assign sounds to incoming call notification where both sounds are played (sound set by notification channel and Incoming call ringtone which is handled by app).
This is a big problem for all calling apps. Can any one have solution for it ?
There seem to be several other questions related to this topic, but each one I've found has a solution that either doesn't use notifications or doesn't work on recent Android versions. The reason I want to use notifications is to ensure the volume for the sound corresponds to the user's notification volume.
Q: How can I play the default notification sound, that doesn't display anything to the user, and responds to the notification volume level?
One solution, which used to work, was to build a notification but leave off the icon, title, and content. This worked on older Android versions (with an error in the log E/NotificationService: WARNING: In a future release this will crash the app:), but is no longer an option.
Other solutions use MediaPlayer or RingTone, but these have the down-side of different user volume controls which mean the sound level won't match that of displayed notifications. In other words, if the user has their media and ring volume set very low, then these approaches may not be heard.
Some similar questions that don't solve this:
Play notification default sound only (Android)
Android Notification to play sound only
Android Marshmallow sound only Notification?
How to play an android notification sound
This plays the right sound, but not at the notification volume:
Uri uriSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
try {
RingtoneManager.getRingtone(getApplicationContext(), uriSound).play();
} catch (Exception e) {
e.printStackTrace();
}
This is a sound-only notification, but only worked (contrary to documentation) on older Android versions:
Notification notification = new NotificationCompat.Builder(this, "channel_id")
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.build();
An example of this working as I desire is Google's Android Messages app. When the app is open to a conversation thread and a new message is received, the notification sound is played and the sound correctly responds to the notification volume level.