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.
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?
My app can play music from the internet. For control, I use a push notification with buttons for pause/play and rewind. I create it via NotificationCompat Builder and set the MediaStyle. On the lock screen, MediaSessionCompat displays a full-screen notification for monitoring. But there is also a duplicate of the first notification. Because of this, I have two notifications on the lock screen.
Perhaps the MediaSession itself should hide the notification by its id or channel? I tried setting VISIBILITY_SECRET to the channel and notification, but it also displayed on the lock screen.
This may be behavior specific to your phone; I would first start by checking what the behavior is on an emulator.
Perhaps you'd like to compare how you're registering your MediaSession in the notification against the Universal Android Music Player Sample. In particular, check if you've set the MediaSession token with the MediaStyle notification using setMediaSession(); you can find how this is accomplished inside UAMP inside UampNotificationManager.
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)
I am trying to turn off and turn on the default notification sound and vibration that is triggered when I receive a notification on my mobile. I am trying to achieve a certain functionality before which I turn off the defaults and after whose completion I would restore the defaults.
Currently I tried setting the STREAM_NOTIFICATION volume to zero and restoring it upon receiving the notification, but it does not work intermittently in some cases.
Is this possible using the Notification class? I am interested in turning off and turning on the default notification sound and vibrate pattern only.
Explanation with code would be greatly appreciated.
There is one very simple solution for it Create one activity for app setting and in your app in which one check box(toggle button) for mute and unmute notification and on button click
getApp().getPreferences().setIsNotitficationMute(isChecked);
and while you are creating notification i.e. in firebase add these code.
if (app.getPreferences().getIsNotitficationMute()) {
notificationSound = null;
} else {
notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
I create a notification in Service (launched from the BroadcastReceiver) this way:
this.notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Foo")
.setContentText("Bar")
.setSmallIcon(R.drawable.logo)
.setContentIntent(pIntent)
.build();
this.initializeNotificationSound(volumne, Uri.parse(melody));
this.initializeNotificationVibration();
notification.flags = Notification.FLAG_INSISTENT;
this.notificationManager.cancelAll();
this.notificationManager.notify(0, notification);
This notification is set when a particular SMS is received. I want to create a notification which will play a defined melody and will not be interrupted by the standard SMS notification.
This functionality works pretty good, however in lollipop, following scenario happens:
SMS is received
My notification is launched
After some time, notification sound is stopped and default SMS ringtone is played
This happens only when ringtone mode is silent. Otherwise, it works as expected, because of the cancelAll() method, which interrupts the SMS notification.
Thanks in advance for your response.
You can not change that. If you notice, when more than one notifications are created at the same time, even from a sole application(e.g. Facebook), the sound they make is played simultaneously. This has to do with the notification management of android system itself. Maybe the problem you are facing happens due to some code changes made to the specific Android system version (API level), maybe to the cancelAll() method. One solution would be to find the appropriate line of code for checking current system version. For example:
if(System_version <= lollipop) {
//code works fine. Keep as it is
}
else{
//find the appropriate method and call it here.
}