繁体   English   中英

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

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

在调试模式下一切正常,如果我运行,flutter run --release。 但是,如果我生成发布版本并使用它运行,自定义声音将不起作用。 我正在使用 flutter。

这是代码,

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);
  }

您可能在 raw 文件夹中缺少 keep.xml 。 然后文件将不会包含在构建中,但仅在以调试模式运行时可用。 在此处查看最后一个答案: Flutter 本地通知自定义声音不起作用(路径问题)

添加完整路径而不是文件名

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

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

请将以下代码放入 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);
 } 

并将您的自定义声音 mp3 文件放入您的项目 android/app/src/raw/mp3 文件中

注意:它仅适用于 Android 自定义声音

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM