• 极光推送Service


    1、配置类

    @Data
    @Component
    @ConfigurationProperties(prefix = "jpush")
    public class JpushProperties {
    
        /**
         * 应用的key(非登录账号)
         */
        private String appKey = "099f9ed5324329b15fc0437";
    
        /**
         * 应用的密码(非登录密码)
         */
        private String masterSecret = "cd15b44475s4f00609010ade";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2、服务类

    
    import cn.hutool.core.collection.CollUtil;
    import cn.hutool.core.io.FileUtil;
    import cn.jiguang.common.ClientConfig;
    import cn.jpush.api.JPushClient;
    import cn.jpush.api.file.FileClient;
    import cn.jpush.api.file.model.FileType;
    import cn.jpush.api.file.model.FileUploadResult;
    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 com.zdxf.common.utils.StringUtils;
    import com.zdxf.common.utils.push.properties.JpushProperties;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Component;
    import org.springframework.util.CollectionUtils;
    
    import java.nio.charset.Charset;
    import java.util.*;
    import java.util.stream.Collectors;
    
    /**
     * @Author: zj
     * @Date: 2023/10/30 0030 14:04
     * @Description: 极光推送
     */
    @Slf4j
    @Component
    public class PushClientService {
    
        private final JPushClient jPushClient;
        private final FileClient fileClient;
    
        public PushClientService(JpushProperties properties) {
            ClientConfig clientConfig = ClientConfig.getInstance();
            jPushClient = new JPushClient(properties.getMasterSecret(), properties.getAppKey(), null, clientConfig);
            fileClient = new FileClient(properties.getMasterSecret(), properties.getAppKey());
        }
    
        /**
         * 发送给注册用户
         *
         * @param notificationTitle 通知内容标题
         * @param msgTitle          消息内容标题
         * @param msgContent        消息内容
         * @param registrationIds 设备注册id
         */
        public  void push(String notificationTitle, String msgTitle, String msgContent,List<String> registrationIds) {
            Map<String, String> extrasParam  = new HashMap<>(2);
            extrasParam.put("newXin", notificationTitle);
            push(notificationTitle, msgTitle,msgContent,extrasParam, null, registrationIds);
        }
    
    
        /**
         * 发送给注册用户
         *
         * @param notificationTitle 通知内容标题
         * @param msgTitle          消息内容标题
         * @param msgContent        消息内容
         * @param extrasParam        扩展字段
         * @param cid 消息id
         * @param registrationIds 设备注册id
         */
        public  void push(String notificationTitle, String msgTitle, String msgContent, Map<String, String> extrasParam,
                          String cid, List<String> registrationIds) {
            if (CollectionUtils.isEmpty(registrationIds)) {return;}
            registrationIds = registrationIds.stream().filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
            if (CollectionUtils.isEmpty(registrationIds)) {return;}
            try {
                Audience audience = Audience.registrationId(registrationIds);
                PushPayload pushPayload = buildPushPayload(notificationTitle, msgTitle, msgContent, extrasParam, cid,audience);
                log.info("推送参数:{}", pushPayload.toString());
                PushResult pushResult = jPushClient.sendPush(pushPayload);
                log.info("推送结果:{}", pushResult);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("极光推送异常:{}", e.getMessage());
            }
        }
    
        /**
         *   当数据太大,用文件的方式发送
         * @param notificationTitle 通知内容标题
         * @param msgTitle          消息内容标题
         * @param msgContent        消息内容
         * @param extrasParam        扩展字段
         * @param cid 消息id
         * @param registrationIds 设备初始化id
         */
        public  void pushFile(String notificationTitle, String msgTitle, String msgContent, Map<String, String> extrasParam,
                              String cid, List<String> registrationIds) {
            try {
                // 上传文件:写入本地、上传极光、最后推送
                String path = "/home/watch_platform/tmp_file/" + UUID.randomUUID()+".txt";
                FileUtil.writeLines(registrationIds, path, Charset.defaultCharset());
                FileUploadResult fileUploadResult = fileClient.uploadFile(FileType.REGISTRATION_ID, path);
                Audience audience = Audience.file(fileUploadResult.getFile_id());
                PushPayload pushPayload = buildPushPayload(notificationTitle, msgTitle, msgContent, extrasParam, cid, audience);
                log.info("推送参数:{}", pushPayload.toString());
                PushResult pushResult = jPushClient.sendFilePush(pushPayload);
                log.info("推送结果:{}", pushResult);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("极光推送异常:{}", e.getMessage());
            }
        }
    
        /**
         * 推送参数
         *
         * @param notificationTitle 通知内容标题
         * @param msgTitle 消息内容标题
         * @param msgContent 消息内容
         * @param extrasParam 扩展字段
         * @param cid 消息id
         * @param audience 推送设备
         * @return 推送参数
         */
        private  PushPayload buildPushPayload(String notificationTitle, String msgTitle, String msgContent,
                                              Map<String, String> extrasParam, String cid,  Audience audience) {
    
            return PushPayload.newBuilder()
                    //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
                    .setPlatform(Platform.android_ios())
                    //设置发送消息id
                    //  .setCid(cid)
                    //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
                    .setAudience(audience)
                    //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
                    .setNotification(Notification.newBuilder()
                        .setAlert(msgTitle)
                        //指定当前推送的android通知
                        .addPlatformNotification(AndroidNotification.newBuilder()
                            .setTitle(notificationTitle)
                            //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                            .addExtras(extrasParam)
                            .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                            .incrBadge(1)
                            .addExtras(extrasParam).build())
                        .build()
                    )
                    // Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
                    // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
                    // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
                    .setMessage(Message.newBuilder()
                            .setMsgContent(msgContent)
                            .setTitle(msgTitle)
                            .addExtras(extrasParam)
                            .build())
                    .setOptions(Options.newBuilder()
                            //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                            .setApnsProduction(false)
                            //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
    //                        .setSendno(1)
                            //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                            .setTimeToLive(43200)
                            .build())
                    .build();
        }
    
        /**
         * 发送测试
         *
         * @param args
         */
        public static void main(String[] args) {
            String title = "声音侦测报警";
            String msgTitle = "孔明设备在15:17发现了声音侦测报警!";
            Map<String, String> extrasParam  = new HashMap<>(2);
            extrasParam.put("notificationTitle", "我是扩展的json数据");
            String cid = String.valueOf(System.currentTimeMillis());
            List<String> registrationIds = CollUtil.newArrayList("1114a89fsd32b1854");
            PushClientService pushClientService = new PushClientService(new JpushProperties());
            pushClientService.push(title, msgTitle, msgTitle, extrasParam, cid, registrationIds);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
  • 相关阅读:
    vant_ CountDown倒计时
    RabbitMq消息模型-队列消息
    如何解决d3dcompiler_43.dll丢失问题?d3dcompiler_43.dll丢失修复方法
    KT6368A蓝牙模块关于IOS系统或者手机获取蓝牙mac地址的方法说明V1
    Git 版本控制工具
    [C语言] 自制的贪吃蛇游戏
    odata expand
    【Java 基础篇】Java 对象序列化流详解
    DDR PCB设计布线时,拓扑结构的选择
    学会规则引擎Drools,让你早点下班
  • 原文地址:https://blog.csdn.net/qq_28392947/article/details/134334326