Below is the code for starting the foreground service. It is working fine for many devices like Samsung, moto, Vivo, Oppo and also with Android version nougat and oreo, but not
Use my method work in all devices
private void checkOptimization() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String packageName = getApplicationContext().getPackageName();
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
if (pm != null) {
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
}
}
}