Skip to content Skip to sidebar Skip to footer

Android Push Notifications Not Working When App Is Closed

I'm using OkSse to subscribe to my Server Sent Events. Whenever a new message is sent by the server, a notification should appear whether the app is in foreground,minimized or full

Solution 1:

Main Reason behind this is AutoLaunch and Battery Optimization(MIUI) devices try to change don't optimize option for your app in Battery setting

privatevoidinitOPPO() {
        try {

            Intenti=newIntent(Intent.ACTION_MAIN);
            i.setComponent(newComponentName("com.oppo.safe", "com.oppo.safe.permission.floatwindow.FloatWindowListActivity"));
            startActivity(i);
        } catch (Exception e) {
            e.printStackTrace();
            try {

                Intentintent=newIntent("action.coloros.safecenter.FloatWindowListActivity");
                intent.setComponent(newComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.floatwindow.FloatWindowListActivity"));
                startActivity(intent);
            } catch (Exception ee) {

                ee.printStackTrace();
                try {

                    Intenti=newIntent("com.coloros.safecenter");
                    i.setComponent(newComponentName("com.coloros.safecenter", "com.coloros.safecenter.sysfloatwindow.FloatWindowListActivity"));
                    startActivity(i);
                } catch (Exception e1) {

                    e1.printStackTrace();
                }
            }

        }
    }

    privatestaticvoidautoLaunchVivo(Context context) {
        try {
            Intentintent=newIntent();
            intent.setComponent(newComponentName("com.iqoo.secure",
                    "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
            context.startActivity(intent);
        } catch (Exception e) {
            try {
                Intentintent=newIntent();
                intent.setComponent(newComponentName("com.vivo.permissionmanager",
                        "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
                context.startActivity(intent);
            } catch (Exception ex) {
                try {
                    Intentintent=newIntent();
                    intent.setClassName("com.iqoo.secure",
                            "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager");
                    context.startActivity(intent);
                } catch (Exception exx) {
                    ex.printStackTrace();
                }
            }
        }
    }

Usage

if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
                                initOPPO();
                            } elseif (Build.MANUFACTURER.equalsIgnoreCase("vivo")) {
                                autoLaunchVivo(BaseSettingActivity.this);
                            } elseif (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) {
                                try {
                                    Intentintent=newIntent();
                                    intent.setComponent(newComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                                    startActivity(intent);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

Solution 2:

Your Approach: Using OkSse for notification means the need of ever running background service.

Problem 1: Android background service restrictions link

JobIntentService internally will use JobService and it will not be a continues running service. It will not run indifferently as execution time limit will apply. It will stop and rescheduled to continue its execution later. So in your case, it will not work.

Problem 2: Devices Xiaomi, Oppo, One Plus, Vivo, Lenovo, Huawei, Samsung have Battery Optimization check which lets JobIntentService to stop.

Answer :

1) Run Foreground service which is ugly and not recommended

2) Firebase High Priority Notification link HIGHLY RECOMMENDED

I have implemented it on the above mention devices, even if the app is set to battery optimization mode you will receive a notification.

Post a Comment for "Android Push Notifications Not Working When App Is Closed"