Problem: I want to increase the volume of notifications here I am using STREAM_ALARM as the notification comes in the background. It's working while the application is in the foreground. but it doesn't work in the background.
Does anyone know how to do that? I have already added the Audio Manager but when the Application is in the background it doesn't increase the volume. Please provide me a solution for this. If you need any other code I have written I can provide that for reference
The code I have provided below is in MainActivity.class and i am calling this function in the onCreate method. Plus onNewIntent()
private void sendNotification(String title, String message) {
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM,
audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),
AudioManager.FLAG_SHOW_UI);
String channel_id = getString(default_notification_channel_id);
String channel_name = getString(default_notification_channel_name);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Uri notification_sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.i_phone_mix);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.putExtra("message", message);
intent.putExtra("title", title);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Log.d(TAG, "Alert Sound Value" + notification_sound);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel_id)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(notification_sound)
.setContentIntent(pendingIntent)
.setContentInfo(title)
.setLargeIcon(icon)
.setColor(Color.RED)
.setLights(Color.RED, 1000, 300)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setShowWhen(true)
// .setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.mipmap.ic_launcher);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.setVolumeControlStream(AudioManager.STREAM_ALARM);
setupChannels(notificationManager);
}
notificationManager.notify(1, notificationBuilder.build());
startActivity(intent);
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupChannels(NotificationManager notificationManager) {
String adminChannelName = getString(default_notification_channel_name);
String adminChannelDescription = getString(default_notification_channel_name);
String channelid = getString(default_notification_channel_id);
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
Uri notification_sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.i_phone_mix);
NotificationChannel adminChannel;
adminChannel = new NotificationChannel(channelid, adminChannelName, NotificationManager.IMPORTANCE_HIGH);
adminChannel.setDescription(adminChannelDescription);
adminChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
adminChannel.enableLights(true);
adminChannel.setLightColor(Color.RED);
adminChannel.enableVibration(true);
adminChannel.canBypassDnd();
Log.d(TAG, "Alert Sound Value2" + notification_sound);
AudioAttributes audio_attribute = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
int vol = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
Log.d(TAG, "Notification Volume" + vol);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM,
audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),
0);
adminChannel.setSound(notification_sound, audio_attribute);
if (notificationManager != null) {
notificationManager.createNotificationChannel(adminChannel);
}
}