簡體   English   中英

Flutter 通知自定義聲音在 android 的發布模式下不起作用

[英]Flutter Notification custom sound not working in release mode in android

以下代碼不適用於發布模式 flutter。

 AndroidNotificationChannel channel= const AndroidNotificationChannel(
          'abc',
          'abc_channel',
          description: 'This channel is used for important notifications.',
          sound: RawResourceAndroidNotificationSound('alert'),
          playSound: true,
          importance: Importance.high,
          enableLights: true,
        );
please put below code in your MainActivity.java file in android folder.

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 文件中

Note: it will only work for Android custom sound

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM