Flutter local notification custom sound doesn't work (path issue)

百般思念 提交于 2020-04-18 02:58:13

问题



I want setting up a custom sound for my local notification in the Android side. I created a raw folder in the res folder of Android project. This is the part of my code which I put my alarm.mp3 file path in the notification.

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

Actually it still sounds with the default system sound.

If I put @raw/alarm.mp3, I got the following exception: 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)


回答1:


You don't need to put '@raw/' to path, plugin is already taking sounds from it (according to plugin source: FlutterLocalNotificationsPlugin.java function retrieveSoundResourceUri) so remove it.

also check that sound file is included in build, ex. by creating keep.xml in raw folder with following content

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@drawable/*,@raw/alarm" />

that might help



来源:https://stackoverflow.com/questions/59783904/flutter-local-notification-custom-sound-doesnt-work-path-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!