Not receiving GCM notifications once app is killed on Xiaomi and Lenovo devices in Android

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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

回答1:

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



回答2:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!