zoukankan      html  css  js  c++  java
  • 极光推送工具类

    以下是本人开发期间整理出来的极光推送Java后台相关的工具类,给大家分享一下..

    <!- 极光pom ->        
    <dependency>
         <groupId>cn.jpush.api</groupId>
        <artifactId>jpush-client</artifactId>
        <version>3.3.10</version>
    </dependency>
    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 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: hanjt
     * @Date: 2019/2/01 11:23
     * @Description: 极光推送工具类
     */
    @Component
    @Slf4j
    public class JPushUtil {
    
        @Value("${jpush.app.key}")
        private String jpushAppKey;
        @Value("${jpush.master.secret}")
        private String jpushMasterSecret;
        @Value("${jpush.apns.prod}")
        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 Result 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 Result.newInstance(Code.ERR, e.getMessage());
            }
        }
    
        /**
         * 指定用户极光定时推送
         *
         * @param message
         * @param to
         * @param token
         * @return
         */
        public Result 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 Result.newInstance(Code.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 Result 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 Result.newInstance(Code.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 Result 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 Result.newInstance(Code.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 Result transmitFlag(String flag, String title, String message) {
            try {
                String to = JPushEnum.MESSAGE_SYSTEM.getCode();
                PushPayload payload = MessageEnum.ALL_PLATFORM.getCode().equals(flag) ? sendAllPush(title, message, to) :
                        MessageEnum.IOS.getCode().equals(flag) ? sendIosPush(title, message, to) :
                                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 Result.newInstance(Code.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;
        }
    }
  • 相关阅读:
    无线鼠标和无线键盘能不能唤醒睡眠中的电脑的解决方案
    教你如何设置同时上内外网(单网卡或双网卡)
    Oracle_字符集问题(数据库与客户端字符集关联关系)
    关于破解移动宽带光猫 型号: GS3101 超级管理员密码
    Oracle 低版本客户端连接 18c 报ORA-28040 和 ORA-01017 错误的解决方法
    sql语句分组统计出年月日下数据记录数目
    Servlet文件上传
    ActiveMq+zookeeper+levelDB集群整合配置
    mongodb 级联操作查询时,关联条件
    maven插件地址博客园
  • 原文地址:https://www.cnblogs.com/JackpotHan/p/11287515.html
Copyright © 2011-2022 走看看