Hi,
I know this has been asked before but I can't find a solution that works for me.
I'm trying to get my app to turn up the volume of the notifications to 100% when a push notification is received but I just can't get this to happen.
I have tried using this solution for Android previously posted on these forums by @Cdn_Euro:
private void setNotificationVolumeToMax() { try { var audioManager = (AudioManager)GetSystemService(Context.AudioService); int max_volume = audioManager.GetStreamMaxVolume(Android.Media.Stream.Notification); audioManager.SetStreamVolume(Android.Media.Stream.Notification, max_volume, 0); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
This works, but only when called at some point along the Activity Lifecycle (OnCreate, OnPause etc.) or when the app is in the foreground.
I need this functionality to happen when the app is in the background to ensure that whenever a notification is received the user will hear it.
I'd really appreciate any help.
It depends on which sort of notifications you sent.
There are two kinds of notifications in FCM:
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
If you are using notification messages, it will be handled by the system notification tray when your application is in the background. So the sound is controlled by the system we can't adjust the volume using the code in your application.
Here is a graph clearly clarify this:
Please use the data messages so that the OnMessageReceived
will always be triggered to create your own notification no matter the application is in the foreground or background.
Here is the format of the data payload:
{ "message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data":{ "Nick" : "Mario", "body" : "great match!", "Room" : "PortugalVSDenmark" } } }
Please notice that if you are using FCM console to send a test message, this will always be a notification type. You need to use the backend to send a data message.
Answers
It depends on which sort of notifications you sent.
There are two kinds of notifications in FCM:
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

If you are using notification messages, it will be handled by the system notification tray when your application is in the background. So the sound is controlled by the system we can't adjust the volume using the code in your application.
Here is a graph clearly clarify this:
Please use the data messages so that the
OnMessageReceived
will always be triggered to create your own notification no matter the application is in the foreground or background.Here is the format of the data payload:
Please notice that if you are using FCM console to send a test message, this will always be a notification type. You need to use the backend to send a data message.
Thank you so much! This is exactly what I needed