wjc
4 天以前 e3b63c3a02fd80641733daf27e79b40ff081a046
app/src/main/java/com/hdl/photovoltaic/push/fcm/MyFirebaseMessagingService.java
@@ -26,6 +26,7 @@
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;
@@ -57,9 +58,6 @@
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "在前台还是在后" + isAppInForeground(this));
        Log.d(TAG, "来自RemoteMessage: " + JSON.toJSONString(remoteMessage));
        Log.d(TAG, "来自: " + remoteMessage.getFrom());
//        // 数据消息:任何状态都会执行
//        if (!remoteMessage.getData().isEmpty()) {
//            handleDataMessage(remoteMessage.getData()); // 总是执行
@@ -69,19 +67,24 @@
//        if (remoteMessage.getNotification() != null) {
//            handleNotification(remoteMessage.getNotification()); // 只有前台执行
//        }
        if (isAppInForeground(this)) {
        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("", "", JSON.toJSONString(remoteMessage.getData()));
                PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing(title, content, JSON.toJSONString(remoteMessage.getData()));
                HdlPushLogic.getInstance().PushPushCommonData(this, pushMessageInfoBean, false);
//            Log.d(TAG, "消息数据: " + remoteMessage.getData());
//            handleDataMessage(remoteMessage.getData());
//            this.showCustomNotification(pushMessageInfoBean);
            }
        } else {
            // 检查消息是否包含通知负载
            if (!remoteMessage.getData().isEmpty()) {
                PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing("", "", JSON.toJSONString(remoteMessage.getData()));
                PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing(title, content, JSON.toJSONString(remoteMessage.getData()));
                this.showCustomNotification(pushMessageInfoBean);
            }
        }
@@ -102,31 +105,33 @@
            createNotificationChannel(notificationManager);
        }
        // 构建通知内容
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "fcm_channel")
                .setSmallIcon(R.drawable.desktop_logo)
                .setSmallIcon(R.drawable.notification_logo)
                .setContentTitle(pushMessageInfoBean.getTitle())
                .setContentText(pushMessageInfoBean.getContent())
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis());
        // 设置点击意图
        // 设置点击意图,为通知添加点击后的跳转意图
        Intent intent = createNotificationIntent(pushMessageInfoBean.getExpandData());
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        builder.setContentIntent(pendingIntent);
        // 显示通知
        notificationManager.notify((int) System.currentTimeMillis(), builder.build());
        // 发送通知,通知栏显示通知
        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 消息",
                "FCM Message",
                NotificationManager.IMPORTANCE_HIGH
        );
        channel.setDescription("Firebase Cloud Messaging 通知");
        channel.setDescription("Firebase Cloud Messaging Notification");
        notificationManager.createNotificationChannel(channel);
    }
@@ -145,8 +150,7 @@
        if (TextUtils.isEmpty(token)) {
            return;
        }
        UserConfigManage.getInstance().setPushToken(token);
        UserConfigManage.getInstance().Save();
        BaseEventBus bus = new BaseEventBus();
        bus.setTopic(ConstantManage.refresh_push_token);
        bus.setType(token);