Doze mode and App Standby may temporarily suspend network and background execution for your app when it goes to background and the phone is idle.
To ensure your users will be able to receive notifications in Doze mode, please follow these steps:
1) Consider implementing FCM high-priority fallback delivery, which makes it possible for devices to receive notifications in real time during Doze mode. Pushy will attempt to deliver your notifications through MQTT and Firebase Cloud Messaging's high priority channel simultaneously, effectively bursting through Doze mode and other third-party manufacturer power saving optimizations.
2) If you can't make use of FCM high-priority fallback delivery for your use case, exempting your app from Android's power saving optimizations will make it possible to receive notifications during Doze mode. Consider asking your users to disable battery optimizations for your app by displaying an in-app dialog with instructions and firing the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
activity (completely safe and will not get your app suspended from Google Play):
The code for displaying this dialog is as follows:
// Android M (6) and up only
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Get power manager instance
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
// Check if app isn't already whitelisted from battery optimizations
if (!powerManager.isIgnoringBatteryOptimizations(getPackageName())) {
// Get app name as string
String appName = getPackageManager().getApplicationLabel(getApplicationInfo()).toString();
// Instruct user to whitelist app from battery optimizations
new AlertDialog.Builder(this)
.setTitle("Disable battery optimizations")
.setMessage("If you'd like to receive notifications in the background, please click OK and select \"All apps\" -> " + appName + " -> Don't optimize.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Display the battery optimization settings screen
startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS));
}
})
.setNegativeButton("Cancel", null).show();
}
}
Note: You can also instruct your users to manually go to Settings -> Apps & notifications -> Advanced -> Special app access -> Battery optimization -> All Apps -> Find "YourAppName" -> Set to "Don't optimize".
Once implemented, either of these solutions will make it possible for your users to receive notifications in real time with your app in the background during Doze mode.
Comments
0 comments
Please sign in to leave a comment.