7

Notification sound settings always disable in Xiomi devices. Check below image.

I want to enable sound programatically. Found similar stackoverflow questions but nothing helped.

Device : Redmi Note 5 pro, Redmi Note 9 pro

OS : MIUI 11 , MIUI 12

Note: But it works fine in all other devices. Problem only in Xiomi devices

enter image description here

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159

2 Answers2

0

I was looking for the same kind of issue 2 months ago, due to restrictions by Xiaomi it has become a lot harder, basically you have to have your app running without restriction in save battery settings and to bring the user to autostart where he will have to click on the toggle button to add your app.

Prompt the user to disable battery optimization for your app:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onStart()
   {
       String packageName = "com.example.myapp";
       PowerManager powerManager = (PowerManager)getApplicationContext().getSystemService(POWER_SERVICE);        
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent i = new Intent();
            if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
                i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                i.setData(Uri.parse("package:" + packageName));
                startActivity(i);
            }
        }
    }
}

For an app to use this, it also must hold the Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission


You also have to prompt the user to add your app to autostart (original credit) :

String manufacturer = "xiaomi";
if (manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
    //this will open auto start screen where user can enable permission for your app
    Intent intent1 = new Intent();
    intent1.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
    startActivity(intent1);
}

It should now be working, let me know if it helped!

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
0

You may try to use NotificationManager.IMPORTANCE_MAX in new NotificationChannel

Arc Li
  • 1
  • 1