在 flutter_local_notifications 中添加 mp3 声音

SOU*_*ADH 5 notifications dart flutter

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

Muh*_* NS 9

在 android 项目中添加 raw 文件夹:android>app>src>main>res> New Android Resource Directory > Select Raw > add your sound

Result Folder :
- android
- app
  - src 
    - main
      - res
        - raw
            lawgo_sound_notification.mp3
Run Code Online (Sandbox Code Playgroud)

这里的图片:[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);
Run Code Online (Sandbox Code Playgroud)

添加 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"**
Run Code Online (Sandbox Code Playgroud)


小智 5

在文档中,有很多关于如何处理自定义声音的信息。您需要在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);
Run Code Online (Sandbox Code Playgroud)

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