DDPush推送学习笔记(一)

2018-02-27 11:50:41来源:oschina作者:yuewawa人点击

分享

一、下载DDPush的jar包并导入到Java项目


二、编写推送代码


Pusher pusher = null;
try {
//三个参数分别对应:DDPush服务器IP,DDPush默认推送端口,响应超时
pusher = new Pusher("192.168.X.X", 9999, 5000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = "";
Scanner scanner = null;

while (!str.equals("bye")) {
System.out.println("请输入要发送的消息:");
scanner = new Scanner(System.in);
str = scanner.next();
System.out.println("你输入的是:"+str);
boolean flag = false;
try {
//user1表示要推到的用户,转换为DDPush的uuid
flag = pusher.push0x20Message(StringUtil.md5Byte("user1"), str.getBytes("UTF-8"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag = false;
}
System.out.println(flag);
}

三、编写接收推送的代码


1)编写自定义的TCPClient


public class MyTCPClient extends TCPClientBase{

public MyTCPClient(byte[] uuid) throws Exception {
//uuid, appid, DDPush服务器IP,DDPush默认接收消息的端口,响应超时
super(uuid, 1, "192.168.X.X", 9966, 5);
}
@Override
public boolean hasNetworkConnection() {
// TODO Auto-generated method stub
return true;
}
@Override
public void onPushMessage(Message msg) {
// TODO Auto-generated method stub
if (msg == null) {
System.out.println("msg is null");
return;
}

if (msg.getData() == null || msg.getData().length == 0) {
System.out.println("msg has no data");
return;
}
String str = null;
try {
str = new String(msg.getData(),5,msg.getContentLength(), "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
str = "Exception";
}
System.out.println(str);
}
@Override
public void trySystemSleep() {
// TODO Auto-generated method stub

}
}

2)调用MyTCPClient


MyTCPClient mClient = null;

mClient = new MyTCPClient(StringUtil.md5Byte("user2"));//user2接收消息的用户
mClient.setHeartbeatInterval(60);

mClient.start();

四、启动DDPush服务端


通过dos命令启动DDPush服务


> start.bat

DDPush服务端文件


五、启动客户端程序就可以发送消息了

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台