From f151e203da870dbe281defcb1daf84a1a38ecac1 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期一, 29 十二月 2025 09:59:57 +0800
Subject: [PATCH] Merge branch 'master' into google

---
 app/src/main/java/com/hdl/photovoltaic/push/fcm/MyFirebaseMessagingService.java |  200 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 200 insertions(+), 0 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/push/fcm/MyFirebaseMessagingService.java b/app/src/main/java/com/hdl/photovoltaic/push/fcm/MyFirebaseMessagingService.java
new file mode 100644
index 0000000..89ec8fa
--- /dev/null
+++ b/app/src/main/java/com/hdl/photovoltaic/push/fcm/MyFirebaseMessagingService.java
@@ -0,0 +1,200 @@
+package com.hdl.photovoltaic.push.fcm;
+
+import android.app.ActivityManager;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Build;
+import android.text.TextUtils;
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.RequiresApi;
+import androidx.core.app.NotificationCompat;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.google.firebase.messaging.FirebaseMessagingService;
+import com.google.firebase.messaging.RemoteMessage;
+import com.hdl.photovoltaic.R;
+import com.hdl.photovoltaic.config.ConstantManage;
+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;
+import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
+import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
+
+import org.greenrobot.eventbus.EventBus;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import cn.jpush.android.api.NotificationMessage;
+
+/**
+ * 璋锋瓕鎺ㄩ�佹秷鎭帴鏀舵柟娉�
+ */
+public class MyFirebaseMessagingService extends FirebaseMessagingService {
+    private static final String TAG = "FCMService";
+
+    /**
+     * 褰撴敹鍒版柊鐨� FCM Token 鏃惰皟鐢�
+     */
+    @Override
+    public void onNewToken(@NonNull String token) {
+        Log.d(TAG, "鍒锋柊 FCM Token: " + token);
+        // 灏� token 鍙戦�佸埌鎮ㄧ殑鏈嶅姟鍣�
+        sendRegistrationToServer(token);
+    }
+
+    /**
+     * 褰撴敹鍒版秷鎭椂璋冪敤
+     */
+    @Override
+    public void onMessageReceived(RemoteMessage remoteMessage) {
+        Log.d(TAG, "鏄惁鍦ㄥ墠鍙帮細" + isAppInForeground(this));
+        Log.d(TAG, "鏉ヨ嚜RemoteMessage: " + JSON.toJSONString(remoteMessage));
+//        // 鏁版嵁娑堟伅锛氫换浣曠姸鎬侀兘浼氭墽琛�
+//        if (!remoteMessage.getData().isEmpty()) {
+//            handleDataMessage(remoteMessage.getData()); // 鎬绘槸鎵ц
+//        }
+//
+//        // 閫氱煡娑堟伅锛氬彧鏈夊墠鍙版椂鎵嶄細鎵ц
+//        if (remoteMessage.getNotification() != null) {
+//            handleNotification(remoteMessage.getNotification()); // 鍙湁鍓嶅彴鎵ц
+//        }
+
+
+        String title = "";//鎺ㄩ�佹爣棰�
+        String content = "";//鎺ㄩ�佸唴瀹�
+        if (remoteMessage.getNotification() != null) {
+            title = remoteMessage.getNotification().getTitle();
+            content = remoteMessage.getNotification().getBody();
+        }
+        if (!remoteMessage.getData().isEmpty()) {
+            CustomNotification.getInstance().messageDataProcessing(this, title, content, JSON.toJSONString(remoteMessage.getData()), CustomNotification.FromPush.FCM);
+        }
+//        //鍘熺敓鍦ㄥ墠鍙版垨鑰卽ni鍦ㄥ墠鍙�
+//        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);
+//            }
+//        }
+    }
+
+
+    /**
+     * 澶勭悊閫氱煡娑堟伅锛堝簲鐢ㄥ湪鍚庡彴鏃讹紝绯荤粺浼氳嚜鍔ㄦ樉绀洪�氱煡锛�
+     *
+     * @param pushMessageInfoBean
+     */
+    private void showCustomNotification(PushMessageInfoBean pushMessageInfoBean) {
+        NotificationManager notificationManager =
+                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+
+        // 鍒涘缓閫氱煡娓犻亾锛圓ndroid 8.0+锛�
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            createNotificationChannel(notificationManager);
+        }
+
+        // 鏋勫缓閫氱煡鍐呭
+        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "fcm_channel")
+                .setSmallIcon(R.drawable.notification_logo)
+                .setContentTitle(pushMessageInfoBean.getTitle())
+                .setContentText(pushMessageInfoBean.getContent())
+                .setAutoCancel(true)
+                .setWhen(System.currentTimeMillis());
+
+        // 璁剧疆鐐瑰嚮鎰忓浘,涓洪�氱煡娣诲姞鐐瑰嚮鍚庣殑璺宠浆鎰忓浘
+        Intent intent = createNotificationIntent(pushMessageInfoBean);
+        PendingIntent pendingIntent = PendingIntent.getActivity(
+                this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
+        builder.setContentIntent(pendingIntent);
+
+        // 鍙戦�侀�氱煡,閫氱煡鏍忔樉绀洪�氱煡
+        int notificationId = (int) System.currentTimeMillis();  // 姣忎釜閫氱煡鐨勫敮涓�ID
+        notificationManager.notify(notificationId, builder.build());
+    }
+
+    @RequiresApi(api = Build.VERSION_CODES.O)
+    private void createNotificationChannel(NotificationManager notificationManager) {
+        NotificationChannel channel = new NotificationChannel(
+                "fcm_channel",
+                "FCM Message",
+                NotificationManager.IMPORTANCE_HIGH
+        );
+        channel.setDescription("Firebase Cloud Messaging Notification");
+        notificationManager.createNotificationChannel(channel);
+    }
+
+    private Intent createNotificationIntent(PushMessageInfoBean pushMessageInfoBean) {
+        Intent intent = new Intent(this, StartActivity.class);
+        intent.putExtra("from_fcm", true);
+        intent.putExtra("pushData", JSONObject.toJSONString(pushMessageInfoBean));
+        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
+        return intent;
+    }
+
+    private void sendRegistrationToServer(String token) {
+        // 灏� FCM Token 鍙戦�佸埌鎮ㄧ殑鏈嶅姟鍣�
+        // 鍙互浣跨敤 Retrofit銆乂olley 绛夌綉缁滃簱
+        Log.d(TAG, "鍙戦�� Token 鍒版湇鍔″櫒: " + token);
+        if (TextUtils.isEmpty(token)) {
+            return;
+        }
+
+        BaseEventBus bus = new BaseEventBus();
+        bus.setTopic(ConstantManage.refresh_push_token);
+        bus.setType(token);
+        EventBus.getDefault().post(bus);
+//        // 绀轰緥锛氫娇鐢� SharedPreferences 瀛樺偍 token
+//        SharedPreferences prefs = getSharedPreferences("fcm_prefs", MODE_PRIVATE);
+//        prefs.edit().putString("fcm_token", token).apply();
+    }
+
+
+    /**
+     * 搴旂敤鏄惁鍦ㄥ墠鍙�
+     *
+     * @param context 涓婁笅
+     * @return true琛ㄧず鍦ㄥ墠鍙帮紝false鍦ㄥ悗鍙�
+     */
+    private static boolean isAppInForeground(Context context) {
+        try {
+            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
+            List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
+            if (appProcesses == null) {
+                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)) || (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
+                        appProcess.processName.equals(uniPackageName))) {
+                    return true;
+                }
+            }
+            return false;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+}

--
Gitblit v1.8.0