android 如果我们在flutter应用程序中发布构建版本,则自定义声音不起作用

nlejzf6q  于 2022-12-02  发布在  Android
关注(0)|答案(3)|浏览(157)

调试模式下一切正常,如果我运行flutter运行--release。但是如果我生成release build并使用它运行,自定义声音就不工作了。我使用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);
  }
aelbi1ox

aelbi1ox1#

您可能在raw文件夹中丢失了keep.xml文件。那么这些文件将不会包含在构建中,而只能在调试模式下运行时使用。请在此处查看最后的答案:Flutter local notification custom sound doesn't work (path issue)

hgc7kmma

hgc7kmma2#

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

**错误:**IOS通知详细信息(声音:“通知声音.mp3”);
**正确:**IOSNotificationDetails(声音:“资产/通知声音.mp3”);

4ngedf3f

4ngedf3f3#

请将以下代码放入MainActivity.javaandroid文件夹中的www.example.com文件中。

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自定义音效

相关问题