Improve push notification issues in Android.

As you already might be facing issues with push notification like not delivering to some percentage of devices. so here are the ways by which we can improve the push notification received rate.

  1. Make app whitelist

By default some of the device manufacturer does not allow all applications to persist in memory when we are not using or removed from recent, only some of the application like facebook, whatsapp, gmail etc are whitelisted from OEM just to enhance the device performance and battery optimisation.

what we can do is firstly we need to check whether our app is battery optimised from device manufacture. we can check using

PowerManager pm = (PowerManager) getActivity().getSystemService(POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !pm.isIgnoringBatteryOptimizations(packageName) ) {
//here we can show some message to user.
} else {
//already white listed
}

but to execute above in android M and above one permission is required

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

if your app is not white listed then show some message to user like (notification are important to receive all updates) and to make app whitelist ask user to grant this permission

Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);

2. User blocked our notifications

we can check if user has blocked our notifications, if he/she has blocked then also we can check and request to allow these notifications.

NotificationManagerCompat.from(context).areNotificationsEnabled()

If notifications are blocked for our application then this will return false then again we have to show some informative message and request for allow of notification using below code

Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");

intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);

intent.putExtra("android.provider.extra.APP_PACKAGE", superActivity.getPackageName());

superActivity.startActivity(intent);

3. what if user uninstalled our app

when we send push notification via fcm the fcm gives response with informative message with HTTP Response 200 that means (uninstall, invalid token, missing registration or invalid package name)

then we have to remove or disable the token so that we don’t request send push notification again. but we have to be careful for invalid package name error.

4. Device does not have play service or not whitelisted

Now what we can do is whenever we launch app we can request for future notification with time from backend server and set a alarm so that app can show these notification at that particular time.

but we have to be careful with some things like, a) don’t show duplicate notifications, b) there should be some expiry time of notification so that we won’t show useless notifications, c) we should keep track in backend that how many notification is seen by user so that we won’t show the same again.

Please do comment if you have any else clap if you like to motivate.

Lead Android Engineer