最近做一个小项目把消息主动推送到手机客户端,于是研究了一下百度云推送的api,因为我主要是做服务端这块,所以重点就看了REST API这块的文档:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/api。
协议相对来说比较好理解,接下来就是按协议来写代码了,因为这个协议是基于http协议,所以首先想到的是用apache http client包来实现协议的请求,在我脑子里第一时间蹦出来的代码大概是这样的:
HttpClient httpClient = new DefaultHttpClient();
//根据实际情况设置httpClient 的各种参数
....
//创建HttpPost实例,设置api的链接
HttpPost httpPost = new HttpPost("http://channel.api.duapp.com/rest/2.0/channel/channel");
//根据协议规定的格式拼装参数值
String apiKey = ...
String secretKey = ...
String method = "push_msg";
String sign = .....
....
//构造post请求的参数
List <NameValuePair> nvps = new ArrayList <NameValuePair>(length);
nvps.add(new BasicNameValuePair("method“, method);
nvps.add(new BasicNameValuePair("apiKey“, apiKey);
....
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
//发送post请求
HttpResponse response = httpClient.execute(httpPost);
//获取返回的数据解析处理等
......
嗯,伪代码大概就是这样了,然后我下载了百度提供的服务端sdk:http://bs.baidu.com/push-sdk-release/Baidu-Push-SDK-Java-1.1.2.zip,
把文件解压后,发现就是一个eclipse的工程,导入到eclipse后就可以看到完整的代码了,sdk除了提供sample例子代码外,还提供了完整的协议封装及发送请求的代码(没错,百度没有把这些封装成jar,只提供jar包,还是不错的),参看sample中发送单播消息的代码,如下:
package com.baidu.yun.channel.sample;
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.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
public class AndroidPushMessageSample {
public static void main(String[] args) {
/*
* @brief 推送单播消息(消息类型为透传,由开发方应用自己来解析消息内容) message_type = 0 (默认为0)
*/
// 1. 设置developer平台的ApiKey/SecretKey
String apiKey = "yBmx69aL5tAhiq7GlGFOsvV9";
String secretKey = "6XtzFIMm17l61MrCAIaWZXc6db8PjfqD";
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());
}
});
try {
// 4. 创建请求类对象
// 手机端的ChannelId, 手机端的UserId, 先用1111111111111代替,用户需替换为自己的
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
// 4:ios 5:wp
request.setChannelId(3544710896330446635L);
request.setUserId("908429798119406941");
request.setMessage("Hello Channel");
// 5. 调用pushMessage接口
PushUnicastMessageResponse response = channelClient
.pushUnicastMessage(request);
// 6. 认证推送成功
System.out.println("push amount : " + response.getSuccessAmount());
} catch (ChannelClientException e) {
// 处理客户端错误异常
e.printStackTrace();
} catch (ChannelServerException e) {
// 处理服务端错误异常
System.out.println(String.format(
"request_id: %d, error_code: %d, error_message: %s",
e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
}
}
}
跟我写的代码的结构有很大不同有木有?思维不一样有木没有,呵呵,没错了,这就是面向对象的思维了,主要逻揖都封装在这三个类里面了:
BaiduChannelClient
PushUnicastMessageRequest
PushUnicastMessageResponse
其中BaiduChannelClient负责发送请求,PushUnicastMessageRequest负责请求协议的数据的封装,PushUnicastMessageResponse负责返回数据的协议封装。
其它不都说了,有兴趣的同学可以仔细看一下百度sdk提供的代码,大概就是这样,总体感觉还是面向面向过程的思维比较直接,但代码不容易维护是事实,面向对象的思维比较抽象,但代码确实是整洁,容易维护,也容易做单元测试。
本文深入探讨了如何利用百度云推送API进行消息主动推送,并通过对比伪代码与官方SDK实现,阐述了面向过程与面向对象编程思维在代码结构上的差异。详细介绍了SDK中的关键类与方法,如BaiduChannelClient、PushUnicastMessageRequest和PushUnicastMessageResponse,展示了如何高效地发送单播消息。


被折叠的 条评论
为什么被折叠?



