百度云推送 Android+IOS

来源:互联网 发布:刀路编程工资高吗 编辑:程序博客网 时间:2024/05/18 16:59
/** *  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
原创粉丝点击