zoukankan      html  css  js  c++  java
  • jpush 延迟推送的栗子

      这个 推送是上个月做的, 上线之后没有问题,所以,我就把 这个整套代码 整理一哈,方便以后使用。

         首先需求是这样的:  在比赛结束后的 10 钟 开始给 参加比赛的球员  发送消息,告诉他们比赛的信息和视频都已经 整理完成了,请用户观看查询。

        其实没有什么特别的内容,只是调用一下jpush的接口而已, 还要对之前的参数和接口调通之后的参数 判断分析。

           

    // 比赛结束的推送消息
        @Override
        public void sendMessageForGameOver(String gameId) {
    
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(100);
            fixedThreadPool.execute(new MessageForGameOver(gameId));
        }
     class MessageForGameOver implements Runnable {
            private String  gameId;
            public MessageForGameOver(String  gameId) {
                this.gameId = gameId;
            }
            public void startEvent(){
                try{
                    //获取主队的所有球员的Id
                    Game gameOverDto=gameService.getGameRaw(gameId);
                    String homeTeamId = gameOverDto.getHomeTeamId();
                    List<String> homeTeamUserIds = getUserIdsByTeamId(homeTeamId);
                    //获取客队的所有球员的Id
                    String guestTeamId = gameOverDto.getGuestTeamId();
                    List<String> guestTeamUserIds = getUserIdsByTeamId(guestTeamId);
    
                    PushMessageDto homeTeamDto = genPushMessageDtoService.genPushMessageDtoForGameOver(gameOverDto, homeTeamUserIds, gameOverDto.getGuestTeamId());
                    PushMessageDto guestTeamDto = genPushMessageDtoService.genPushMessageDtoForGameOver(gameOverDto, guestTeamUserIds, gameOverDto.getHomeTeamId());
                    PushMessage pushMessage = new PushMessage();
                    pushMessage.setBizid(gameId);
                    pushMessage.setBiztype(2);
                    pushMessage.setProcessStatus(2);
                    pushMessage.setProcessCount(0);
                    pushMessage.setCreatedTime(new Date());
                    pushMessage.setUpdatedTime(new Date());
                    PushMessage message = pushMessageService.insertSelective(pushMessage);
    
                    saveMessageDetailService.saveMessageDetailForGameOver(gameOverDto); // 消息内容保存到本地的
                    sendMessageToJpush(homeTeamDto, guestTeamDto, message); // 发送消息
                }catch (Exception e){
                    log.error("消息发送出错"+e.getMessage());
                }
            }
    
            @Override
            public void run() {  // 线程 启动  延迟 10 分钟
                try {
                    Thread.sleep(10*60*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startEvent();
            }
        }
    

      

     private void sendMessageToJpush(PushMessageDto homeTeamDto, PushMessageDto guestTeamDto, PushMessage message) {
            Boolean flag = false;
    
            if (homeTeamDto != null) {
                CallBackMessage callBackMessage = sendMessage(homeTeamDto);   // 这里主要是这个方法来发送消息
                if (callBackMessage.getCode() == 200) {
                    flag = true;
                    message.setProcessCount(1);
                    message.setProcessStatus(3);
                    pushMessageService.updateByPrimaryKeySelective(message);
                }else {
                    message.setProcessCount(1);
                    message.setProcessStatus(4);
                    message.setFailureReason(callBackMessage.getErrorMessage());
                    pushMessageService.updateByPrimaryKeySelective(message);
                }
            }
    
            if (guestTeamDto != null) {
                CallBackMessage callBackMessage = sendMessage(guestTeamDto);
                if (callBackMessage.getCode() == 200) {
                    message.setProcessCount(1);
                    message.setProcessStatus(3);
                    pushMessageService.updateByPrimaryKeySelective(message);
                }else{
                    message.setProcessCount(1);
                    message.setProcessStatus(4);
                    message.setFailureReason(callBackMessage.getErrorMessage());
                    pushMessageService.updateByPrimaryKeySelective(message);
                }
            }
        }
    

      

     @Override
        public CallBackMessage sendMessage(PushMessageDto pushMessageDto) { // 发送消息的地方
            JPushClient jPushClient;
            if ("basketball".equals(pushMessageDto.getSportType())) {
                jPushClient = new JPushClient(SECRET, APP_KEY, 3);  // jpush 提供的类 
            } else if ("football".equals(pushMessageDto.getSportType())) {
                jPushClient = new JPushClient(SECRET_NEW, APP_KEY_NEW, 3);
            } else {
                String errMsg = "Connection error, unknown sport type, cannot choose app key. Sport type: " +
                        pushMessageDto.getSportType();
                log.error(errMsg);
                return new CallBackMessage(404, "error", errMsg);
            }
            PushPayload payload = buildPushObject_ios_audienceMore_messageWithExtras(pushMessageDto);
            try {
                if (payload != null) {
                    Iterator it = pushMessageDto.getUserIds().iterator();
                    String value = null;
                    while (it.hasNext()){
                        value = (String)it.next();
                        System.out.println(value);
                        log.info("JPush发送get用户,用户ID 为"+value);
                    }
                    PushResult result = jPushClient.sendPush(payload);  //  发送到jpush  
                    return new CallBackMessage(200, "ok", "success");
                }
                return new CallBackMessage(500, "error", "初始化参数失败,这些用户都没有设置过alias!");
            } catch (APIConnectionException e) {
                log.error("Connection error, should retry later,errorMessage:" + e.getMessage());
                return new CallBackMessage(404, "error", "Connection error, should retry later,errorMessage" + e.getMessage());
            } catch (APIRequestException e) {
                log.error("HTTP Status: " + e.getStatus());
                log.error("Error Code: " + e.getErrorCode());
                log.error("Error Message: " + e.getErrorMessage());
                return new CallBackMessage(e.getErrorCode(), e.getStatus() + "", e.getErrorMessage());
            }
        }
    

      

  • 相关阅读:
    使用Regex.Replace只替换字符串一次
    Socket
    [转载]ASP.NET中在不同的子域中共享Session
    C#构造函数
    C# 的 ArrayList
    [转]决定何时使用 DataGrid、DataList 或 Repeater
    window.showModalDialog弹出对话框刷新问题
    ASP.NET] 选择文件夹的对话框
    网页打印javascript:window.print()
    开展工作
  • 原文地址:https://www.cnblogs.com/murong/p/6401149.html
Copyright © 2011-2022 走看看