我正在构建一个带有 firebase 通知的 flutter 应用程序。但是,我需要自定义声音来进行通知。我已经在AndroidNotificationDetails下实现了
playSound: true, sound: RawResourceAndroidNotificationSound('notification'),
并将mp3文件复制到正确位置的raw文件夹中,但没有用。有没有人有办法解决这个问题?
将此声音键添加到您的通知负载中
{
"to" : "AsS23rH5543:CI2k_AsS23rH5543CIZvvDMAsS23rH5543P1...",
"notification" : {
"body" : "your body",
"title" : "Your Title",
"icon" : "your icon",
"sound" : "default"
}
}
如果您需要更多帮助,请参阅此链接
首先创建一个额外的通知通道
// channel for default notifications
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'wave_remote_notifications', // id
'Wave messages and updates', // title
description: 'Primary channel for notifications and messages in wave app',
importance: Importance.max,
);
// channel for custom sound
const AndroidNotificationChannel channelAudio = AndroidNotificationChannel(
'wave_remote_notifications_priority', // id
'Wave messages and updates on priority', // title
description: 'Primary channel for notifications and messages in wave app',
sound: RawResourceAndroidNotificationSound('mario'),
importance: Importance.max,
);
// if you use flutter_local_notification
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channelAudio);
你可以看到我们已经添加了
RawResourceAndroidNotificationSound('mario')
现在将 mp3/wav 文件添加到res/raw/yourmp3files.mp3
现在通知将起作用,但仅适用于调试模式,因为您添加的此文件不会包含在发布模式中
因此,之后您需要在
res/raw/keep.xml
中添加keep.xml,以将您的mp3files.mp3包含到构建中
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@raw/yourmp3files"
/>
您必须将声音添加到 XCode 中。您只需将音频文件(.caf 或 .wav 支持的格式)拖放到 XCode 中的根目录(通常称为 Runner for Flutter)
仅此而已
最后一步
{
"to": "f31vmSf8Qy2N57_33C9cf3:APA91bHBaz7yyRrKQTr_25mLNK2OVlBjdMqM8ux8kQwaeQcsXE6XnOhio7Tp5-CABTFTSbV1vBqqfIVj9Qd9eUdqNi9oojq8FdOWI-l1CU9Xw16-Xb4ZHVTycOM7nIRudIAmMkU6fjbm",
// "collapse_key": "type_a",
"priority": "high",
"notification": {
"title": "New visitor request",
"body": "Answer to accept",
"sound": "mario.wav",
"android_channel_id": "wave_remote_notifications_priority"
},
"data": {
"title": "New visitor request",
"body": "Answer to accept ",
"sound": "mario",
},
"content_available": true,
"apns": {
"payload": {
"aps": {
"mutable-content": 1,
"content-available": 1
}
}
}
}
现在一切就绪,应该可以工作了