wjc
4 天以前 acccdeba7fe28f7f4b72ba464ad4789b0d9ddb2a
app/src/main/java/com/hdl/photovoltaic/push/fcm/MyFirebaseMessagingService.java
@@ -23,6 +23,7 @@
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.enums.MessageStateType;
import com.hdl.photovoltaic.other.HdlPushLogic;
import com.hdl.photovoltaic.push.CustomNotification;
import com.hdl.photovoltaic.push.PushMessageInfoBean;
import com.hdl.photovoltaic.ui.BPowerStationActivity;
import com.hdl.photovoltaic.ui.StartActivity;
@@ -38,6 +39,9 @@
import cn.jpush.android.api.NotificationMessage;
/**
 * 谷歌推送消息接收方法
 */
public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCMService";
@@ -56,7 +60,7 @@
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "在前台还是在后" + isAppInForeground(this));
        Log.d(TAG, "是否在前台:" + isAppInForeground(this));
        Log.d(TAG, "来自RemoteMessage: " + JSON.toJSONString(remoteMessage));
//        // 数据消息:任何状态都会执行
//        if (!remoteMessage.getData().isEmpty()) {
@@ -67,27 +71,32 @@
//        if (remoteMessage.getNotification() != null) {
//            handleNotification(remoteMessage.getNotification()); // 只有前台执行
//        }
        String title = "";//推送标题
        String content = "";//推送内容
        if (remoteMessage.getNotification() != null) {
            title = remoteMessage.getNotification().getTitle();
            content = remoteMessage.getNotification().getBody();
        }
        //原生在前台或者uni在前台
        if (isAppInForeground(this) || HDLUniMPSDKManager.getInstance().showUniMP()) {
            // 检查消息是否包含数据负载
            if (!remoteMessage.getData().isEmpty()) {
                PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing(title, content, JSON.toJSONString(remoteMessage.getData()));
                HdlPushLogic.getInstance().PushPushCommonData(this, pushMessageInfoBean, false);
//            this.showCustomNotification(pushMessageInfoBean);
            }
        } else {
            // 检查消息是否包含通知负载
            if (!remoteMessage.getData().isEmpty()) {
                PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing(title, content, JSON.toJSONString(remoteMessage.getData()));
                this.showCustomNotification(pushMessageInfoBean);
            }
        if (!remoteMessage.getData().isEmpty()) {
            CustomNotification.getInstance().messageDataProcessing(this, title, content, JSON.toJSONString(remoteMessage.getData()), CustomNotification.FromPush.FCM);
        }
//        //原生在前台或者uni在前台
//        if (isAppInForeground(this)) {
//            // 检查消息是否包含数据负载
//            if (!remoteMessage.getData().isEmpty()) {
//                PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing(title, content, JSON.toJSONString(remoteMessage.getData()));
//                HdlPushLogic.getInstance().PushPushCommonData(this, pushMessageInfoBean, false);
////            this.showCustomNotification(pushMessageInfoBean);
//            }
//        } else {
//            // 检查消息是否包含通知负载
//            if (!remoteMessage.getData().isEmpty()) {
//                PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing(title, content, JSON.toJSONString(remoteMessage.getData()));
//                this.showCustomNotification(pushMessageInfoBean);
//            }
//        }
    }
@@ -114,7 +123,7 @@
                .setWhen(System.currentTimeMillis());
        // 设置点击意图,为通知添加点击后的跳转意图
        Intent intent = createNotificationIntent(pushMessageInfoBean.getExpandData());
        Intent intent = createNotificationIntent(pushMessageInfoBean);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        builder.setContentIntent(pendingIntent);
@@ -135,10 +144,10 @@
        notificationManager.createNotificationChannel(channel);
    }
    private Intent createNotificationIntent(String data) {
    private Intent createNotificationIntent(PushMessageInfoBean pushMessageInfoBean) {
        Intent intent = new Intent(this, StartActivity.class);
        intent.putExtra("from_fcm", true);
        intent.putExtra("pushData", data);
        intent.putExtra("pushData", JSONObject.toJSONString(pushMessageInfoBean));
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }
@@ -175,9 +184,11 @@
                return false;
            }
            final String packageName = context.getPackageName();
            final String uniPackageName = packageName + ":unimp0";
            for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
                        appProcess.processName.equals(packageName)) {
                if ((appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
                        appProcess.processName.equals(packageName)) || (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
                        appProcess.processName.equals(uniPackageName))) {
                    return true;
                }
            }