百度云推送 Android+IOS

/**
 *  Android 推送
 * @param description 需要推送的消息
 * @return
 * @throws PushClientException
 * @throws PushServerException
 */
public APIResponse  androidPush(String description) throws PushClientException, PushServerException {
    // 1. get apiKey and secretKey from developer console
    String apiKey = "吐舌头wdqeqweqqeqweqw";//百度推送申请应用后产生的
    String secretKey = "eqweqwqweqeqwewqeqwqe";//百度推送申请应用后产生的
    PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
    // 2. build a BaidupushClient object to access released interfaces
    BaiduPushClient pushClient = new BaiduPushClient(pair,
            BaiduPushConstants.CHANNEL_REST_URL);

    // 3. register a YunLogHandler to get detail interacting information
    // in this request.
    pushClient.setChannelLogHandler(new YunLogHandler() {
        @Override
        public void onHandle(YunLogEvent event) {
            System.out.println(event.getMessage());
        }
    });

    try {
        // 4. specify request arguments
        JSONObject jo=new JSONObject();
        JSONObject jo1=new JSONObject();
        jo.put("title","JULI_TEST");
        jo.put("description", description);
        jo.put("notification_builder_id", 0);
        jo.put("notification_basic_style",7);
        jo.put("open_type",3);
        jo.put("custom_content",jo1.put("key","value"));
        String message=jo.toJSONString();
        PushMsgToAllRequest request = new PushMsgToAllRequest()
                .addMsgExpires(new Integer(3600))//相对于当前时间的消息过期时间,单位为秒
                .addMessageType(1)//0:透传消息 1:通知 默认值为0
                .addMessage(message) //添加透传消息
                        //.addSendTime(System.currentTimeMillis() / 1000 + 120) // 设置定时推送时间,必需超过当前时间一分钟,单位秒.实例2分钟后推送
                .addDeviceType(3);//Android

        // 5. http request
        PushMsgToAllResponse response = pushClient.pushMsgToAll(request);
        // Http请求结果解析打印

        returnMap.put("msgId", response.getMsgId());
        returnMap.put("sendTime", response.getSendTime());
        returnMap.put("timerId", response.getTimerId());
        return APIResponse.success(returnMap);
    } catch (PushClientException e) {
        if (BaiduPushConstants.ERROROPTTYPE) {
            throw e;
        } else {
            e.printStackTrace();
        }
        return APIResponse.error("Android推送失败");
    } catch (PushServerException e) {
        if (BaiduPushConstants.ERROROPTTYPE) {
            throw e;
        } else {
            System.out.println(String.format(
                    "requestId: %d, errorCode: %d, errorMessage: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }
        return APIResponse.error("Android推送失败");
    }

}

 
 
 
 
/**
 * IOS 推送
 * @param description 需要推送的消息
 * @return
 * @throws PushClientException
 * @throws PushServerException
 */
public APIResponse IOSPush(String description) throws PushClientException, PushServerException {
    // 1. get apiKey and secretKey from developer console
    String apiKey = "sdasdsadasasdasd"; //百度推送申请应用后产生的
    String secretKey = "asdasdadadasaddada";//百度推送申请应用后产生的
    PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
    // 2. build a BaidupushClient object to access released interfaces
    BaiduPushClient pushClient = new BaiduPushClient(pair,
            BaiduPushConstants.CHANNEL_REST_URL);

    // 3. register a YunLogHandler to get detail interacting information
    // in this request.
    pushClient.setChannelLogHandler(new YunLogHandler() {
        @Override
        public void onHandle(YunLogEvent event) {
            System.out.println(event.getMessage());
        }
    });

    try {
        // 4. specify request arguments
        JSONObject jo=new JSONObject();
        JSONObject jo1=new JSONObject();
        jo.put("title","hello");
        jo.put("description", description);
        jo.put("notification_builder_id", 0);
        jo.put("notification_basic_style",7);
        jo.put("open_type",3);
        jo.put("custom_content",jo1.put("key","value"));
        String message=jo.toJSONString();
        PushMsgToAllRequest request = new PushMsgToAllRequest()
                .addMsgExpires(new Integer(3600))//相对于当前时间的消息过期时间,单位为秒
                .addMessageType(1)//0:透传消息 1:通知 默认值为0
                .addMessage(message) //添加透传消息
                        //.addSendTime(System.currentTimeMillis() / 1000 + 120) // 设置定时推送时间,必需超过当前时间一分钟,单位秒.实例2分钟后推送
                .addDeviceType(4);//IOS
        // 5. http request
        PushMsgToAllResponse response = pushClient.pushMsgToAll(request);
        // Http请求结果解析打印

        returnMap.put("msgId", response.getMsgId());
        returnMap.put("sendTime", response.getSendTime());
        returnMap.put("timerId", response.getTimerId());
        return APIResponse.success(returnMap);
    } catch (PushClientException e) {
        if (BaiduPushConstants.ERROROPTTYPE) {
            throw e;
        } else {
            e.printStackTrace();
        }
        return APIResponse.error("IOS推送失败");
    } catch (PushServerException e) {
        if (BaiduPushConstants.ERROROPTTYPE) {
            throw e;
        } else {
            System.out.println(String.format(
                    "requestId: %d, errorCode: %d, errorMessage: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }
        return APIResponse.error("IOS推送失败");
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值