首页 » IT技术 » 网络服务 » 正文

Java WebServices 接口实现百度云推送,向C#提供推送服务

听起来是多么的狗血啊,其实事情是这样的,公司开发的商有信产品APP要使用百度云推送实现消息推送功能,可是百度云推送没有提供.net的服务器端SDK,只提供了java和php的,而公司都是.net程序员,无奈,为了开发速度,只能由我写个WebServices接口让他们调用,简介将消息给百度,再由百度给APP。

这个java版的WebServices提供了多种消息推送方式,日志记录,另外这个接口可以向多个不同APP推送信息,是个通用版的接口,使用者只需提供设备类型,要推送的消息,APP程序的包名就能推送信息了,当然前提是在百度那边配置好,拿到SecretKey 和APIKey写在Java的配置文件中。

下面是一些代码。

源代码   
package com.gogowan.baidupush;
import net.sf.json.JSONObject;
import com.baidu.yun.push.client.BaiduPushClient;
import com.baidu.yun.push.constants.BaiduPushConstants;
import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;
import com.baidu.yun.push.model.PushBatchUniMsgRequest;
import com.baidu.yun.push.model.PushBatchUniMsgResponse;
import com.baidu.yun.push.model.PushMsgToAllRequest;
import com.baidu.yun.push.model.PushMsgToAllResponse;
import com.baidu.yun.push.model.PushMsgToSingleDeviceRequest;
import com.baidu.yun.push.model.PushMsgToSingleDeviceResponse;
import com.baidu.yun.push.model.PushMsgToTagRequest;
import com.baidu.yun.push.model.PushMsgToTagResponse;
import com.gogowan.util.Log;
public class PushMsgImpl {
private BaiduPushClient pushClient = null;
private int errorcode = -1;
private String errormsg = "服务器端异常";
public PushMsgImpl(String packagename) {
pushClient = new PushClientFactory(packagename).getPushClient();
}
/**
* 向所有用户发送一个推送
*
* @param msgtype
* 设置消息类型,0表示透传消息,1表示通知
* @param msg
* 设置消息内容
* @param devicetype
* 设置设备类型 deviceType => 1 for web,2 for pc, 3 for android, 4 for
* ios, 5 for wp.
* */
public String send(int msgtype, String msg, int devicetype) {
Log.addLog(msg);
JSONObject object = null;
try {
PushMsgToAllRequest request = new PushMsgToAllRequest()
.addMsgExpires(new Integer(3600)).addMessageType(msgtype)
.addMessage(msg).addDeviceType(devicetype);
PushMsgToAllResponse response = pushClient.pushMsgToAll(request);
String msgId = response.getMsgId();
long sendTime = response.getSendTime();
System.out.println("msgId: " + msgId + ",sendTime: " + sendTime);
object = new JSONObject();
object.put("msgId", msgId);
object.put("sendTime", sendTime);
} catch (PushClientException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
} else {
new Log().printException(e);
}
} catch (PushServerException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
} else {
System.out.println(String.format(
"requestId: %d, errorCode: %d, errorMsg: %s", e
.getRequestId(), e.getErrorCode(), e
.getErrorMsg()));
new Log().printException(e);
}
}
return object == null ? serverError(errorcode, errormsg) : object
.toString();
}
/**
* 向指定的CannelID发送一个推送信息
*
* @param msgtype
* 设置消息类型,0表示透传消息,1表示通知
* @param msg
* 设置消息内容
* @param devicetype
* 设置设备类型 deviceType => 1 for web,2 for pc, 3 for android, 4 for
* ios, 5 for wp.
* @param ChannelId
* 指定用户的ChannelId
* */
public String send(int msgtype, String msg, int devicetype, String ChannelId) {
Log.addLog(msg);
String str = null;
PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest()
.addChannelId(ChannelId).addMsgExpires(new Integer(3600))
.addMessageType(msgtype).addMessage(msg).addDeviceType(
devicetype);
try {
str = sendRequst(request);
} catch (PushClientException e) {
// TODO Auto-generated catch block
new Log().printException(e);
} catch (PushServerException e) {
// TODO Auto-generated catch block
new Log().printException(e);
}
return str;
}
/**
* 向指定一组用户发送一个推送信息
*
* @param msgtype
* 设置消息类型,0表示透传消息,1表示通知
* @param msg
* 设置消息内容
* @param devicetype
* 设置设备类型 deviceType => 1 for web,2 for pc, 3 for android, 4 for
* ios, 5 for wp.
* @param ChannelId
* 指定一组用户的ChannelId
* @param classtheme
* 设置主题的名字
*
* @throws PushClientException
* @throws PushServerException
* */
public String send(int msgtype, String msg, int devicetype,
String ChannelIds[], String classtheme) {
JSONObject object = null;
for (int i = 0; i < ChannelIds.length; i++) {
System.out.println(ChannelIds[i]);
System.out.println("'");
}
Log.addLog("msgtype:"+msgtype+"######"+"msg:"+msg+"######"+"ChannelIds:"+ChannelIds.length+"######"+"classtheme:"+classtheme+"######"+"devicetype:"+devicetype);
try {
PushBatchUniMsgRequest request = new PushBatchUniMsgRequest()
.addChannelIds(ChannelIds).addMsgExpires(new Integer(3600))
.addMessageType(msgtype).addMessage(msg).addDeviceType(
devicetype).addTopicId(classtheme);
PushBatchUniMsgResponse response = pushClient
.pushBatchUniMsg(request);
String msgId = response.getMsgId();
long sendTime = response.getSendTime();
System.out.println("msgId: " + msgId + ",sendTime: " + sendTime);
object = new JSONObject();
object.put("msgId", msgId);
object.put("sendTime", sendTime);
} catch (PushClientException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
new Log().printException(e);
} else {
// e.printStackTrace();
e.printStackTrace();
new Log().printException(e);
}
} catch (PushServerException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
new Log().printException(e);
} else {
System.out.println(String.format(
"requestId: %d, errorCode: %d, errorMsg: %s", e
.getRequestId(), e.getErrorCode(), e
.getErrorMsg()));
new Log().printException(e);
}
}
return object == null ? serverError(errorcode, errormsg) : object
.toString();
}
/**
* 向指定tag组用户发送一个推送信息
*
* @param msgtype
* 设置消息类型,0表示透传消息,1表示通知
* @param msg
* 设置消息内容
* @param devicetype
* 设置设备类型 deviceType => 1 for web,2 for pc, 3 for android, 4 for
* ios, 5 for wp.
* @param tagname
* 设置tag组的名称
* */
public String send2Tag(int msgtype, String msg, int devicetype,
String tagname) {
Log.addLog(msg);
JSONObject object = null;
try {
PushMsgToTagRequest request = new PushMsgToTagRequest().addTagName(
tagname).addMsgExpires(new Integer(3600)).addMessageType(
msgtype).addMessage(msg).addDeviceType(devicetype);
PushMsgToTagResponse response = pushClient.pushMsgToTag(request);
String msgId = response.getMsgId();
long sendTime = response.getSendTime();
System.out.println("msgId: " + msgId + ",sendTime: " + sendTime);
object = new JSONObject();
object.put("msgId", msgId);
object.put("sendTime", sendTime);
} catch (PushClientException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
} else {
new Log().printException(e);
}
} catch (PushServerException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
} else {
System.out.println(String.format(
"requestId: %d, errorCode: %d, errorMsg: %s", e
.getRequestId(), e.getErrorCode(), e
.getErrorMsg()));
new Log().printException(e);
}
}
return object == null ? serverError(errorcode, errormsg) : object
.toString();
}
private String sendRequst(PushMsgToSingleDeviceRequest request)
throws PushClientException, PushServerException {
JSONObject object = null;
try {
PushMsgToSingleDeviceResponse response = pushClient
.pushMsgToSingleDevice(request);
String msgId = response.getMsgId();
long sendTime = response.getSendTime();
System.out.println("msgId: " + msgId + ",sendTime: " + sendTime);
object = new JSONObject();
object.put("msgId", msgId);
object.put("sendTime", sendTime);
} catch (PushClientException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
throw e;
} else {
new Log().printException(e);
}
} catch (PushServerException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
throw e;
} else {
System.out.println(String.format(
"requestId: %d, errorCode: %d, errorMsg: %s", e
.getRequestId(), e.getErrorCode(), e
.getErrorMsg()));
new Log().printException(e);
}
}
return object == null ? serverError(errorcode, errormsg) : object
.toString();
}
private String serverError(int errorcode, String errormsg) {
JSONObject jo = new JSONObject();
jo.put("errorcode", errorcode);
jo.put("errormsg", errormsg);
Log.addLog(jo.toString());
return jo.toString();
}
}

发表评论