友盟推送 红米手机收不到推送的问题

在集成友盟推送时,需要集成各大厂商通道来提升推送的到达率,在集成小米厂商通道时,文档有段代码为:

MiPushRegistar.register(final Context context, final String XIAOMI_ID, final String XIAOMI_KEY);

而这段代码现在最新的版本却存在一个问题,点击方法进去会看到一段代码:

    public static boolean checkDevice(Context context) {
        boolean result = false;

        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo pkgInfo = null;
            if (TextUtils.equals(XIAOMI, phoneBrand.toLowerCase())) {
                pkgInfo = manager.getPackageInfo("com.xiaomi.xmsf", 4);
                if (pkgInfo != null && pkgInfo.versionCode >= 105) {
                    result = true;
                }
            }
        } catch (Throwable var4) {
            ALog.e("MiPushRegistar", "checkDevice", var4, new Object[0]);
        }

        ALog.d("MiPushRegistar", "checkDevice", new Object[]{"result", result});
        return result;
    }

这段代码仅仅只是判断了Build.BRAND为"Xiaomi",而部分红米手机Build.BRAND为"Redmi",所以导致了集成小米厂商推送没有成功,在APP离线的情况下会接收不到相关通知。

所以修复问题需要加上Redmi的判断,解决方案就是复制原代码再加上相关判断,新建类名:MiFixPushRegister,把集成伪代码修改如下:

MiFixPushRegister.register(final Context context, final String XIAOMI_ID, final String XIAOMI_KEY);

修改后的MiFixPushRegister类的相关完整代码如下:

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.taobao.accs.utl.ALog;
import com.taobao.accs.utl.UtilityImpl;
import com.taobao.agoo.BaseNotifyClickActivity;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushMessage;

public class MiFixPushRegister {
    private static final String PACKAGE_XIAOMI = "com.xiaomi.xmsf";
    private static final String XIAOMI = "Xiaomi".toLowerCase();
    private static final String REDMI = "Redmi".toLowerCase();
    private static String phoneBrand;

    static {
        phoneBrand = Build.BRAND;
    }

    public static boolean checkDevice(Context context) {
        boolean result = false;

        try {
            PackageManager manager = context.getPackageManager();
            if (TextUtils.equals(XIAOMI, phoneBrand.toLowerCase()) ||
                    TextUtils.equals(REDMI, phoneBrand.toLowerCase())) {
                PackageInfo pkgInfo = manager.getPackageInfo(PACKAGE_XIAOMI, PackageManager.GET_SERVICES);
                if (pkgInfo != null && pkgInfo.versionCode >= 105) {
                    result = true;
                }
            }
        } catch (Throwable var4) {
            ALog.e("MiPushRegistar", "checkDevice", var4, new Object[0]);
        }

        ALog.d("MiPushRegistar", "checkDevice", new Object[]{"result", result});
        return result;
    }

    public static void register(Context context, String xiaomiId, String xiaomiKey) {
        try {
            if (!UtilityImpl.isMainProcess(context)) {
                ALog.e("MiPushRegistar", "register not in main process, return", new Object[0]);
                return;
            }

            if (checkDevice(context)) {
                ALog.i("MiPushRegistar", "register begin", new Object[0]);
                BaseNotifyClickActivity.addNotifyListener(new MiFixPushRegister.XiaoMiNotifyListener());
                MiPushClient.registerPush(context, xiaomiId, xiaomiKey);
            }
        } catch (Throwable var4) {
            ALog.e("MiPushRegistar", "register", var4, new Object[0]);
        }

    }

    public static void unregister(Context context) {
        try {
            MiPushClient.unregisterPush(context);
        } catch (Throwable var2) {
            ALog.e("MiPushRegistar", "unregister", var2, new Object[0]);
        }

    }

    private static class XiaoMiNotifyListener implements BaseNotifyClickActivity.INotifyListener {
        private XiaoMiNotifyListener() {
        }

        public String parseMsgFromIntent(Intent intent) {
            String msg = null;

            try {
                MiPushMessage miPushMessage = (MiPushMessage) intent.getSerializableExtra("key_message");
                if (miPushMessage != null)
                    msg = miPushMessage.getContent();
            } catch (Exception var4) {
                var4.printStackTrace();
            }

            ALog.i("MiPushRegistar", "parseMsgFromIntent", new Object[]{"msg", msg});
            return msg;
        }

        public String getMsgSource() {
            return "xiaomi";
        }

        @NonNull
        public String toString() {
            return "INotifyListener: " + this.getMsgSource();
        }
    }
}

 

你可能感兴趣的:(android开发问题,友盟推送红米收不到推送,红米手机BRAND,MiPushRegistar,红米安卓10版本收不到推送)