簡體   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