1、package net;import com.baidu.yun.channel.auth.ChannelKeyPair;import com.baidu.yun.channel.client.BaiduChannelClient;import com.baidu.yun.channel.exception.ChannelClientException;import com.baidu.yun.channel.exception.ChannelServerException;import com.baidu.yun.channel.model.PushBroadcastMessageReque
2、st;import com.baidu.yun.channel.model.PushBroadcastMessageResponse;import com.baidu.yun.channel.model.PushUnicastMessageRequest;import com.baidu.yun.channel.model.PushUnicastMessageResponse;import com.baidu.yun.core.log.YunLogEvent;import com.baidu.yun.core.log.YunLogHandler;public class TestPushMes
3、sage /* 测试推送*/public static void main(String args) String string = “title“:“你有新消息“,“description“:“ 今天下午全体员工发奖金“,“+ “notification_builder_id“:0,“notification_basic_style“:2,“custom_content“: “id“:“485446“,“open_type“:“2“;/String string =“hello“;/int status = pushBroadcastMessage(string,3,1);int statu
4、s = pushOnlyPleploMessage(string,3,1);System.out.println(status);/* 初始化百度推送*/private static BaiduChannelClient initPushClient()/这两个 key 自己申请String apiKey = “自己申请的 apikey“;String secretKey = “自己申请的 secretKey“;/ 1. 设置 developer 平台的 ApiKey/SecretKey ChannelKeyPair pair = new ChannelKeyPair(apiKey, secr
5、etKey); / 2. 创建 BaiduChannelClient 对象实例 BaiduChannelClient channelClient = new BaiduChannelClient(pair); / 3. 若要了解交互细节,请注册 YunLogHandler 类 channelClient.setChannelLogHandler(new YunLogHandler() Override public void onHandle(YunLogEvent event) System.out.println(event.getMessage(); );return channelCl
6、ient;/* 用百度推送向所有人发送消息或者通知*/public static int pushBroadcastMessage(String Content,int devType,int pushType) BaiduChannelClient channelClient = initPushClient(); try / 4. 创建请求类对象 PushBroadcastMessageRequest request = new PushBroadcastMessageRequest(); / devType = 1: web 2: pc 3:android 4:ios 5:wpreque
7、st.setDeviceType(devType);/ pushType 为 0 设置发送消息,为 1 设置发送通知request.setMessageType(pushType);request.setMessage(Content);/ 5. 调用 pushMessage 接口 PushBroadcastMessageResponse response = channelClient .pushBroadcastMessage(request);/ 6. 认证推送成功 System.out.println(“认证推送成功 push amount : “ + response.getSucc
8、essAmount(); return response.getSuccessAmount(); catch (ChannelClientException e) / 处理客户端错误异常 e.printStackTrace(); return 2; catch (ChannelServerException e) / 处理服务端错误异常 System.out.println(String.format( “request_id: %d, error_code: %d, error_message: %s“, e.getRequestId(), e.getErrorCode(), e.getEr
9、rorMsg(); return 3;/* 向单个人推送消息或者通知* return*/public static int pushOnlyPleploMessage(String Content,int devType,int pushType)BaiduChannelClient channelClient = initPushClient();try / 4. 创建请求类对象/ 手机端的 ChannelId, 手机端的 UserId, 用户需替换为自己的PushUnicastMessageRequest request = new PushUnicastMessageRequest();
10、/ devType = 1: web 2: pc 3:android 4:ios 5:wprequest.setDeviceType(devType);/ChannelId、UserId 这两个为每个手机绑定的,根据需要自己填写,暂用 11111111111111 代替request.setChannelId(111111111111L);request.setUserId(“111111111111111111“);/ request.setMessageType(pushType); /pushType 为 0 时发送消息 / request.setMessage(“Hello Chann
11、el“); request.setMessageType(pushType);/ pushType 为 1 时发送通知request.setMessage(Content);/ 5. 调用 pushMessage 接口PushUnicastMessageResponse response = channelClient.pushUnicastMessage(request);/ 6. 认证推送成功System.out.println(“认证推送成功 push amount : “ + response.getSuccessAmount();return response.getSuccessAmount(); catch (ChannelClientException e) / 处理客户端错误异常e.printStackTrace();return 2; catch (ChannelServerException e) / 处理服务端错误异常System.out.println(String.format(“request_id: %d, error_code: %d, error_message: %s“,e.getRequestId(), e.getErrorCode(), e.getErrorMsg();return 3;
Copyright © 2018-2021 Wenke99.com All rights reserved
工信部备案号:浙ICP备20026746号-2
公安局备案号:浙公网安备33038302330469号
本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。