【问题标题】:Add mp3 sound in flutter_local_notifications在 flutter_local_notifications 中添加 mp3 声音
【发布时间】:2019-12-13 14:34:20
【问题描述】:

如何在flutter_local_notification中添加自定义mp3声音,有添加自定义铃声的功能,可惜没有文档或示例。

【问题讨论】:

    标签: flutter dart notifications


    【解决方案1】:

    在 android 项目中添加 raw 文件夹: android>app>src>main>res> 新的 Android 资源目录 > 选择 Raw > 在此处添加你的声音

    Result Folder :
    - android
    - app
      - src 
        - main
          - res
            - raw
                lawgo_sound_notification.mp3
    

    图片在这里:[1]:https://i.stack.imgur.com/EHquv.png

    实现您的本地通知代码:

    var androidPlatformChannel = new AndroidNotificationDetails(
        "your_channel_id", "name", "desc_channel",
        sound: RawResourceAndroidNotificationSound('lawgo_sound_notification'),
        playSound: true,
        importance: Importance.Max,
        priority: Priority.High);
    

    添加 onResume、OnLaunch 支持,当应用在后台通知负载上运行时应设置为:

    { "notification" : {
         "body" : "Body of Your Notification",
         "title": "Title of Your Notification",
         "android_channel_id": "your_channel_id"
     },
     "data" : {
         "body" : "Body of Your Notification in Data",
         "title": "Title of Your Notification in Title",
         "click_action": "FLUTTER_NOTIFICATION_CLICK",
     }
    }
    
    **important should add "android_channel_id": "default_notification_channel_id"**
    

    【讨论】:

    • 我使用了这种方法,但我的通知在某些设备上没有响铃。并且自定义声音有效。
    【解决方案2】:

    在文档中,有很多关于如何处理自定义声音的信息。您需要在android文件夹中配置资源并使用config设置声音。此外,您可能需要检查。如何。使用通知渠道。在源代码中,有一个示例文件夹,其中有演示:

    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'your other channel id',
        'your other channel name',
        'your other channel description',
        icon: 'secondary_icon',
        sound: 'slow_spring_board',
        largeIcon: 'sample_large_icon',
        largeIconBitmapSource: BitmapSource.Drawable,
        vibrationPattern: vibrationPattern,
        enableLights: true,
        color: const Color.fromARGB(255, 255, 0, 0),
        ledColor: const Color.fromARGB(255, 255, 0, 0),
        ledOnMs: 1000,
        ledOffMs: 500);
    

    您可以在https://github.com/MaikuB/flutter_local_notifications/blob/master/flutter_local_notifications/example/lib/main.dart查看完整示例

    【讨论】:

    • @Richardo Markiewicz 如何添加声音:在特定日期和时间显示每周通知中的“slow_spring_board”。
    • @SOUGATRIYADH 您必须将其添加为 android/app/src/main/res/raw/ 中的 android 资源文件,该文件应称为 slow_spring_board.mp3 (或您传递的同名以“.mp3”结尾的“声音”
    • 例如,如果我的通知是从网络上显示的,我如何添加来自互联网的图像和来自互联网的声音?文件必须在 res/raw 文件夹中吗?
    • 另外,请确保您的音频文件以小写形式输入,否则您将收到构建错误。
    • “有很多关于如何处理自定义声音的信息......”不,没有。 @Ricardo Markiewicz
    猜你喜欢
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    相关资源
    最近更新 更多