Files
IM-server-new/broker/src/main/java/cn/wildfirechat/push/PushServer.java
T

110 lines
4.4 KiB
Java
Raw Normal View History

2019-03-16 17:04:23 +08:00
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package cn.wildfirechat.push;
2020-12-12 16:15:13 +08:00
import cn.wildfirechat.common.IMExceptionEvent;
2019-03-16 17:04:23 +08:00
import cn.wildfirechat.proto.ProtoConstants;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
2019-03-16 17:04:23 +08:00
import io.moquette.persistence.MemorySessionStore;
import io.moquette.server.config.IConfig;
import io.moquette.spi.ISessionsStore;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import win.liyufan.im.HttpUtils;
import win.liyufan.im.Utility;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static io.moquette.BrokerConstants.PUSH_ANDROID_SERVER_ADDRESS;
import static io.moquette.BrokerConstants.PUSH_IOS_SERVER_ADDRESS;
2021-11-19 20:21:41 +08:00
import static win.liyufan.im.HttpUtils.HttpPostType.POST_TYPE_Push;
2019-03-16 17:04:23 +08:00
public class PushServer {
private static final Logger LOG = LoggerFactory.getLogger(PushServer.class);
protected static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
2019-03-16 17:04:23 +08:00
public interface PushMessageType {
int PUSH_MESSAGE_TYPE_NORMAL = 0;
int PUSH_MESSAGE_TYPE_VOIP_INVITE = 1;
int PUSH_MESSAGE_TYPE_VOIP_BYE = 2;
2020-03-26 13:31:35 +08:00
int PUSH_MESSAGE_TYPE_FRIEND_REQUEST = 3;
2020-10-22 10:32:51 +08:00
int PUSH_MESSAGE_TYPE_VOIP_ANSWER = 4;
2021-09-20 08:21:16 +08:00
int PUSH_MESSAGE_TYPE_RECALLED = 5;
int PUSH_MESSAGE_TYPE_DELETED = 6;
2019-03-16 17:04:23 +08:00
}
private static PushServer INSTANCE = new PushServer();
private ISessionsStore sessionsStore;
private static ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*5);
private String androidPushServerUrl;
private String iOSPushServerUrl;
private PushServer() {
}
public static PushServer getServer() {
return INSTANCE;
}
public void init(IConfig config, ISessionsStore sessionsStore) {
this.sessionsStore = sessionsStore;
this.androidPushServerUrl = config.getProperty(PUSH_ANDROID_SERVER_ADDRESS);
this.iOSPushServerUrl = config.getProperty(PUSH_IOS_SERVER_ADDRESS);
}
public void pushMessage(PushMessage pushMessage, String deviceId, String pushContent) {
2020-02-21 11:19:52 +08:00
LOG.info("try to delivery push diviceId = {}, conversationType = {}, pushContent = {}", deviceId, pushMessage.convType, pushContent);
2019-03-16 17:04:23 +08:00
executorService.execute(() ->{
try {
pushMessageInternel(pushMessage, deviceId, pushContent);
} catch (Exception e) {
e.printStackTrace();
2020-12-12 16:15:13 +08:00
Utility.printExecption(LOG, e, IMExceptionEvent.EventType.PUSH_SERVER_Exception);
2019-03-16 17:04:23 +08:00
}
});
}
private void pushMessageInternel(PushMessage pushMessage, String deviceId, String pushContent) {
if (pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_NORMAL && StringUtil.isNullOrEmpty(pushContent)) {
LOG.info("push content empty, deviceId {}", deviceId);
return;
}
MemorySessionStore.Session session = sessionsStore.getSession(deviceId);
int badge = session.getUnReceivedMsgs();
if (badge <= 0) {
badge = 1;
}
if (StringUtil.isNullOrEmpty(session.getDeviceToken())) {
LOG.warn("Device token is empty for device {}", deviceId);
return;
}
pushMessage.packageName = session.getAppName();
pushMessage.pushType = session.getPushType();
pushMessage.pushContent = pushContent;
pushMessage.deviceToken = session.getDeviceToken();
pushMessage.unReceivedMsg = badge;
2020-11-06 16:44:21 +08:00
pushMessage.userId = session.getUsername();
2022-01-06 09:56:46 +08:00
if (session.getPlatform() == ProtoConstants.Platform.Platform_iOS || session.getPlatform() == ProtoConstants.Platform.Platform_Android) {
2019-03-16 17:04:23 +08:00
String url = androidPushServerUrl;
2022-01-06 09:56:46 +08:00
if (session.getPlatform() == ProtoConstants.Platform.Platform_iOS) {
2019-03-16 17:04:23 +08:00
url = iOSPushServerUrl;
pushMessage.voipDeviceToken = session.getVoipDeviceToken();
}
HttpUtils.httpJsonPost(url, gson.toJson(pushMessage, pushMessage.getClass()), POST_TYPE_Push);
2019-03-16 17:04:23 +08:00
} else {
LOG.info("Not mobile platform {}", session.getPlatform());
}
}
}