wjc
1 天以前 b48b2ecc0882fb68ae0c6bd631bb5b913fe1428c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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 表示来自极光,FCM,阿里云平台的消息
     */
    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);
        //原生在前台或者uni在前台
        if (this.isAppInForegroundOrIsShowUniMP(context)) {
            HdlPushLogic.getInstance().PushPushCommonData(context, pushMessageInfoBean, false);
        } else {
            this.showCustomNotification(context, pushMessageInfoBean, fromPush);
        }
//        this.showCustomNotification(context, pushMessageInfoBean);
    }
 
    /**
     * 是否在前台  true表示在前台,false表示在后台
     *
     * @param context 上下文
     * @return true表示在前台,false表示在后台
     */
    public boolean isAppInForegroundOrIsShowUniMP(Context context) {
        return (this.isAppInForeground(context) || HDLUniMPSDKManager.getInstance().showUniMP());
    }
 
    /**
     * 处理通知消息(应用在后台时,系统会自动显示通知)
     *
     * @param pushMessageInfoBean 推送数据转换成pushMessageInfoBean对象
     * @param fromPush            表示来自极光,FCM,阿里云平台的消息
     */
    private void showCustomNotification(Context context, PushMessageInfoBean pushMessageInfoBean, FromPush fromPush) {
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
 
        // 创建通知渠道(Android 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、Volley 等网络库
        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 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();
            for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
                        appProcess.processName.equals(packageName)) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }
 
    public enum FromPush {
        FCM,
        AliYun,
        JPush,
    }
}