百度云推送Java端.docx_第1页
百度云推送Java端.docx_第2页
百度云推送Java端.docx_第3页
百度云推送Java端.docx_第4页
免费预览已结束,剩余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.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 (ChannelSe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论