收藏 分销(赏)

百度云推送Java端.docx

上传人:xrp****65 文档编号:6116410 上传时间:2024-11-28 格式:DOCX 页数:5 大小:14.46KB
下载 相关 举报
百度云推送Java端.docx_第1页
第1页 / 共5页
百度云推送Java端.docx_第2页
第2页 / 共5页
点击查看更多>>
资源描述
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.PushBroadcastMessageRequest; 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 TestPushMessage { /** * 测试推送 */ 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 status = 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, secretKey); // 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 channelClient; } /** * 用百度推送向所有人发送消息或者通知 */ 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:wp request.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.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; } } /** * 向单个人推送消息或者通知 * @return */ public static int pushOnlyPleploMessage(String Content,int devType,int pushType){ BaiduChannelClient channelClient = initPushClient(); try { // 4. 创建请求类对象 // 手机端的ChannelId, 手机端的UserId, 用户需替换为自己的 PushUnicastMessageRequest request = new PushUnicastMessageRequest(); // devType => 1: web 2: pc 3:android 4:ios 5:wp request.setDeviceType(devType); //ChannelId、UserId这两个为每个手机绑定的,根据需要自己填写,暂用11111111111111代替 request.setChannelId(111111111111L); request.setUserId("111111111111111111"); // request.setMessageType(pushType); //pushType为0时发送消息 // request.setMessage("Hello Channel"); 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; } } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 环境建筑 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服