在 flutter_local_notifications 中添加 mp3 声音

Add mp3 sound in flutter_local_notifications

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

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

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

查看完整示例

在 android 项目中添加原始文件夹: android>app>src>main>res> New Android Resource Directory > Select 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"**