Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加入数据库 #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ mpush allocator demo
>
> content-type : text/plain;charset=utf-8

### 其他
### 打包部署
alloc打包方法
进入master目录 运行命令:mvn install(或者将mpush-client-0.8.0.jar手动放入alloc的jar文件夹下)
进入alloc目录,运行命令
mvn clean package -Pzip,pub


56 changes: 35 additions & 21 deletions src/main/java/com/shinemo/mpush/alloc/PushHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import com.mpush.tools.Jsons;
import com.mpush.tools.common.Strings;
import com.sun.net.httpserver.HttpExchange;
import com.mpush.common.druid.MysqlConnecter;
import com.sun.net.httpserver.HttpHandler;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -45,6 +47,7 @@
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final PushSender pushSender = PushSender.create();
private final AtomicInteger idSeq = new AtomicInteger();
String message = null;

public void start() {
pushSender.start();
Expand All @@ -62,7 +65,7 @@ public void handle(HttpExchange httpExchange) throws IOException {

sendPush(params);

byte[] data = "服务已经开始推送,请注意查收消息".getBytes(Constants.UTF_8);
byte[] data = message.getBytes(Constants.UTF_8);
httpExchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8");
httpExchange.sendResponseHeaders(200, data.length);//200, content-length
OutputStream out = httpExchange.getResponseBody();
Expand All @@ -77,27 +80,38 @@ private void sendPush(Map<String, Object> params) {
Boolean broadcast = (Boolean) params.get("broadcast");
String condition = (String) params.get("condition");

//验证用户在数据库中是否存在
MysqlConnecter mc = new MysqlConnecter();
String mobile = mc.selectOne("select mobile from m_user where device_id='"+userId+"'");
System.out.println("-----绑定用户在数据库--------"+mobile);
if (StringUtils.isBlank(mobile)){
message = "绑定用户在数据库中不存在:userId="+userId;
}else {
NotificationDO notificationDO = new NotificationDO();
//notificationDO.content = "MPush开源推送," + hello;
notificationDO.content = hello;
//notificationDO.title = "消息推送";
//notificationDO.nid = idSeq.get() % 2 + 1;
//notificationDO.ticker = "你有一条新的消息,请注意查收";
System.out.println("json内容:"+Jsons.toJson(notificationDO));
PushMsg pushMsg = PushMsg.build(MsgType.NOTIFICATION_AND_MESSAGE, Jsons.toJson(notificationDO));
pushMsg.setMsgId("msg_" + idSeq.incrementAndGet());

pushSender.send(PushContext
.build(pushMsg)
.setUserId(Strings.isBlank(userId) ? null : userId)
.setBroadcast(broadcast != null && broadcast)
.setCondition(Strings.isBlank(condition) ? null : condition)
.setCallback(new PushCallback() {
@Override
public void onResult(PushResult result) {
logger.info(result.toString());
}
})
);
message = "服务已经开始推送,请注意查收消息";
}

NotificationDO notificationDO = new NotificationDO();
notificationDO.content = "MPush开源推送," + hello;
notificationDO.title = "MPUSH推送";
notificationDO.nid = idSeq.get() % 2 + 1;
notificationDO.ticker = "你有一条新的消息,请注意查收";
PushMsg pushMsg = PushMsg.build(MsgType.NOTIFICATION_AND_MESSAGE, Jsons.toJson(notificationDO));
pushMsg.setMsgId("msg_" + idSeq.incrementAndGet());

pushSender.send(PushContext
.build(pushMsg)
.setUserId(Strings.isBlank(userId) ? null : userId)
.setBroadcast(broadcast != null && broadcast)
.setCondition(Strings.isBlank(condition) ? null : condition)
.setCallback(new PushCallback() {
@Override
public void onResult(PushResult result) {
logger.info(result.toString());
}
})
);
}

private byte[] readBody(HttpExchange httpExchange) throws IOException {
Expand Down