极光推送工具类

package com.jackpot.message;

import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
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 cn.jpush.api.schedule.ScheduleResult;
import com.alibaba.fastjson.JSON;
import com.zmkj.base.util.StringUtils;
import com.zmkj.core.enums.JPushEnum;
import com.zmkj.core.enums.MessageEnum;
import com.zmkj.core.zm.ZmCode;
import com.zmkj.core.zm.ZmResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author: jackpot
 * @Date: 2019/2/01 11:23
 * @Description: 极光消息推送
 */
@Component
@Slf4j
public class JPushMessage {

    @Value("${your appKey}")
    private String jpushAppKey;
    @Value("${your masterSecret}")
    private String jpushMasterSecret;
    @Value("${your apnsProd}")
    private boolean apnsProd;

    private JPushClient jpushClient = new JPushClient(jpushMasterSecret, jpushAppKey, null, ClientConfig.getInstance());
    private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    /**
     * 指定用户极光推送
     *
     * @param message
     * @param to
     * @param token
     * @return
     */
    public ZmResult push(String message, String to, String... token) {
        try {
            PushPayload payload = pushAllNotify(message, to, token);
            PushResult result = jpushClient.sendPush(payload);
            log.info(JSON.toJSONString(result));
            return null;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ZmResult.newInstance(ZmCode.ERR, e.getMessage());
        }
    }

    /**
     * 指定用户极光定时推送
     *
     * @param message
     * @param to
     * @param token
     * @return
     */
    public ZmResult schedulePush(String title, String message, String to, LocalDateTime time, String... token) {
        try {
            PushPayload push = pushAllNotify(message, to, token);
            ScheduleResult result = jpushClient.createSingleSchedule(title, dtf.format(time), push);
            log.info(JSON.toJSONString(result));
            return null;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ZmResult.newInstance(ZmCode.ERR, e.getMessage());
        }
    }

    /**
     * 全平台指定用户推送
     *
     * @param message
     * @param to
     * @param token
     * @return
     */
    public PushPayload pushAllNotify(String message, String to, String... token) {
        PushPayload.Builder payload = PushPayload.newBuilder()
                .setPlatform(Platform.all());
        payload.setAudience(StringUtils.isEmpty(token) ? Audience.all() : Audience.alias(token));
        payload.setNotification(Notification.newBuilder().setAlert(message)
                .addPlatformNotification(IosNotification.newBuilder().addExtra("to", to).build())
                .addPlatformNotification(AndroidNotification.newBuilder().addExtra("to", to).build())
                .build()
        );
        if (apnsProd) payload.setOptions(Options.newBuilder().setApnsProduction(true).build());
        return payload.build();
    }

    /**
     * 指定平台全用户推送
     * time不为空--定时推送
     *
     * @param flag(1:Android,2:IOS,3:全部)
     * @param message
     * @return
     */
    public ZmResult pushFlag(Integer flag, String title, String message, LocalDateTime time) {
        try {
            String to = JPushEnum.MESSAGE_SYSTEM.getCode();
            PushPayload payload = MessageEnum.ALL_PLATFORM.getCode().equals(flag) ? pushAllNotify(message, to) :
                    MessageEnum.IOS.getCode().equals(flag) ? pushIosNotify(message, to) :
                            pushAndroidNotify(message, to);
            //时间为空则为正常推送
            if (StringUtils.isEmpty(time)) {
                PushResult result = jpushClient.sendPush(payload);
                log.info(JSON.toJSONString(result));
            }
            //时间不为则为定时推送
            else {
                ScheduleResult result = jpushClient.createSingleSchedule(title, dtf.format(time), payload);
                log.info(JSON.toJSONString(result));
            }
            return null;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ZmResult.newInstance(ZmCode.ERR, e.getMessage());
        }
    }

    /**
     * 安卓全平台推送
     *
     * @param message
     * @param to
     * @return
     */
    private PushPayload pushAndroidNotify(String message, String to) {
        PushPayload.Builder payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(
                        Notification.newBuilder().addPlatformNotification(
                                AndroidNotification.newBuilder().setAlert(message).addExtra("to", to).build()
                        ).build()
                );
        return payload.build();
    }

    /**
     * ios全平台推送
     *
     * @param message
     * @param to
     * @return
     */
    private PushPayload pushIosNotify(String message, String to) {
        PushPayload.Builder payload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(
                        Notification.newBuilder().addPlatformNotification(
                                IosNotification.newBuilder().setAlert(message).addExtra("to", to).build()
                        ).build()
                );
        if (apnsProd) payload.setOptions(Options.newBuilder().setApnsProduction(true).build());
        return payload.build();
    }

    /**
     * 指定用户极光透传
     *
     * @param message
     * @param to
     * @param token
     * @return
     */
    public ZmResult transmit(String title, String message, String to, String... token) {
        try {
            PushPayload payload = sendAllPush(title, message, to, token);
            PushResult result = jpushClient.sendPush(payload);
            log.info(JSON.toJSONString(result));
            return null;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ZmResult.newInstance(ZmCode.ERR, e.getMessage());
        }
    }

    /**
     * 全平台指定用户极光透传
     *
     * @param title   标题
     * @param message 消息内容
     * @param to      app跳转表识
     * @return
     */
    public PushPayload sendAllPush(String title, String message, String to, String... token) {
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(StringUtils.isEmpty(token) ? Audience.all() : Audience.alias("token"))
                .setMessage(Message.newBuilder()
                        .setMsgContent(message)
                        .setTitle(title)
                        .addExtra("to", to)
                        .build())
                .build();
        if (apnsProd)
            payload.resetOptionsApnsProduction(true);
        return payload;
    }


    /**
     * 指定平台全用户透传
     *
     * @param flag(1:Android,2:IOS,3:全平台)
     * @param message
     * @return
     */
    public ZmResult transmitFlag(String flag, String title, String message) {
        try {
            PushPayload payload = null;
            String to = JPushEnum.MESSAGE_SYSTEM.getCode();
            //全平台
            if (flag.equals("3")) {
                payload = sendAllPush(title, message, to);
            }
            //ios
            else if (flag.equals("1")) {
                payload = sendIosPush(title, message, to);
            }
            //安卓
            else if (flag.equals("2")) {
                payload = sendAndroidPush(title, message, to);
            }
            PushResult result = jpushClient.sendPush(payload);
            log.info(JSON.toJSONString(result));
            return null;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ZmResult.newInstance(ZmCode.ERR, e.getMessage());
        }
    }

    /**
     * 安卓全平台透传
     *
     * @param message
     * @param to
     * @return
     */
    private PushPayload sendAndroidPush(String title, String message, String to) {
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setMessage(Message.newBuilder()
                        .setMsgContent(message)
                        .setTitle(title)
                        .addExtra("to", to)
                        .build()).build();
        return payload;
    }

    /**
     * ios全平台透传
     *
     * @param message
     * @param to
     * @return
     */
    private PushPayload sendIosPush(String title, String message, String to) {
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setMessage(Message.newBuilder()
                        .setMsgContent(message)
                        .setTitle(title)
                        .addExtra("to", to)
                        .build()).build();
        if (apnsProd) payload.resetOptionsApnsProduction(true);
        return payload;
    }
}

猜你喜欢

转载自www.cnblogs.com/JackpotHan/p/11287515.html
今日推荐