4

Can anyone help me to receive notifications on Xiaomi and Lenovo devices even after the app is killed(no more in the background)?

Edit 1

I added GCM broadcast receiver. Here is the code

inside AndroidManifest.xml

 <receiver
       android:name="com.don.offers.broadcast_receiver.GcmBroadcastReceiver"
       android:permission="com.google.android.c2dm.permission.SEND" >
       <intent-filter>
           <!-- Receives the actual messages. -->
           <action android:name="com.google.android.c2dm.intent.RECEIVE" />
           <category android:name="com.google.android.gcm.demo.app" />
       </intent-filter>
   </receiver>

GcmBroadcastReceiver.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(),
                RegistrationIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

It solved my problem on MI device but not on Lenovo devices.

Thanks

Sapna Sharma
  • 460
  • 7
  • 22
  • Hi. Have you seen the answers in this [post](http://stackoverflow.com/q/39504805/4625829)? – AL. Feb 17 '17 at 14:01
  • Possible duplicate of [Android app not receiving Firebase Notification when app is stopped from multi-task tray](http://stackoverflow.com/questions/39504805/android-app-not-receiving-firebase-notification-when-app-is-stopped-from-multi-t) – Damian Kozlak Feb 19 '17 at 03:58
  • @AL yes I did but its not what I am looking for. – Sapna Sharma Feb 20 '17 at 11:50

2 Answers2

1

Lenovo mobiles are using Background task killer for stop background apps, to Hide task killer by untick restrict in application menu

1

On devices with MIUI you can ask user to add your app in the autostart list of the phone using this:

 private void addAppToAutoStartList() {
  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
  alertDialogBuilder.setTitle("Warning!");
  alertDialogBuilder.setMessage("Please add this app to the Auto Start list of your device for better performance.");
  alertDialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
   @Override public void onClick(DialogInterface dialogInterface, int i) {
    dialogInterface.dismiss();
    try {
     AppPreferences.getInstance(HomeActivity.this).setMiSpecialSetting(true);
     Intent intent = new Intent();
     intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
     startActivity(intent);
    } catch (Exception e) {
     Toast.makeText(HomeActivity.this, "Unable to add!", Toast.LENGTH_SHORT).show();
    }
   }
  });
  alertDialogBuilder.setNegativeButton("Ignore", new DialogInterface.OnClickListener() {
   @Override public void onClick(DialogInterface dialog, int arg1) {
    dialog.dismiss();
   }
  });
  AlertDialog alertDialog = alertDialogBuilder.create();
  alertDialog.show();
 }  

And call this method by checking for manufacturer like

if(android.os.Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) { addAppToAutoStartList(); 
}

Conclusion: 1. In this way ff user adds your app to autostart list then your app will be able to get push notifications without any problem. 2. If you have any scheduled job to run then you will be able to run your job even after one key clean but there is a limitation, your job will run but not according to your time and flex, it will get called anytime after 1 day and next call might come after 2 days so there is no guarantee of periodic call. But this is the only way I can see as of now for MIUI like custom OS. And I have tested this in many Xiaomi devices having android 5 to 7 and every where the results are same.

Abhimanyu Raj
  • 73
  • 1
  • 9