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/CustomNotification.java | 195 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 195 insertions(+), 0 deletions(-)
diff --git a/app/src/main/java/com/hdl/photovoltaic/push/CustomNotification.java b/app/src/main/java/com/hdl/photovoltaic/push/CustomNotification.java
new file mode 100644
index 0000000..4aa2c10
--- /dev/null
+++ b/app/src/main/java/com/hdl/photovoltaic/push/CustomNotification.java
@@ -0,0 +1,195 @@
+package com.hdl.photovoltaic.push;
+
+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.RequiresApi;
+import androidx.core.app.NotificationCompat;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.hdl.photovoltaic.R;
+import com.hdl.photovoltaic.config.ConstantManage;
+import com.hdl.photovoltaic.other.HdlPushLogic;
+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.List;
+
+/**
+ * 鑷畾涔夌姸鎬佺殑娑堟伅鏍峰紡
+ */
+public class CustomNotification {
+ private static final String TAG = "CustomNotification";
+
+ private static volatile CustomNotification sCustomNotification;
+
+ /**
+ * 鑾峰彇褰撳墠瀵硅薄
+ *
+ * @return HdlDeviceLogic
+ */
+ public static synchronized CustomNotification getInstance() {
+ if (sCustomNotification == null) {
+ synchronized (CustomNotification.class) {
+ if (sCustomNotification == null) {
+ sCustomNotification = new CustomNotification();
+ }
+ }
+
+ }
+ return sCustomNotification;
+ }
+
+ /**
+ * 鎺ㄩ�佹秷鎭鐞�
+ *
+ * @param context 涓婁笅鏂�
+ * @param title 鏍囬
+ * @param content 鍐呭
+ * @param data 娑堟伅璐熻浇鏁版嵁
+ * @param fromPush 琛ㄧず鏉ヨ嚜鏋佸厜锛孎CM锛岄樋閲屼簯骞冲彴鐨勬秷鎭�
+ */
+ public void messageDataProcessing(Context context, String title, String content, String data, FromPush fromPush) {
+ Log.d(TAG, "鎺ㄩ�佹秷鎭鐞嗭細" + data);
+ if (TextUtils.isEmpty(data)) {
+ return;
+ }
+ PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing(title, content, data);
+ //鍘熺敓鍦ㄥ墠鍙版垨鑰卽ni鍦ㄥ墠鍙�
+ if (this.isAppInForeground(context)) {
+ HdlPushLogic.getInstance().PushPushCommonData(context, pushMessageInfoBean, true);
+ } else {
+ this.showCustomNotification(context, pushMessageInfoBean, fromPush);
+ }
+// this.showCustomNotification(context, pushMessageInfoBean);
+ }
+
+
+ /**
+ * 澶勭悊閫氱煡娑堟伅锛堝簲鐢ㄥ湪鍚庡彴鏃讹紝绯荤粺浼氳嚜鍔ㄦ樉绀洪�氱煡锛�
+ *
+ * @param pushMessageInfoBean 鎺ㄩ�佹暟鎹浆鎹㈡垚pushMessageInfoBean瀵硅薄
+ * @param fromPush 琛ㄧず鏉ヨ嚜鏋佸厜锛孎CM锛岄樋閲屼簯骞冲彴鐨勬秷鎭�
+ */
+ private void showCustomNotification(Context context, PushMessageInfoBean pushMessageInfoBean, FromPush fromPush) {
+ NotificationManager notificationManager =
+ (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+
+ // 鍒涘缓閫氱煡娓犻亾锛圓ndroid 8.0+锛�
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ createNotificationChannel(notificationManager);
+ }
+
+ // 鏋勫缓閫氱煡鍐呭
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "fcm_channel")
+ .setSmallIcon(R.drawable.notification_logo)
+ .setContentTitle(pushMessageInfoBean.getTitle())
+ .setContentText(pushMessageInfoBean.getContent())
+ .setAutoCancel(true)
+ .setWhen(System.currentTimeMillis());
+
+ // 璁剧疆鐐瑰嚮鎰忓浘,涓洪�氱煡娣诲姞鐐瑰嚮鍚庣殑璺宠浆鎰忓浘
+ Intent intent = createNotificationIntent(context, pushMessageInfoBean, fromPush);
+ PendingIntent pendingIntent = PendingIntent.getActivity(
+ context, 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)
+ public void createNotificationChannel(NotificationManager notificationManager) {
+ NotificationChannel channel = new NotificationChannel(
+ "push_channel",
+ "push Message",
+ NotificationManager.IMPORTANCE_HIGH
+ );
+ channel.setDescription("Firebase Cloud Messaging Notification");
+ notificationManager.createNotificationChannel(channel);
+ }
+
+ /**
+ * 鑾峰彇Intent
+ *
+ * @param context 涓婁笅鏂�
+ * @param pushMessageInfoBean 鏁版嵁妯″瀷
+ * @param fromPush 娑堟伅鏉ヨ嚜锛堟瀬鍏夛紝璋锋瓕锛岄樋閲屼簯锛夊钩鍙�
+ * @return 鎰忓浘
+ */
+ public Intent createNotificationIntent(Context context, PushMessageInfoBean pushMessageInfoBean, FromPush fromPush) {
+ Intent intent = new Intent(context, StartActivity.class);
+ if (pushMessageInfoBean != null) {
+ //浼犻�掓帹閫佹暟鎹�
+ intent.putExtra("from_message", fromPush);
+ intent.putExtra("pushData", JSONObject.toJSONString(pushMessageInfoBean));
+ }
+ //璁剧疆鏍囧織锛屾竻绌轰换鍔℃爤
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ 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鍦ㄥ悗鍙�
+ */
+ public 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;
+ }
+ }
+
+ public enum FromPush {
+ FCM,
+ AliYun,
+ JPush,
+ }
+}
--
Gitblit v1.8.0