【问题标题】:Not receiving GCM notifications once app is killed on Xiaomi and Lenovo devices in Android一旦应用程序在 Android 中的小米和联想设备上被杀死,就不会收到 GCM 通知
【发布时间】:2017-12-05 18:36:37
【问题描述】:

任何人都可以帮助我在小米和联想设备上接收通知,即使在应用程序被终止后(不再在后台)?

编辑 1

我添加了 GCM 广播接收器。这是代码

在 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);
    }
}

它解决了我在 MI 设备上的问题,但在联想设备上却没有。

谢谢

【问题讨论】:

标签: android notifications google-cloud-messaging


【解决方案1】:

联想手机正在使用后台任务杀手来停止后台应用程序,通过取消勾选应用程序菜单中的限制来隐藏任务杀手

【讨论】:

  • 我可以通过编程方式限制它吗?
【解决方案2】:

在带有 MIUI 的设备上,您可以要求用户使用以下命令将您的应用添加到手机的自动启动列表中:

 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();
 }  

并通过检查制造商来调用此方法

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

结论: 1.通过这种方式ff用户将您的应用程序添加到自动启动列表中,那么您的应用程序将能够毫无问题地获得推送通知。 2.如果您有任何预定的作业要运行,那么即使一键清除,您也可以运行您的作业,但有一个限制,您的作业将运行但不根据您的时间和弹性,它会在 1 后随时调用第二天和下一个电话可能会在 2 天后到来,因此无法保证定期通话。但这是我目前能看到的唯一方式,用于 MIUI 之类的自定义操作系统。我已经在许多具有 android 5 到 7 的小米设备上进行了测试,结果都相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2021-09-21
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多