简体   繁体   English

如果我们在 flutter 应用程序中进行发布构建,自定义声音将不起作用

[英]Custom sound not working if we make release build in flutter app

Everything is working fine in debug mode and if I run, flutter run --release.在调试模式下一切正常,如果我运行,flutter run --release。 But custom sound is not working if I generate release build and running using that.但是,如果我生成发布版本并使用它运行,自定义声音将不起作用。 I am using flutter.我正在使用 flutter。

here is the code,这是代码,

Future _showNotificationWithSound(title, message) async {
    var vibrationPattern = Int64List(4);
    vibrationPattern[0] = 0;
    vibrationPattern[1] = 1000;
    vibrationPattern[2] = 5000;
    vibrationPattern[3] = 2000;

    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'added_my_channel_id',
        'abc',
        'abc',
        icon: 'app_icon',
        playSound: true,
        sound: RawResourceAndroidNotificationSound('notifiysound'),
        vibrationPattern: vibrationPattern,
        enableLights: true,
        color: const Color.fromARGB(255, 255, 0, 0),
        ledColor: const Color.fromARGB(255, 255, 0, 0),
        ledOnMs: 1000,
        ledOffMs: 500);

    var iOSPlatformChannelSpecifics =
    IOSNotificationDetails(sound: 'notifiysound.mp3');
    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    flutterLocalNotificationsPlugin. show(
        0, title, message,platformChannelSpecifics);
  }

You're probably missing a keep.xml in raw folder.您可能在 raw 文件夹中缺少 keep.xml 。 Then the files won't be included in build but only available when running in debug mode.然后文件将不会包含在构建中,但仅在以调试模式运行时可用。 See last answer here: Flutter local notification custom sound doesn't work (path issue)在此处查看最后一个答案: Flutter 本地通知自定义声音不起作用(路径问题)

Add full path instead of file name添加完整路径而不是文件名

Incorret: IOSNotificationDetails(sound: 'notifiysound.mp3'); Incorret: IOSNotificationDetails(sound: 'notifiysound.mp3');

Correct: IOSNotificationDetails(sound: 'assets/notifiysound.mp3');正确: IOSNotificationDetails(sound: 'assets/notifiysound.mp3');

please put below code in your MainActivity.java file in android folder.请将以下代码放入 android 文件夹中的 MainActivity.java 文件中。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
{
  Uri soundUri=Uri.parse("android.resource://"+getApplicationContext()
                    .getPackageName() + "/" +  R.raw.alert);
  AudioAttributes audioAttributes =AudioAttributes.Builder()
                  .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                  .setUsage(AudioAttributes.USAGE_ALARM)
                  .build();
  NotificationChannel channel = new 
  NotificationChannel("channelId","channelName", 
  NotificationManager.IMPORTANCE_HIGH);
  channel.setSound(soundUri, audioAttributes);
  NotificationManager notificationManager = 
  getSystemService(NotificationManager.class);
  notificationManager.createNotificationChannel(channel);
 } 

and put your Custom sound mp3 file in your projects android/app/src/raw/mp3 file并将您的自定义声音 mp3 文件放入您的项目 android/app/src/raw/mp3 文件中

Note: it will only work for Android custom sound注意:它仅适用于 Android 自定义声音

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 通知自定义声音在 android 的发布模式下不起作用 - Flutter Notification custom sound not working in release mode in android flutter FCM 通知声音在发行版中不起作用 - flutter FCM notification sound not working in release Flutter 应用程序在发布版本上的工作方式不同 - Flutter app works differently on release build Flutter build gradle 在尝试发布 Android 应用 apk 时失败 - Flutter build gradle failed while trying to make a release android app apk 我正在使用firebase_admob在我的flutter应用中实施广告,但广告在发布版本apk中不起作用 - I am using firebase_admob to implement ads in my flutter app, but ads are not working in release build apk 我正在使用firebase_admob在我的flutter应用中实施abmob,但是广告在发行版构建apk中不起作用 - I am using firebase_admob to implement abmob in my flutter app, but ads are not working in release build apk 为什么Flutter Build Apk和Flutter无法在Release上安装 - Why Flutter Build Apk and Flutter install on Release not working Systrace 自定义事件未显示在应用程序的发布版本中 - Systrace custom events not showing in release build of app Flutter 应用程序崩溃并且构建无法正常工作 - Flutter App Crashes and build is not working Firebase App邀请无法在发行版本中运行APK - Firebase App Invite not working in release build APK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM