JPushServiceImpl.java 4.77 KB
package com.diligrp.rider.service.impl;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.diligrp.rider.config.JPushProperties;
import com.diligrp.rider.service.JPushService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class JPushServiceImpl implements JPushService {

    private final JPushClient jPushClient;
    private final JPushProperties properties;

    @Override
    public void pushToRider(Long riderId, String title, String content, String bizType, Long bizId) {
        if (!properties.isEnabled()) {
            log.debug("JPush 未启用,跳过推送 riderId={} title={}", riderId, title);
            return;
        }
        if (riderId == null || riderId <= 0) {
            log.warn("riderId 无效,跳过推送 riderId={}", riderId);
            return;
        }
        try {
            PushPayload payload = buildPayload(
                    Audience.alias(String.valueOf(riderId)),
                    title, content, bizType, bizId);
            PushResult result = jPushClient.sendPush(payload);
            log.info("JPush 推送成功 riderId={} msgId={} sendno={}",
                    riderId, result.msg_id, result.sendno);
        } catch (APIConnectionException e) {
            log.error("JPush 网络异常 riderId={}", riderId, e);
        } catch (APIRequestException e) {
            log.error("JPush 业务异常 riderId={} status={} errorCode={} msg={}",
                    riderId, e.getStatus(), e.getErrorCode(), e.getErrorMessage());
        } catch (Exception e) {
            log.error("JPush 推送未知异常 riderId={}", riderId, e);
        }
    }

    @Override
    public void pushToCity(Long cityId, String title, String content, String bizType) {
        if (!properties.isEnabled()) {
            log.debug("JPush 未启用,跳过广播 cityId={} title={}", cityId, title);
            return;
        }
        if (cityId == null || cityId <= 0) {
            log.warn("cityId 无效,跳过广播 cityId={}", cityId);
            return;
        }
        try {
            PushPayload payload = buildPayload(
                    Audience.tag("city_" + cityId),
                    title, content, bizType, 0L);
            PushResult result = jPushClient.sendPush(payload);
            log.info("JPush 城市广播成功 cityId={} msgId={}", cityId, result.msg_id);
        } catch (APIConnectionException e) {
            log.error("JPush 城市广播网络异常 cityId={}", cityId, e);
        } catch (APIRequestException e) {
            log.error("JPush 城市广播业务异常 cityId={} status={} errorCode={} msg={}",
                    cityId, e.getStatus(), e.getErrorCode(), e.getErrorMessage());
        } catch (Exception e) {
            log.error("JPush 城市广播未知异常 cityId={}", cityId, e);
        }
    }

    private PushPayload buildPayload(Audience audience, String title, String content,
                                     String bizType, Long bizId) {
        AndroidNotification.Builder androidBuilder = AndroidNotification.newBuilder()
                .setAlert(content)
                .setTitle(title);
        IosNotification.Builder iosBuilder = IosNotification.newBuilder()
                .setAlert(content)
                .incrBadge(1)
                .setSound("default");

        if (bizType != null && !bizType.isEmpty()) {
            androidBuilder.addExtra("bizType", bizType);
            iosBuilder.addExtra("bizType", bizType);
        }
        if (bizId != null) {
            androidBuilder.addExtra("bizId", String.valueOf(bizId));
            iosBuilder.addExtra("bizId", String.valueOf(bizId));
        }

        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(audience)
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(androidBuilder.build())
                        .addPlatformNotification(iosBuilder.build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(properties.isApnsProduction())
                        .setTimeToLive(properties.getTimeToLive())
                        .build())
                .build();
    }
}