消息推送,顾名思义,是由一方主动发起,而另一方与发起方以某一种方式建立连接并接收消息。在Android开发中,这里的发起方我们把它叫做推送服务器(Push Server),接收方叫做客户端(Client)。相比通过轮询来获取新消息或通知,推送无论是在对客户端的资源消耗还是设备耗电量来说都比轮询要好。
Java调用百度推送服务器端源代码,具体如下:package org.pro.push.util;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
/**
* explain: BaiDu Push Class
* translate: 百度推送调用类
* @author Muci 伤寒
* Copyright (C), ORG伤寒
* qq:1877378299
* mail:admin@baozoubook.com
* tel:18616220047
*/
public class BaiduPush {
/**百度推送的APIKEY*/
public static final String APIKEY = "xxxxxxxxxxxxxxxxxxxxxxxx";
/**百度推送的密钥*/
public static final String SECRETKEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
/**
* explain: INIT BaiDu channel client
* translate: 初始化百度推送引用渠道
* @param yunLogHandler 日志处理对象
* @return 百度推送引用渠道
*/
private BaiduChannelClient initChannelClient(YunLogHandler yunLogHandler){
// 获取密钥链接通道
ChannelKeyPair channelKeyPair = new ChannelKeyPair(BaiduPush.APIKEY, BaiduPush.SECRETKEY);
// 获取百度链接通道
BaiduChannelClient baiduChannelClient = new BaiduChannelClient(
channelKeyPair);
// 设置消息日志
baiduChannelClient.setChannelLogHandler(yunLogHandler);
return baiduChannelClient;
}
/**
* explain: Group of news push
* translate: 群发推送方法
* @param yunLogHandler 日志处理对象
* @param deviceType 装配类型 1: WEB 2: PC 3:ANDROID 4:IOS 5:WINDOWS_PHONE
* @param messageType 消息类型 0:设置发送消息,1:设置发送通知
* @param message 消息内容 JSON格式
* @return PushBroadcastMessageResponse 推送广播消息响应对象
* @throws ChannelClientException 客户服务渠道异常
* @throws ChannelServerException 频道服务器渠道异常
*/
public PushBroadcastMessageResponse groupPush(YunLogHandler yunLogHandler,int deviceType,int messageType,String message) throws ChannelClientException, ChannelServerException{
//初始化百度推送引用渠道
BaiduChannelClient baiduChannelClient = this.initChannelClient(yunLogHandler);
//获取群推送消息请求对象
PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
//装载推送类型参数
request.setDeviceType(deviceType);
//装载消息类型参数
request.setMessageType(messageType);
//装载消息参数
request.setMessage(message);
//调用百度推送渠道对象发送消息,接收推送广播消息响应对象
PushBroadcastMessageResponse response = baiduChannelClient.pushBroadcastMessage(request);
//返回推送广播响应对象
return response;
}
/**
* explain: single message push
* translate: 单一消息推送
* @param yunLogHandler 日志处理对象
* @param deviceType 装配类型 1: WEB 2: PC 3:ANDROID 4:IOS 5:WINDOWS_PHONE
* @param messageType 消息类型 0:设置发送消息,1:设置发送通知
* @param channelId 通道ID 由客户端提供
* @param userId 用户ID 由客户端提供
* @param message 需推送的消息
* @return 推单播消息响应对象
* @throws ChannelServerException 频道服务器渠道异常
* @throws ChannelClientException 客户服务渠道异常
*/
public PushUnicastMessageResponse singlePush(YunLogHandler yunLogHandler,int deviceType,int messageType,String channelId, String userId, String message) throws ChannelClientException, ChannelServerException{
//初始化百度推送引用渠道
BaiduChannelClient baiduChannelClient = this.initChannelClient(yunLogHandler);
//获取推单播消息请求对象
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
//装载推送类型参数
request.setDeviceType(deviceType);
//装载通道ID参数
request.setChannelId(Long.valueOf(channelId));
//装载用户ID参数
request.setUserId(userId);
//装载消息类型参数
request.setMessageType(messageType);
//装载消息参数
request.setMessage(message);
//调用百度推送渠道对象发送消息,接收推送单播消息响应对象
PushUnicastMessageResponse response = baiduChannelClient.pushUnicastMessage(request);
//返回推送单播消息响应对象
return response;
}
/**
* explain: get YunLogHandler
* translate: 获取日志处理对象
* @return 日志处理对象
*/
public YunLogHandler getYunLogHandler(){
return new YunLogHandler() {
@Override
public void onHandle(YunLogEvent event) {
System.out.println("time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\n" + event.getMessage());
}
};
}
/**
* 测试调用单一消息推送的方法
*/
/*public static void main(String[] args) {
int singlePushType = 0;
BaiduPush baiduPush = new BaiduPush();
try {
PushUnicastMessageResponse pushUnicastMessageResponse = baiduPush.singlePush(baiduPush.getYunLogHandler(), 3, 1, "4293667056080933767", "621534449248051524", "{\"title\":\"这是一个群发测试\",\"description\":\"测试哦测试啊测试恩\"}");
singlePushType = pushUnicastMessageResponse.getSuccessAmount();
} catch (ChannelClientException e) {
//e.printStackTrace();
singlePushType=2;
} catch (ChannelServerException e) {
//e.printStackTrace();
System.out.println(String.format("request_id: %d, error_code: %d, error_message: %s", e.getRequestId(),e.getErrorCode(),e.getErrorMsg()));
singlePushType=3;
}
System.out.println(singlePushType);
}*/
/**
* 测试调用群发消息推送的方法
*/
/*public static void main(String[] args) {
int singlePushType = 0;
BaiduPush baiduPush = new BaiduPush();
try {
PushBroadcastMessageResponse pushBroadcastMessageResponse = baiduPush.groupPush(baiduPush.getYunLogHandler(), 3, 1, "{\"title\":\"这是一个群发测试\",\"description\":\"测试哦测试啊测试恩\"}");
singlePushType = pushBroadcastMessageResponse.getSuccessAmount();
} catch (ChannelClientException e) {
e.printStackTrace();
singlePushType=2;
} catch (ChannelServerException e) {
e.printStackTrace();
System.out.println(String.format("request_id: %d, error_code: %d, error_message: %s", e.getRequestId(),e.getErrorCode(),e.getErrorMsg()));
singlePushType=3;
}
System.out.println(singlePushType);
}*/
}