Nupur
Nupur

Reputation: 51

Flutter Notification custom sound not working in release mode in android

Below code is no working for release mode 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,
        );

Upvotes: 1

Views: 1020

Answers (1)

Sumita Naiya
Sumita Naiya

Reputation: 393

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

and put your Custom sound mp3 file in your projects android/app/src/raw/mp3 file

Note: it will only work for Android custom sound

Upvotes: 1

Related Questions