说明:
1. 利用华为提供的jar包(smproxy(20040730).jar)进行开发。
2. 根据华为提供的“短消息代理(CMPP20 SMProxy)使用说明书.doc”文档说明。
对于当前通常的开发,即需上行,也需下行。
所以首先必须继承 SMProxy的抽象类,对其进行封装代码如下:
/**
* 说明:这个类关键是继承SMproxy,然后才可以接收短信。
* 触发onDeliver事件。
* 时间:2008-12-16
*/
import com.huawei.insa2.util.Args;
import com.huawei.smproxy.SMProxy;
import com.huawei.insa2.comm.cmpp.message.*;
import com.ibatis.common.logging.Log;
import com.ibatis.common.logging.LogFactory;
public class SMProxyRec extends SMProxy {
private static Log log = LogFactory.getLog(SMProxyRec.class);
private SMProxySendFacadeImpl demo;
public SMProxyRec(SMProxySendFacadeImpl demo,Args args)
{
super(args);
this.demo = null;
this.demo = demo;
}
public CMPPMessage onDeliver(CMPPDeliverMessage msg)
{
log.debug("启动SMProxyRec接收短信");
demo.ProcessRecvDeliverMsg(msg);
return super.onDeliver(msg);
}
public void OnTerminate()
{
demo.Terminate();
}
}
然后,就可应用该类,进行短信的接收和发送的事件重写。
/**
* 说明:网关短信处理主类。
* 接收
* ProcessRecvDeliverMsg(CMPPMessage msg)接收短信
* 发送
* SendMessage(SmsBean sms) 仅处理短信的发送
* 时间:2008-12-16
*
*/
//基本类的方法导入
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List; //华为软件包
import com.huawei.insa2.comm.cmpp.message.*;
import com.huawei.insa2.util.*; //日志
import com.ibatis.common.logging.Log;
import com.ibatis.common.logging.LogFactory; //数据库操作包
import net.trust.IbatisDaoTools.BaseSqlMapDAO;
public class SMProxySendFacadeImpl {
// 从xml中读取配置信息
private static Args argsconn; // 读取连接信息
private static Args argssumbit; // 读取提交信息
// 基本提交信息参数说明
private static int pk_Total = 1;
private static int pk_Number = 1;
private static int registered_Delivery = 1;
private static int msg_Level = 1;
private static String service_Id = "";
private static int fee_UserType = 2;
private static String fee_Terminal_Id = "";
private static int tp_Pid = 0;
private static int tp_Udhi = 0;
private static int msg_Fmt = 15;
private static String msg_Src = "";
private static String fee_Type = "02";
private static String fee_Code = "000";
private static Date valid_Time = null;
private static Date at_Time = null;
private static String src_Terminal_Id = "";
private static String[] dest_Terminal_Id = { "" };
private static byte[] msg_Content = null;
private static String reserve = "";
/** 短信收发接口 */
public static SMProxyRec myProxy = null;
// 数据库操作类
public BaseSqlMapDAO baseSqlMapDAO;
// 日志记录
private static Log log = LogFactory.getLog(SMProxySendFacadeImpl.class);
// 基本参数设置
private void ProBaseConf() {
try {
log.debug("初始化连接信息");
// 连接配置信息
argsconn = new Cfg("Smproxy.xml", false).getArgs("CMPPConnect");
// 初始化短信收发接口
myProxy = new SMProxyRec(this, argsconn);
// 提交参数设置
argssumbit = new Cfg("Smproxy.xml", false)
.getArgs("CMPPSubmitMessage");
pk_Total = argssumbit.get("pk_Total", 1);
pk_Number = argssumbit.get("pk_Number", 1);
registered_Delivery = argssumbit.get("registered_Delivery", 1);
msg_Level = argssumbit.get("msg_Level", 1);
service_Id = argssumbit.get("service_Id", "");
fee_UserType = argssumbit.get("fee_UserType", 2);
fee_Terminal_Id = argssumbit
.get("fee_Terminal_Id", "");
tp_Pid = argssumbit.get("tp_Pid", 1);
tp_Udhi = argssumbit.get("tp_Udhi", 1);
msg_Fmt = argssumbit.get("msg_Fmt", 15);
msg_Src = argssumbit.get("msg_Src", "");
fee_Type = argssumbit.get("fee_Type", "02");
fee_Code = argssumbit.get("fee_Code", "000");
src_Terminal_Id = argssumbit
.get("src_Terminal_Id", "");
reserve = argssumbit.get("reserve", "");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 构造方法
*/
public SMProxySendFacadeImpl() {
// 处理基本的配置信息
ProBaseConf();
}
/**
* 华为短信发送类处理(公用类)
*
* @param 短信类,包括手机号,内容,序号
*/
public int SendMessage(SmsBean sms) {
// 返回结果
int result = 0;
// 发送内容
try {
msg_Content = sms.getSmsContent().getBytes("GBK");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 发送号码
ArrayList telList = sms.getMobileNoList();
dest_Terminal_Id = new String[telList.size()];
for (int i = 0; i < telList.size(); i++) {
dest_Terminal_Id[i] = telList.get(i).toString();
}
// 存活有效期
valid_Time = new Date(System.currentTimeMillis() + (long) 0xa4cb800); // new
// Date();//
// 定时发送时间
at_Time = null;// new Date(System.currentTimeMillis() + (long)
// 0xa4cb800); //new Date();
// 用户手机上显示为短消息的主叫号码
// src_Terminal_Id=src_Terminal_Id+"001";
// 初始化提交信息
CMPPSubmitMessage submitMsg = new CMPPSubmitMessage(pk_Total,
pk_Number, registered_Delivery, msg_Level, service_Id,
fee_UserType, fee_Terminal_Id, tp_Pid, tp_Udhi, msg_Fmt,
msg_Src, fee_Type, fee_Code, valid_Time, at_Time,
src_Terminal_Id, dest_Terminal_Id, msg_Content, reserve);
try {
CMPPSubmitRepMessage submitRepMsg = (CMPPSubmitRepMessage) myProxy
.send(submitMsg);
if (submitRepMsg.getResult() == 0) {
result = 1;
log.debug("发送短信成功msgid:" + submitRepMsg.getMsgId().toString()
+ "/t SequenceId:" + submitRepMsg.getSequenceId());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 对于MyProxy分发短信的接收
* 接收短信后的处理,现在的情况是通过网关方式处理。编码是utf-8
* @param msg
*/
public void ProcessRecvDeliverMsg(CMPPMessage msg) {
CMPPDeliverMessage deliverMsg = (CMPPDeliverMessage) msg;
if (deliverMsg.getRegisteredDeliver() == 0)
try {
// 编码方式
if (deliverMsg.getMsgFmt() == 8) {
log.debug("deliverMsg.getMsgFmt() == 8");
log.debug(String.valueOf(String.valueOf((new StringBuffer(
"接收消息: 主叫号码=")).append(";内容=").append(
new String(deliverMsg.getMsgContent(),
"UTF-16BE")))));
}
// 编码方式GBK
else {
log.debug(String.valueOf(String.valueOf((new StringBuffer(
"接收消息: 主叫号码=")).append(
deliverMsg.getSrcterminalId()).append(";内容=")
.append(new String(deliverMsg.getMsgContent())))));
}
} catch (Exception e) {
e.printStackTrace();
}
else
{
log.debug(String.valueOf(String.valueOf((new StringBuffer(
"收到状态报告消息: stat="))
.append(new String(deliverMsg.getStat())).append(
"dest_termID=").append(
new String(deliverMsg.getDestTerminalId())).append(
";destterm=").append(
new String(deliverMsg.getDestnationId())).append(
";serviceid=").append(
new String(deliverMsg.getServiceId())).append(
";tppid=").append(deliverMsg.getTpPid()).append(
";tpudhi=").append(deliverMsg.getTpUdhi()).append(
";msgfmt").append(deliverMsg.getMsgFmt()).append(
//";内容").append(new String(deliverMsg.getMsgContent())).append(
";srctermid=").append(
new String(deliverMsg.getSrcterminalId())).append(
";deliver=").append(
deliverMsg.getRegisteredDeliver()))));
}
}
/**
* 终端结束程序
*/
public void Terminate() {
log.debug("SMC下发终断消息");
myProxy.close();
myProxy = null;
}
/**
* 关闭连接
*/
public void Close() {
// 查询SMProxy与ISMG的TCP连接状态
String stateDesc = myProxy.getConnState();
log.debug("数据连接状态:" + stateDesc);
// 退出
myProxy.close();
}
// 测试方法
public static void main(String[] args) {
}
}
该类写完毕后,用户就可调用该类中的SendMessage(SmsBean sms)方法进行发送短信。
短信的接收,是自动触发的用户不必别外写事件。只要有连上服务器,就可以了。
附:Smproxy.xml网关配置文件:
注:现在对敏感觉的字段隐藏,用户可用模拟器,设置相关网关,端口,用户名,密码,网关接入号等信息。这样就不受环境约束。
下载地址:
短信源码:http://download.csdn.net/source/878588
cmpp2.0模拟器:http://download.csdn.net/source/878567