Ad

Why Am I Not Able To Recive A Push Notification When My App Is Battery Optimized?

When my app is battery optimized I cannot recive push notifications. Is there a posibility to get whitelisted by Google?

In the past I used Firebase to send my Push notifications. I tought it was a problem with Firebase and so I tryed with OneSignal but I got the same issue. I am also sending with high priority, but it is not working when battery optimized.

Is there a posibility to get this solved or is the only way that the user disables the battery optimization?

Will I be banned from the Play Store if I use this code?

    if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) {
  String pkg=getPackageName();
  PowerManager pm=getSystemService(PowerManager.class);

  if (!pm.isIgnoringBatteryOptimizations(pkg)) {
    Intent i=
      new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
        .setData(Uri.parse("package:"+pkg));

    startActivity(i);
  }
}
Ad

Answer

Unfortunately, this is a known issue. Most mobile manufacturing companies lately, with their own modified versions of Android actually stop most, if not all, services running in the background for saving battery. As far as i can tell, there is only one thing you can do which is to nicely prompt the user to whitelist your app so that it can run without any hurdle even during the power saving mode.

Ad
source: stackoverflow.com
Ad