Reading this https://firebase.google.com/docs/cloud-messaging/http-server-ref似乎它应该自动管理(如果你没有使用通知生成器 https://github.com/firebase/quickstart-android/blob/a0e8c27bb60f82ac3dcae6ff1e3926d3b0ea4a69/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/java/MyFirebaseMessagingService.java#L47-L90)在Android上,但你必须指定.mp3
也扩展并把它放进去notification
场而不data
one..
"sound": "alarm.mp3"
iOS
在引擎盖下的行为非常不同,但您可以通过设置来使用自定义声音sound:
通知负载中也包含字段。反正.mp3
不是有效的 APN 通知文件格式,您还需要指定文件扩展名。
"sound": "filename.caf"
Follow 苹果文档 https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SupportingNotificationsinYourApp.html为了为您的应用程序伪造您的自定义声音文件。
mp3 不是有效格式
准备自定义警报声音
本地和远程通知可以指定自定义警报声音
通知送达时播放。您可以将音频打包
aiff、wav 或 caf 文件中的数据。因为他们是由
系统声音设施,自定义声音必须是以下之一
音频数据格式:
-
Linear PCM
-
MA4 (IMA/ADPCM)
-
µLaw
-
aLaw
将自定义声音文件放入您的app bundle
或在Library/Sounds
应用程序容器目录的文件夹。风俗
声音一定是30秒以内播放时。如果自定义声音是
超过该限制,将播放默认系统声音。
您可以使用afconvert
转换声音的工具。例如,要
转换16位线性PCM系统声音Submarine.aiff
to IMA4
音频在一个CAF file
,在终端应用程序中使用以下命令:
afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v
例如转换您的mp3
文件在一个caf
您可以在终端中输入的文件:
afconvert -f caff -d LEI16 alarm.mp3 alarm.caf
读这个doc https://firebase.google.com/docs/cloud-messaging/http-server-ref为了深入了解所有通用和特定的通知有效负载字段。
UPDATE
我已经测试了 Android 部分,我可以确认将您的.mp3
文件输入res/raw/
文件夹中声音按照记录和预期播放。
这是我的通知负载:
{
"to" : "my_device_token",
"collapse_key" : "type_a",
"priority" : "high",
"notification" : {
"body" : "Test Notification body for custom sound {{datestamp}}",
"title": "Custom sound alert.mp3",
"sound": "alert.mp3"
}
}
转换后我也测试了iOS版本.mp3
文件到.caf
以这种方式文件:
afconvert -f caff -d LEI16 alert.mp3 alert.caf
相同json
具有不同文件名的有效负载有效:
{
"to" : "my_device_token",
"collapse_key" : "type_a",
"priority" : "high",
"notification" : {
"body" : "Test Notification body for custom sound {{datestamp}}",
"title": "Custom sound alert.mp3",
"sound": "alert.caf"
}
}
请记住将该文件添加到您的main bundle
.
如果应用程序终止或在后台运行,则该方法有效。
如果您想在应用程序位于前台时显示警报并播放声音,则必须在onMessage
有人已经告诉过你的事件here https://stackoverflow.com/questions/53531387/firebase-push-notification-with-custom-sound-flutter,或者您可以使用平台渠道 https://flutter.io/docs/development/platform-integration/platform-channels在这里构建您自己的通知通知生成器 https://developer.android.com/reference/android/app/Notification.Builder在 Android 上和联合国通知中心 https://developer.apple.com/documentation/usernotifications/unusernotificationcenter?language=objc在 iOS 上(例如)。
UPDATE
此问题已得到解决。看here https://github.com/FirebaseExtended/flutterfire/issues/523#issuecomment-722240457官方评论:
大家好????
作为我们路线图的一部分(#2582 https://github.com/FirebaseExtended/flutterfire/issues/2582)我们刚刚发布了完整的返工版
firebase_messaging 插件旨在解决这个问题和许多其他问题
问题。
如果可以的话,请尝试开发版本(请参阅迁移指南 https://firebase.flutter.dev/docs/migration/用于升级和更改),如果您有任何反馈,请加入
在讨论中here https://github.com/FirebaseExtended/flutterfire/discussions/4023.
考虑到返工的范围,我将继续并关闭此项目
有利于尝试最新插件的问题。
谢谢大家????