Flutter本地通知自定义声音不起作用(路径问题)

Pat*_*nta 12 notifications flutter


我想在 Android 端为本地通知设置自定义声音。我在Android项目的res文件夹中创建了一个raw文件夹。这是我将alarm.mp3 文件路径放入通知中的代码部分。

var androidPlatformChannelSpecifics =
                    new AndroidNotificationDetails(
                        "$id",
                        not.columnValue + not.deveui,
                        not.columnValue + not.deveui,
                        sound: "@raw/alarm",
                        importance: Importance.Max,
                        priority: Priority.High);
Run Code Online (Sandbox Code Playgroud)

其实还是用默认的系统声音。

如果我输入@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'),已解决

wah*_*jus 25

感谢 sunnyque 提供了很棒的解决方案,但这就是我如何将其实现到我的 fcm 中,

首先:您必须设置“channel_id”,“playSound:true”,“sound:RawResourceAndroidNotificationSound('yourmp3files.mp3')”在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,
    );
Run Code Online (Sandbox Code Playgroud)

然后,将 mp3 文件添加到res/raw/yourmp3files.mp3 添加 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"
    />
Run Code Online (Sandbox Code Playgroud)

完成了,现在您可以在应用程序中测试 fcm 自定义声音,执行以下操作

卸载最后一个版本

扑干净

扑扑酒吧得到

颤动构建apk

颤动运行——释放

tada,告诉我它是否有效,呵呵

我希望这对将来的人有帮助

  • 删除 RawResourceAndroidNotificationSound 中的“.mp3”解决了我的问题 (5认同)