Flutter 本地通知自定义声音不起作用(路径问题)
Flutter local notification custom sound doesn't work (path issue)
我想在 Android 端为我的本地通知设置自定义声音。
我在 Android 项目的 res 文件夹中创建了一个原始文件夹。
这是我将 alarm.mp3 文件路径放在通知中的代码的一部分。
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'),已解决
您不需要将“@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" />
可能会有帮助
感谢 sunnyque 提供了很棒的解决方案,但这是我将其实现到我的 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 以将您的 mp3files.mp3 包含到构建中 keep.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@raw/yourmp3files"
/>
大功告成,现在可以在应用中测试 fcm 自定义声音了,做
uninstal last build
flutter clean
flutter pub get
flutter build apk
flutter run --release
tada ,告诉我它是否有效,呵呵
我希望这对以后的人有所帮助
为了完成之前的有用帮助,如果您的应用是在您的声音修改之前创建的,请不要忘记更改您的频道 ID,我不知道为什么但是对于现有频道,声音没有被考虑在内(为此浪费了几个小时,希望它能帮助别人)
我想在 Android 端为我的本地通知设置自定义声音。 我在 Android 项目的 res 文件夹中创建了一个原始文件夹。 这是我将 alarm.mp3 文件路径放在通知中的代码的一部分。
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'),已解决
您不需要将“@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" />
可能会有帮助
感谢 sunnyque 提供了很棒的解决方案,但这是我将其实现到我的 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 以将您的 mp3files.mp3 包含到构建中 keep.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@raw/yourmp3files"
/>
大功告成,现在可以在应用中测试 fcm 自定义声音了,做
uninstal last build
flutter clean
flutter pub get
flutter build apk
flutter run --release
tada ,告诉我它是否有效,呵呵
我希望这对以后的人有所帮助
为了完成之前的有用帮助,如果您的应用是在您的声音修改之前创建的,请不要忘记更改您的频道 ID,我不知道为什么但是对于现有频道,声音没有被考虑在内(为此浪费了几个小时,希望它能帮助别人)