I am using a NotificationChannel to define my app's notification. I set it's sound with the following code :
AudioAttributes.Builder constructeurAttributsAudio=new AudioAttributes.Builder();
constructeurAttributsAudio.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT);
canalNotification.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + contexte.getPackageName() + "/raw/cloche"),constructeurAttributsAudio.build());
When the notification appears, the sound is correctly emitted, but it's volume is set at the maximum and doesn't take into account the sound level set for notifications by the user. Does anyone know how I can have my app set the notification sound level to the value choosen by the user?
Edit #1 : if I copy the code to execute it in the app's body (triggered by a Button click) instead of in the onReceive method of my BroadcastReceiver, the notification sound is correctly emitted at the sound level chosen by the user for notifications.
Edit #2 : strangely, the notification sound level is correct when the app is executed on the emulator! Could the reason be a parameter in the phone's configuration? (They both run under Android 9).
Uninstalling and reinstalling the app solved the problem!
I use the following code, hopefully useful for you too:
private void setvolume(int volume)
{
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
Uri notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer player = MediaPlayer.create(getApplicationContext(), notification);
player.start();
}
Related
Context
I am working on an app that uses FCM. The use of this application is to alert a user of an event that is occurring (such as an alarm system). In view of the alarm nature of the notification, it is essential that a sound is played when receiving a notification even if the smartphone is in silent or vibrate mode.
Question
Is there a way to achieve this described behavior for all smartphone modes (silent, vibrate, sound) ?
What I've tried
As I am working with API26> I created a notification channel to have the highest priority which is Max Priority,
I've set the notification channel to bypass Do Not Disturb mode like so:
notificationChannel.SetBypassDnd(true);
Obviously it only affects the Do Not Disturb mode and absolutely not what I want,
In the notification builder, I've set the notification priority to Max and the category to Alarm:
.SetPriority(NotificationCompat.PriorityMax)
.SetCategory(NotificationCompat.CategoryAlarm);
Reading the Android documentation, this feature is also related to Do Not Disturb Mode.
I am actively looking for a solution to this problem, but at this point I'm a bit stuck.
Any suggestions ?
I've read about a full screen intent in the Android documentation but it's not written that a sound will fire if the smartphone is in silent mode.
Maybe there is a way to create a service that rings when the notification arrives? But this service has to be running all the time, which isn't really a good design idea.
If you guys have any idea, any remarks or suggestions, i'd be grateful to read them !
I believe you need to set priority for your notification.
private fun setPriorityForAlarmNotification() {
if (notificationManager.isNotificationPolicyAccessGranted) {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val policy = NotificationManager.Policy(PRIORITY_CATEGORY_ALARMS, 0, 0)
notificationManager.notificationPolicy = policy
}
}
}
As I can see you setCategory for your notification builder is NotificationCompat.CategoryAlarm.
However, in order to set this priority, you need this permission on your manifest
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
And request permission if needed
fun requestNotificationPolicyPermission() {
val notificationManager = activity!!.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (!notificationManager.isNotificationPolicyAccessGranted) {
val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
startActivityForResult(intent, REQUEST_CODE_NOTIFICATION_POLICY)
}
}
This solution should work absolutely. Hope this can help you :D
I'm following this guide to configure my NotificationChannels https://developer.android.com/training/notify-user/channels.html#java
But when I open the settings screen with this code:
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId());
startActivity(intent);
There is no tune preference. I can only turn On/Off the sound. Do I need some additional configuration of my channel, or is it impossible to set custom tune of notification?(I know that I can play the tune manually when notification pops up, but I really don't like this solution)
Channel code:
public String createChannel() {
NotificationChannel channel = new NotificationChannel(INCOMING_CHAT_CHANNEL_ID, INCOMING_CHAT_CHANNEL_TITLE,
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(INCOMING_CHAT_CHANNEL_DESCRIPTION);
getNotificationManager().createNotificationChannel(channel);
return channel.getId();
}
And then I use returned channel Id:
Notification sound setting will only appear for higher priority notifications.
Try to change your Channel behaviour to first (Make sound and pop up screen) or second (Make sound) option.
Please try to post the code where you are creating the Notification channel.
This is a very common question, but I did not find an answer to it. I created app with Xamarin.Android. My app creates a locale notifications (with time delayed), it works except android 4.4(on emulator and device).
My device: Samsung GT-I9195, Android 4,4,2
This is my code create notification (in BroadcastReceiver):
var builder = new NotificationCompat.Builder(context)
.SetContentTitle(notification.Title)
.SetContentText(notification.Body)
.SetSmallIcon(notification.IconId)
.SetAutoCancel(true);
builder.SetDefaults(NotificationCompat.DefaultAll);
In this case, only vibro works.
I tried other options, for example:
builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Alam));
...
//load embeded sound
Android.Net.Uri alarmUri = Android.Net.Uri.Parse($"{ ContentResolver.SchemeAndroidResource}:{CrossCurrentActivity.Current.AppContext.PackageName}/{Resource.Raw.sound}");
builder.SetSound(alarmUri);
I even tried to launch a media player(in OnReceive).
MediaPlayer mp = MediaPlayer.Create(CrossCurrentActivity.Current.AppContext, alarmUri);
mp.Start();
But all this does not work. Only vibration, without sound. Any ideas?
I'm developing an app that handles sound and vibration of apps' notifications. I am listening to notifications using NotificationListenerService. I am able to turn on or off notification's sound using AudioManager as follows:
// It turns off notification's sound
myAudioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
// It turns on notification's sound
myAudioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
I have also tried to turn on or off notification's vibration with following code but it didn't work on my phone which has Android KitKat 4.4.4
// to turn off notification's vibration
myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
// to turn on notification's vibration
myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
Here's what I did. In my NotificationListenerService, I cancel the original notification, turn off its vibration, and send it out again. Code like this should work:
Integer key = 1;
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Notification n = sbn.getNotification();
if (doINeedToDisableVibration(sbn)) {
cancelNotification(sbn.getKey());
n.defaults &= ~Notification.DEFAULT_VIBRATE;
n.vibrate = null;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
synchronized(key) {
nm.notify(key++, n);
}
}
}
In doINeedToDisableVibration(sbn), either make sure that vibration is on in the notification, or else check that sbn.packageName is NOT the same as your own package name, or you have a danger of generating an endless loop.
I am not sure this will work with all notifications. But it did work with MMS notifications on my HTC One A9 with 6.0.
setVibrateSetting api is deprecated in API 16. As per android developer site,
Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode().
So, you try with Ringer mode to enable/disable vibration.
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
for setting silent mode :
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
For normal mode :
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
For ring vibrate mode:
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
I searched with many terms and sentencing but could not find any information on this whatsoever.
So anyways: How can I get default or currently set music/tone/sound for Alarm (Default app) ?
Say if I wake up every morning with alarm song playing: Song#1 using default alarm app, how can I get that Song#1 in my custom app?
I am trying to create my own alarm app but I don't want to set a tone that user may not like.
Perhaps there is a way to open default alarm tone picker/browser and let user set it in my custom app? Or does all of that need to be custom coded?
If none of above is possible - How can I just get default alarm sound to play in my app?
Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtoneAlarm = RingtoneManager.getRingtone(getApplicationContext(), alarmTone);
ringtoneAlarm.play();