阅读此文档,似乎应该在 Android 上自动管理(如果您没有使用通知构建器),但您还需要指定 .mp3
扩展名并将其放在notification
字段中而不是data
字段中。
"sound": "alarm.mp3"
iOS
的内部行为非常不同,但您可以通过在通知负载中设置 sound:
字段来使用自定义声音。 无论如何,.mp3
不是有效的 APN 通知文件格式,您还需要指定文件扩展名。
"sound": "filename.caf"
请遵循Apple文档来为您的应用程序制作自定义声音文件。
mp3不是有效的格式。
准备自定义警报声音
本地和远程通知可以指定在传递通知时要播放的自定义警报声音。您可以将音频数据打包成aiff、wav或caf文件。由于它们是由系统声音设施播放的,因此自定义声音必须处于以下某种音频数据格式之一:
线性PCM
MA4(IMA / ADPCM)
μ律
aLaw
将自定义声音文件放置在您的
应用程序包
或应用程序容器目录
下的Library/Sounds
文件夹中。当播放时,自定义声音必须不超过30秒。如果自定义声音超过该限制,则播放默认的系统声音。您可以使用
afconvert
工具来转换声音。例如,要将16位线性PCM系统声音Submarine.aiff
转换为以CAF文件
中的IMA4
音频格式,请在Terminal应用程序中使用以下命令:
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
阅读此文档,以深入了解所有通用和特定的通知负载字段。
更新
我已测试了 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"
}
}
请记得将文件添加到您的“主要包”中。
如果应用程序已终止或在后台运行,则可以使用该方法。
如果您想在应用程序处于前台时显示警报并播放声音,则必须在“onMessage”事件上进行管理,就像有人已经告诉您在这里一样,或者您可以使用一个平台通道在这里自己构建具有Notification.Builder的通知(Android)和UNNotificationCenter(iOS)。
更新
此问题已解决。请参见官方评论here:
大家好
作为我们路线图的一部分(#2582),我们刚刚发布了一个完整的重制firebase_messaging插件,旨在解决这个问题和许多其他问题。
如果可能的话,请尝试dev版本(请参见迁移指南以进行升级和更改),如果您有任何反馈,请加入在此处的讨论here。
鉴于重制的范围,我将关闭这个问题,以尝试最新的插件。
谢谢大家
对我来说,我正在使用flutter_local_notifications来创建通知渠道。
包含此函数(可以创建多个通知渠道)
Future<void> _createNotificationChannel(String id, String name,
String description, String sound) async {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var androidNotificationChannel = AndroidNotificationChannel(
id,
name,
description,
sound: RawResourceAndroidNotificationSound(sound),
playSound: true,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(androidNotificationChannel);}
在initState中调用该函数:(这将创建2个通知渠道)
_createNotificationChannel("channel_id_1", "channel_name", "description", "alert");
_createNotificationChannel("channel_id_2", "channel_name", "description", "alarm");
请记得将alert
和alarm
的文件以 .mp3
的文件格式,保存在res/raw
目录下。
使用此有效负载:
{
"notification": {
"title": "My First Notification",
"body": "Hello, I'm push notification"
},
"data": {
"title": "My First Notification"
},
"android": {
"notification": {
"channel_id": "channel_id_1"
}
},
"to": "device_token"}
"notification": {
"body": "Test notification",
"title": "Test Test Test",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"sound": "your_custom_sound.wav"
"android_channel_id": "channel_id_youcreated",
},
'to':
"",
},
在尝试了这个 Android 解决方案之后,我仍然无法使用自定义声音进行更改。您需要使用 .ogg 文件类型来解决此问题。更改为 .ogg 文件类型后,我可以使用自定义声音进行更改。谢谢。