0

I am facing issues in receiving FCM push notifications when the app is killed on certain devices like Oppo, Huawei, Xiaomi and OnePlus. I understand that it is due to battery optimisation settings in custom ROM phones. I am able to receive notifications if the settings are turned ON manually.

But I would like to know if there is a way to overcome this programmatically. Has anyone achieved this?

Thanks in advance!

1 Answers1

0

Few mobile manufacturers are not allow the external notification until the permission set to receive the notification

Call this method inside onTokenRefresh()

private void addAutoStartup() {
    try {
        Intent intent = new Intent();
        String manufacturer = android.os.Build.MANUFACTURER;
        if ("xiaomi".equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
        } else if ("oppo".equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
        } else if ("vivo".equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
        } else if ("Letv".equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
        } else if ("Honor".equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
        }
        List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if  (list.size() > 0) {
            startActivity(intent);
        }
    } catch (Exception e) {
        Log.e(TAG , String.valueOf(e));
    }
}
Chethan Kumar
  • 185
  • 1
  • 12
  • Thanks Chethan, but this will again require user’s action to turn it ON. I have added this, but yet the battery optimisation would kill it and I am trying to find if it is possible to do it programmatically without user’s action. – aiswarya-sk Nov 26 '18 at 09:19
  • please look into it [link](https://stackoverflow.com/a/49473433/9254960). some explanation have given. – Chethan Kumar Nov 26 '18 at 09:28
  • Yes, this is the fact. I understand, but many apps are whitelisted and I would like to know if there is a way to do it programmatically – aiswarya-sk Nov 26 '18 at 09:35