我正在从 firebase 向我的 Android 应用程序发送推送通知。但是当我的应用程序在后台时,firebase onMessageReceived 方法不会被调用,而是 firebase 向系统发送通知以在系统托盘中显示通知。 通知出现在系统托盘中,但没有通知声音,即使我在系统设置中允许我的应用程序发出通知声音。
当收到来自 firebase 的通知时,我可以做些什么来播放通知声音。
这就是我从 firebase 向我的应用发送通知的方式 Blogpost link .
最佳答案
在通知的通知载荷中有一个声音键。
从官方文档看它的用途是:
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/.
例如:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon",
"sound" : "mySound"
}
}
如果你想使用设备的默认声音,你应该使用:"sound": "default"
。
请参阅此链接以获取有效负载中所有可能的键: https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
对于那些不知道 Firebase 在应用处于后台时以不同方式处理通知的用户。在这种情况下,不会调用 onMessageReceived 函数。
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.
关于android - 从android中的firebase发送通知时没有通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37959588/