var androidPlatformChannelSpecifics =
new AndroidNotificationDetails(
"$id",
not.columnValue + not.deveui,
not.columnValue + not.deveui,
sound: "@raw/alarm",
importance: Importance.Max,
priority: Priority.High);
实际上,它仍然会使用默认的系统声音。
如果输入@raw/alarm.mp3
,则会出现以下异常:Unhandled Exception: PlatformException(INVALID_SOUND, The resource %s could not be found. Please make sure it has been added as a raw resource to your Android head project., null)
注意:声音:RawResourceAndroidNotificationSound('alarm'),已解决
答案 0 :(得分:3)
感谢 sunnque 提供了很棒的解决方案,但这是我在 fcm 中实现它的方法,
首先:你必须设置"channel_id","playSound: true", “声音:RawResourceAndroidNotificationSound('yourmp3files.mp3')”: adding specific channel on flutter_local_notification
Future displayNotification(Map<String, dynamic> message) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'you_can_name_it_whatever',
'flutterfcm',
'flutterfcm',
playSound: true,
sound: RawResourceAndroidNotificationSound('yourmp3files.mp3'),
importance: Importance.max,
priority: Priority.high,
);
然后,将 mp3 文件添加到 res/raw/yourmp3files.mp3 adding mp3
之后,您需要在 res/raw/keep.xml 中添加 keep.xml 以将 yourmp3files.mp3 包含到构建 keep.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@raw/yourmp3files"
/>
大功告成,现在您可以在您的应用中测试 fcm 自定义声音了,做
<块引用>卸载上次构建
<块引用>扑通干净
<块引用>颤动酒吧获得
<块引用>flutter build apk
<块引用>颤动运行 --release
tada ?,告诉我它是否有效,呵呵
我希望这对未来的人有帮助?
答案 1 :(得分:0)
您无需将'@ raw /'放置到路径中,插件已经在从中获取声音(根据插件来源:FlutterLocalNotificationsPlugin.java函数retrieveSoundResourceUri),因此将其删除。
还要检查声音文件是否包含在内部版本中。通过在原始文件夹中创建具有以下内容的keep.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@drawable/*,@raw/alarm" />
可能有帮助