wjc
16 小时以前 f151e203da870dbe281defcb1daf84a1a38ecac1
app/src/main/java/com/hdl/photovoltaic/other/HdlPushLogic.java
New file
@@ -0,0 +1,264 @@
package com.hdl.photovoltaic.other;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.ConstantManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.enums.MessageStateType;
import com.hdl.photovoltaic.internet.HttpClient;
import com.hdl.photovoltaic.internet.api.HttpApi;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.push.PushMessageInfoBean;
import com.hdl.photovoltaic.ui.BPowerStationActivity;
import com.hdl.photovoltaic.ui.bean.MessageBean;
import com.hdl.photovoltaic.utils.SharedPreUtils;
import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
import org.greenrobot.eventbus.EventBus;
import java.util.Objects;
import cn.jpush.android.api.NotificationMessage;
/**
 * 极光推送信息处理逻辑
 */
public class HdlPushLogic {
    private static volatile HdlPushLogic sHdlPushLogic;
    /**
     * 获取当前对象
     *
     * @return HdlAccountLogic
     */
    public static synchronized HdlPushLogic getInstance() {
        if (sHdlPushLogic == null) {
            synchronized (HdlPushLogic.class) {
                if (sHdlPushLogic == null) {
                    sHdlPushLogic = new HdlPushLogic();
                }
            }
        }
        return sHdlPushLogic;
    }
    /**
     * 添加推送Token
     *
     * @param cloudCallBeak -
     */
    public void pushAdd(CloudCallBeak<String> cloudCallBeak) {
        String requestUrl = HttpApi.POST_push_add;
        JsonObject json = new JsonObject();
        json.addProperty("deviceName", Build.MODEL);//设备名称
        json.addProperty("deviceType", "Android");//设备类型
        json.addProperty("produce", AppConfigManage.isIsOnlineServer());//表示是否是正式服务器
        json.addProperty("pushToken", UserConfigManage.getInstance().getPushToken());//App的推送Token,在极光为RegId
        json.addProperty("software", "PHOTOVOLTAIC");//也就云端说的通道号(channel)
        json.addProperty("platform", "ALIYUN_APNS");//阿里云的版本的platform
//        json.addProperty("platform", "FCM");//google的版本的platform
        json.addProperty("language", UserConfigManage.getInstance().getCurrentAppLanguage());//2.0追加的字段
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String pushId) {
                if (!TextUtils.isEmpty(pushId)) {
                    UserConfigManage.getInstance().setPushId(pushId);
                    UserConfigManage.getInstance().Save();
                }
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(pushId);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 删除推送Token
     *
     * @param cloudCallBeak -
     */
    public void pushDel(CloudCallBeak<String> cloudCallBeak) {
        String requestUrl = HttpApi.POST_push_del;
        JsonObject json = new JsonObject();
        json.addProperty("pushId", UserConfigManage.getInstance().getPushId());
        json.addProperty("pushToken", UserConfigManage.getInstance().getPushToken());
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(jsonStr);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 推送数据统一处理的方法
     *
     * @param context             上下文
     * @param pushMessageInfoBean 推送数据对象实体
     * @param isAppInForeground   true表示是前台,false表示后台
     */
    public void PushPushCommonData(Context context, PushMessageInfoBean pushMessageInfoBean, boolean isAppInForeground) {
        if (UserConfigManage.getInstance().isBAccount()) {
            //添加推送数据到缓存列表中;
//            this.addPushDataToMemoryList(pushMessageInfoBean);
            if (isAppInForeground) {
                //应用在前台通知更新住宅消息列表即可
                BaseEventBus bus = new BaseEventBus();
                bus.setTopic(ConstantManage.refresh_message_house);
                bus.setType(ConstantManage.refresh_message_house);
                EventBus.getDefault().postSticky(bus);
            } else {
                // 安装商跳转界面
//                Intent intent = new Intent(context, BPowerStationActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                intent.putExtra("skip", "skip");//里面判断有这个标识就跳转到消息中心界面
//                context.startActivity(intent);
            }
        } else {
//            if (isOpened) {
            //产品经理说暂时不做 2024年03月29日10:34:44
//            }
        }
    }
    /**
     * 添加在推送数据到缓存列表中
     */
    private void addPushDataToMemoryList(PushMessageInfoBean pushMessageInfoBean) {
        try {
            boolean isPushAddCache = SharedPreUtils.getBoolean("pushAddCache");
            if (!isPushAddCache) {
                //有可能删除推送Token接口失败,换账号后,依然也会收到上次账号的推送;
                return;
            }
            if (TextUtils.isEmpty(pushMessageInfoBean.getExpantContent())) {
                return;
            }
            Gson gson = new Gson();
            MessageBean messageBean = gson.fromJson(pushMessageInfoBean.getExpantContent(), MessageBean.class);
            if (messageBean == null) {
                return;
            }
            messageBean.setTitle(pushMessageInfoBean.getContent());
            messageBean.setDeviceDesc(pushMessageInfoBean.getContent());
            HdlMessageLogic.getInstance().setListMessage(messageBean, 0);
            HdlLogLogic.print("极光推送---添加在推送数据到缓存列表中---" + new Gson().toJson(pushMessageInfoBean));
        } catch (Exception ignored) {
        }
    }
    /**
     * 推送数据作转指定推送模型
     *
     * @param title   推送标题 可选参数(不存在传空字符)
     * @param content 推送内容 可选参数(不存在传空字符)
     * @param data    消息拓展负载数据 可选参数(不存在传空字符)
     */
    public PushMessageInfoBean pushDataProcessing(String title, String content, String data) {
        try {
            PushMessageInfoBean pushMessageInfoBean = new PushMessageInfoBean();
            pushMessageInfoBean.setTitle(title);
            pushMessageInfoBean.setContent(content);
            if (TextUtils.isEmpty(data)) {
                return pushMessageInfoBean;
            }
            JSONObject pushDataObject = JSON.parseObject(data);
            if (pushDataObject.containsKey("expandData")) {
                String expandDataJson = Objects.requireNonNull(pushDataObject.get("expandData")).toString();
                if (TextUtils.isEmpty(expandDataJson)) {
                    return pushMessageInfoBean;
                }
                pushMessageInfoBean.setExpandData(expandDataJson);
                JSONObject expandDataObject = JSON.parseObject(expandDataJson);
                if (expandDataObject.containsKey("messageType")) {
                    String messageType = Objects.requireNonNull(expandDataObject.get("messageType")).toString();
                    if (!TextUtils.isEmpty(messageType)) {
                        pushMessageInfoBean.setMessageType(messageType);
                    }
                }
                if (TextUtils.isEmpty(title)) {
                    if (expandDataObject.containsKey("title")) {
                        String messageTitle = Objects.requireNonNull(expandDataObject.get("title")).toString();
                        if (!TextUtils.isEmpty(messageTitle)) {
                            pushMessageInfoBean.setTitle(messageTitle);
                        }
                    }
                }
                if (TextUtils.isEmpty(content)) {
                    if (expandDataObject.containsKey("content")) {
                        String messageContent = Objects.requireNonNull(expandDataObject.get("content")).toString();
                        if (!TextUtils.isEmpty(messageContent)) {
                            pushMessageInfoBean.setTitle(messageContent);
                        }
                    }
                }
                if (expandDataObject.containsKey("homeId")) {
                    String homeId = Objects.requireNonNull(expandDataObject.get("homeId")).toString();
                    if (!TextUtils.isEmpty(homeId)) {
                        pushMessageInfoBean.setHomeId(homeId);
                    }
                }
                if (expandDataObject.containsKey("expantContent")) {
                    String expantContentJson = Objects.requireNonNull(expandDataObject.get("expantContent")).toString();
                    if (!TextUtils.isEmpty(expantContentJson)) {
                        pushMessageInfoBean.setExpantContent(expantContentJson);
                    }
                    JSONObject expantContentObject = JSON.parseObject(expantContentJson);
                    if (expantContentObject.containsKey("aiPrompt")) {
                        String aiPrompt = Objects.requireNonNull(expantContentObject.get("aiPrompt")).toString();
                        if (!TextUtils.isEmpty(aiPrompt)) {
                            pushMessageInfoBean.setAiPrompt(Boolean.parseBoolean(aiPrompt));
                        }
                    }
                    if (expantContentObject.containsKey("msgId")) {
                        String msgId = Objects.requireNonNull(expantContentObject.get("msgId")).toString();
                        if (!TextUtils.isEmpty(msgId)) {
                            pushMessageInfoBean.setMsgId(msgId);
                        }
                    }
                }
            }
            return pushMessageInfoBean;
        } catch (Exception e) {
            return new PushMessageInfoBean();
        }
    }
}