1) Please ensure you aren't observing this behavior when starting your app from within Android Studio. When you run your app from within Android Studio, and you later terminate it by swiping it away from the recent apps, it will no longer receive notifications until reopened (more info).
2) Some Chinese Android manufacturers make use of custom power-saving restrictions that inhibit sending push notifications to your users while your app is in the background, swiped away from recent apps, or after a device restart.
Chinese market devices from the following manufacturers include custom power-saving restrictions:
- Xiaomi
- Huawei
- OnePlus
- Samsung
- Oppo
- Vivo
- Asus
- Sony
- Honor
In order to receive notifications in the background on these devices, there are three options:
1) If the affected devices are not within mainland China, we recommend implementing FCM high priority fallback delivery. This is proven to work around several of these manufacturers' custom power saving mechanisms (Xiaomi, OnePlus, and more), by having Pushy deliver your notifications through MQTT and Firebase Cloud Messaging's high-priority channel simultaneously, while only waking up your app once.
2) Display a dialog asking your users to whitelist your app from the respective manufacturer's power saving mechanism, within the device settings. The steps for achieving this are different for each manufacturer.
More info about this issue:
3) If you're not distributing your app through Google Play, you can make it possible for your users to exempt your app from battery optimizations without leaving your app, which will effectively disable the third-party manufacturer custom power saving optimizations for your app:
First, declare the following permission in yourAndroidManifest.xml
:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
Next, call the following from one of your activities to display an in-app dialog to the user which will allow them to whitelist your app from battery optimizations without leaving your app:
// Get instance of Power Manager
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
// Android M (6) and up only
// Check if the user has not already whitelisted your app from battery optimizations
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !powerManager.isIgnoringBatteryOptimizations(getPackageName())) {
// Display an in-app dialog which will allow the user to exempt your app without leaving it
startActivity(new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:"+getPackageName())));
}
Note: Please do not use the above solution if you are distributing your app through Google Play, as it will most likely lead to app suspension.
Comments
0 comments
Please sign in to leave a comment.