I am sending push notification from firebase to my Android Application. but when my app is in background firebase onMessageReceived method is not called instead firebase send notification to system for showing notification in system tray. notification appears in system tray but no sound for notification even i have allowed notification sound for my app in system settings.
what I can do to play notification sound when notification received from firebase.
This is how I am sending notification from firebase to my app Blogpost link.
How to Add firebase in your Android Application
Step 1, Go to the Firebase console. Under Engage, go to Cloud Messaging. We can use this tool to send out notifications to a specific group, device, or topic. Click on “Send your first message” and enter a title and text for the notification.
Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.
In the notification payload of the notification there is a sound key.
From the official documentation its use is:
Indicates a sound to play when the device receives a notification. Supports default or the filename of a sound resource bundled in the app. Sound files must reside in /res/raw/.
Eg:
{ "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "notification" : { "body" : "great match!", "title" : "Portugal vs. Denmark", "icon" : "myicon", "sound" : "mySound" } }
If you want to use default sound of the device, you should use: "sound": "default"
.
See this link for all possible keys in the payloads: https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
For those who don't know firebase handles notifications differently when the app is in background. In this case the onMessageReceived function is not called.
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload. In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
select advanced options when Write a message, and choose sound activated
this is My solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With