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,
|
}
|
}
|