[Push]百度消息推送的应用
来源:互联网 发布:手机报警软件 编辑:程序博客网 时间:2024/05/16 17:04
推送技术是指通过客户端与服务器端建立长链接,客户端可以接收由服务器端不定时发送的消息,在客户机/服务器的应用程序中,推送技术能够向客户机传送数据而无需其发出请求,例如发送电子邮件。相比较而言,万维网却是基于拉技术(Pull Technology),因此客户机浏览器必须事先向网页发出请求,所需信息才能被传送过来。
手机推送服务的原理很简单,就是通过建立一条手机与服务器的连接链路,当有消息需要发送到手机时,通过此链路发送即可
/** * 初始化百度推送 * @return */public static BaiduPushClient initPushClient(){String apiKey=""; String secretKey=""; //需要去百度云推送平台申请PushKeyPair pair = new PushKeyPair(apiKey, secretKey); //设置两个属性BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);//实例化BaiduPushClient对象pushClient.setChannelLogHandler(new YunLogHandler() {@Overridepublic void onHandle(YunLogEvent arg0) {System.out.println(arg0.getMessage());}});return pushClient;}/** * 消息推送 * @return */public static int pushOnlyMessage(String content,int pushType,int devType){BaiduPushClient pushClient=initPushClient(); //得到百度推送PushMsgToSingleDeviceRequest request=new PushMsgToSingleDeviceRequest();request.addMsgExpires(new Integer(3600))//消息有效时间request.addDeviceType(devType); // devType => 1: web 2: pc 3:android 4:ios 5:wprequest.addChannelId(""); // 广播IDrequest.addMessageType(pushType);//request.addMessage(content);try {PushMsgToSingleDeviceResponse response=pushClient.pushMsgToSingleDevice(request);System.out.println("推送成功"+response.getMsgId());} catch (PushClientException e) {e.printStackTrace();} catch (PushServerException e) {e.printStackTrace();}return 0;}
实例代码:
//向安卓移动端推送public String sendAndroidMessage(String messageTitle, String messageContent, String[] channelIds)throws PushClientException,PushServerException { //1. 创建PushKeyPair用于app的合法身份认证apikey和secretKey可在应用详情中获取PushKeyPair pair = new PushKeyPair(apiKeyForAndorid, secretKeyForAndorid);// 2. 创建BaiduPushClient,访问SDK接口BaiduPushClient pushClient = new BaiduPushClient(pair,BaiduPushConstants.CHANNEL_REST_URL);// 3. 注册YunLogHandler,获取本次请求的交互信息pushClient.setChannelLogHandler(new YunLogHandler() {@Overridepublic void onHandle(YunLogEvent event) {log.debug(event.getMessage());}});try {// 4. 设置请求参数,创建请求实例//创建Android通知JSONObject notification = new JSONObject();notification.put("title", messageTitle);//标题notification.put("description",messageContent);//内容notification.put("notification_builder_id", 0);//客户端自定义通知样式,如果没有设置默认为0notification.put("notification_basic_style", 4);//只有notification_builder_id为0时才有效,才需要设置,notification.put("open_type", 2);//点击通知后的行为(打开Url:1; 自定义行为:2:其它值则默认打开应用;)notification.put("pkg_content", "#Intent;launchFlags=0x10000000;" + "component=com.gasj.cp/.ui.MessageInfoActivity;" + "S.descrip="+messageContent+";" + "B.ispush=true;S.title="+messageTitle+";end");PushBatchUniMsgRequest request = new PushBatchUniMsgRequest().addChannelIds(channelIds)//频道.addMsgExpires(new Integer(3600))//设置消息的有效时间,单位秒,默认3600*5..addMessageType(1)//通知类型 0 透传,1通知.addMessage(notification.toString()).addDeviceType(3)//客户端类型 3,安卓 4 ios.addTopicId("BaiduPush");// 设置类别主题 // 5. 执行Http请求PushBatchUniMsgResponse response = pushClient.pushBatchUniMsg(request); // 6. Http请求返回值解析log.info(String.format("msgId: %s, sendTime: %d",response.getMsgId(), response.getSendTime()));result="已发送";} catch (PushClientException e) {log.error(e.getMessage());result="发送失败";} catch (PushServerException e) {log.error(String.format("requestId: %d, errorCode: %d, errorMessage: %s",e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));result="发送失败";}return result;}
0 0
- [Push]百度消息推送的应用
- 百度云推送push与deviceone应用
- IOS Push消息推送原理及应用
- IOS Push消息推送原理及应用
- 百度云推送push的使用
- [置顶] 百度云推送push的使用
- OS消息推送的工作机制 PUSH
- Android Push消息推送
- 消息推送技术Push
- Android Push消息推送
- Push---百度云推送Demo
- 百度推送(Push)简单集成
- iphone push notification 消息推送
- iphone push notification 消息推送
- 消息推送(Server Push)
- phonegap push-消息推送实现
- 消息推送(push原理)
- Android开发 消息推送 Push
- 判断各种浏览器的内核和 移动端的类型
- 字符串5:词语变形练习题
- Android 防止你的Activity 不能被销毁
- ACM知识点、比赛总结
- 392. Is Subsequence
- [Push]百度消息推送的应用
- STM32设置时钟
- 单个页面的站点怎么样做seo优化才能将关键字排名提升
- 类方法 对象方法,以及类方法和对象方法的区别
- Android开发环境搭建教程
- 计算机网络知识·三
- 分布式锁
- 字符串6:两串旋转练习题(*)
- 1