1、优化服务端session管理实现

2、android优化sdk通知管理
This commit is contained in:
远方夕阳 2022-09-04 22:47:38 +08:00
parent ea9efc8594
commit 538f80cee1
11 changed files with 70 additions and 49 deletions

View File

@ -24,6 +24,7 @@ package com.farsunset.cim.component.handler;
import com.farsunset.cim.component.handler.annotation.CIMHandler; import com.farsunset.cim.component.handler.annotation.CIMHandler;
import com.farsunset.cim.component.redis.SignalRedisTemplate; import com.farsunset.cim.component.redis.SignalRedisTemplate;
import com.farsunset.cim.constant.ChannelAttr; import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.constants.Constants;
import com.farsunset.cim.entity.Session; import com.farsunset.cim.entity.Session;
import com.farsunset.cim.group.SessionGroup; import com.farsunset.cim.group.SessionGroup;
import com.farsunset.cim.handler.CIMRequestHandler; import com.farsunset.cim.handler.CIMRequestHandler;
@ -53,6 +54,10 @@ public class BindHandler implements CIMRequestHandler {
@Override @Override
public void process(Channel channel, SentBody body) { public void process(Channel channel, SentBody body) {
if (sessionGroup.isManaged(channel)){
return;
}
ReplyBody reply = new ReplyBody(); ReplyBody reply = new ReplyBody();
reply.setKey(body.getKey()); reply.setKey(body.getKey());
reply.setCode(HttpStatus.OK.value()); reply.setCode(HttpStatus.OK.value());
@ -79,6 +84,8 @@ public class BindHandler implements CIMRequestHandler {
*/ */
sessionService.add(session); sessionService.add(session);
channel.attr(Constants.SESSION_ID).set(session.getId());
/* /*
* 添加到内存管理 * 添加到内存管理
*/ */

View File

@ -23,6 +23,7 @@ package com.farsunset.cim.component.handler;
import com.farsunset.cim.component.handler.annotation.CIMHandler; import com.farsunset.cim.component.handler.annotation.CIMHandler;
import com.farsunset.cim.constant.ChannelAttr; import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.constants.Constants;
import com.farsunset.cim.entity.Session; import com.farsunset.cim.entity.Session;
import com.farsunset.cim.group.SessionGroup; import com.farsunset.cim.group.SessionGroup;
import com.farsunset.cim.handler.CIMRequestHandler; import com.farsunset.cim.handler.CIMRequestHandler;
@ -42,31 +43,25 @@ public class ClosedHandler implements CIMRequestHandler {
@Resource @Resource
private SessionService sessionService; private SessionService sessionService;
@Resource
private SessionGroup sessionGroup;
@Override @Override
public void process(Channel channel, SentBody message) { public void process(Channel channel, SentBody message) {
String uid = channel.attr(ChannelAttr.UID).get(); Long sessionId = channel.attr(Constants.SESSION_ID).get();
if (uid == null){ if (sessionId == null){
return; return;
} }
String nid = channel.attr(ChannelAttr.ID).get();
sessionGroup.remove(channel);
/* /*
* ios开启了apns也需要显示在线因此不删记录 * ios开启了apns也需要显示在线因此不删记录
*/ */
if (!Objects.equals(channel.attr(ChannelAttr.CHANNEL).get(), Session.CHANNEL_IOS)){ if (Objects.equals(channel.attr(ChannelAttr.CHANNEL).get(), Session.CHANNEL_IOS)){
sessionService.delete(uid,nid); sessionService.updateState(sessionId, Session.STATE_INACTIVE);
return; return;
} }
sessionService.updateState(uid,nid, Session.STATE_INACTIVE); sessionService.delete(sessionId);
} }
} }

View File

@ -21,13 +21,16 @@
*/ */
package com.farsunset.cim.constants; package com.farsunset.cim.constants;
import io.netty.util.AttributeKey;
public interface Constants { public interface Constants {
String PUSH_MESSAGE_INNER_QUEUE = "signal/channel/PUSH_MESSAGE_INNER_QUEUE"; String PUSH_MESSAGE_INNER_QUEUE = "signal/channel/PUSH_MESSAGE_INNER_QUEUE";
String BIND_MESSAGE_INNER_QUEUE = "signal/channel/BIND_MESSAGE_INNER_QUEUE"; String BIND_MESSAGE_INNER_QUEUE = "signal/channel/BIND_MESSAGE_INNER_QUEUE";
String PING_MESSAGE_INNER_QUEUE = "signal/channel/PING_MESSAGE_INNER_QUEUE";
String APNS_DEVICE_TOKEN = "APNS_OPEN_%s"; String APNS_DEVICE_TOKEN = "APNS_OPEN_%s";
AttributeKey<Long> SESSION_ID = AttributeKey.valueOf("session_id");
} }

View File

@ -33,17 +33,13 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public interface SessionRepository extends JpaRepository<Session, Long> { public interface SessionRepository extends JpaRepository<Session, Long> {
@Modifying
@Query("delete from Session where uid = ?1 and nid = ?2")
void delete(String uid,String nid);
@Modifying @Modifying
@Query("delete from Session where host = ?1 ") @Query("delete from Session where host = ?1 ")
void deleteAll(String host); void deleteAll(String host);
@Modifying @Modifying
@Query("update Session set state = ?3 where uid = ?1 and nid = ?2") @Query("update Session set state = :state where id = :id")
void updateState(String uid,String nid,int state); void updateState(long id,int state);
@Modifying @Modifying
@Query("update Session set state = " + Session.STATE_APNS + " where uid = ?1 and channel = ?2") @Query("update Session set state = " + Session.STATE_APNS + " where uid = ?1 and channel = ?2")

View File

@ -33,14 +33,14 @@ public interface SessionService {
void add(Session session); void add(Session session);
void delete(String uid,String nid); void delete(long id);
/** /**
* 删除本机的连接记录 * 删除本机的连接记录
*/ */
void deleteLocalhost(); void deleteLocalhost();
void updateState(String uid,String nid,int state); void updateState(long id,int state);
void openApns(String uid,String deviceToken); void openApns(String uid,String deviceToken);

View File

@ -31,6 +31,7 @@ import javax.annotation.Resource;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class SessionServiceImpl implements SessionService { public class SessionServiceImpl implements SessionService {
@ -56,18 +57,19 @@ public class SessionServiceImpl implements SessionService {
} }
@Override @Override
public void delete(String uid, String nid) { public void delete(long id) {
sessionRepository.delete(uid,nid); sessionRepository.deleteById(id);
} }
@Override @Override
public void deleteLocalhost() { public void deleteLocalhost() {
sessionRepository.deleteAll(host); sessionRepository.deleteAll(host);
} }
@Override @Override
public void updateState(String uid, String nid, int state) { public void updateState(long id, int state) {
sessionRepository.updateState(uid,nid,state); sessionRepository.updateState(id,state);
} }
@Override @Override
@ -84,6 +86,9 @@ public class SessionServiceImpl implements SessionService {
@Override @Override
public List<Session> findAll() { public List<Session> findAll() {
return sessionRepository.findAll(); return sessionRepository.findAll()
.stream()
.filter(session -> session.getState() == Session.STATE_ACTIVE || session.getState() == Session.STATE_APNS)
.collect(Collectors.toList());
} }
} }

View File

@ -6,7 +6,7 @@
<groupId>com.farsunset</groupId> <groupId>com.farsunset</groupId>
<artifactId>cim-android-sdk</artifactId> <artifactId>cim-android-sdk</artifactId>
<version>4.2.5</version> <version>4.2.6</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name> <name>${project.groupId}:${project.artifactId}</name>

View File

@ -43,6 +43,10 @@ class CIMCacheManager {
public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE"; public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE";
public static final String KEY_NTC_CHANNEL_NAME = "KEY_NTC_CHANNEL_NAME";
public static final String CONTENT_URI = "content://%s.cim.provider"; public static final String CONTENT_URI = "content://%s.cim.provider";

View File

@ -119,15 +119,9 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
} }
private void startPushService() { private void startPushService() {
Intent intent = new Intent(context, CIMPushService.class);
try { intent.setAction(ServiceAction.ACTION_ACTIVATE_PUSH_SERVICE);
Intent intent = new Intent(context, CIMPushService.class); CIMPushManager.startService(context,intent);
intent.setAction(ServiceAction.ACTION_ACTIVATE_PUSH_SERVICE);
CIMPushManager.startService(context,intent);
}catch (Exception ignore){
context.sendBroadcast(new Intent(IntentAction.ACTION_CONNECTION_RECOVERY));
}
} }
private void onInnerConnectionClosed() { private void onInnerConnectionClosed() {

View File

@ -31,6 +31,7 @@ import android.os.Build;
import android.os.LocaleList; import android.os.LocaleList;
import android.text.TextUtils; import android.text.TextUtils;
import com.farsunset.cim.sdk.android.constant.BundleKey; import com.farsunset.cim.sdk.android.constant.BundleKey;
import com.farsunset.cim.sdk.android.constant.IntentAction;
import com.farsunset.cim.sdk.android.constant.RequestKey; import com.farsunset.cim.sdk.android.constant.RequestKey;
import com.farsunset.cim.sdk.android.constant.ServiceAction; import com.farsunset.cim.sdk.android.constant.ServiceAction;
import com.farsunset.cim.sdk.android.logger.CIMLogger; import com.farsunset.cim.sdk.android.logger.CIMLogger;
@ -314,10 +315,15 @@ public class CIMPushManager {
} }
protected static void startService(Context context, Intent intent) { protected static void startService(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent); context.startService(intent);
return;
}
try {
context.startForegroundService(intent);
}catch (Exception ignore){
context.sendBroadcast(new Intent(IntentAction.ACTION_CONNECTION_RECOVERY));
} }
} }

View File

@ -53,8 +53,6 @@ public class CIMPushService extends Service {
private static final int NOTIFICATION_ID = Integer.MAX_VALUE; private static final int NOTIFICATION_ID = Integer.MAX_VALUE;
private static final int PERSIST_NOTIFICATION_ID = Integer.MIN_VALUE;
private CIMConnectorManager connectorManager; private CIMConnectorManager connectorManager;
private KeepAliveBroadcastReceiver keepAliveReceiver; private KeepAliveBroadcastReceiver keepAliveReceiver;
private ConnectivityManager connectivityManager; private ConnectivityManager connectivityManager;
@ -64,7 +62,9 @@ public class CIMPushService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
connectorManager = CIMConnectorManager.getManager(this.getApplicationContext()); connectorManager = CIMConnectorManager.getManager(this.getApplicationContext());
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -118,7 +118,7 @@ public class CIMPushService extends Service {
String action = newIntent.getAction(); String action = newIntent.getAction();
if (!persistHolder.get()) { if (!persistHolder.get()) {
createNotification(); createTransientNotification();
} }
if (ServiceAction.ACTION_CREATE_CIM_CONNECTION.equals(action)) { if (ServiceAction.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
@ -167,7 +167,7 @@ public class CIMPushService extends Service {
notificationHandler.sendEmptyMessageDelayed(0, 200); notificationHandler.sendEmptyMessageDelayed(0, 200);
} }
return Service.START_REDELIVER_INTENT; return super.onStartCommand(intent,flags,startId);
} }
private void prepareConnect(long delayMillis) { private void prepareConnect(long delayMillis) {
@ -241,12 +241,21 @@ public class CIMPushService extends Service {
} }
} }
private void createNotification() { private void createTransientNotification() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return; return;
} }
if (notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) != null) {
startForeground(NOTIFICATION_ID, new Notification.Builder(this,PERSIST_NTC_CHANNEL_ID)
.setContentTitle(CIMCacheManager.getString(this,CIMCacheManager.KEY_NTC_CHANNEL_NAME))
.build());
return;
}
if (notificationManager.getNotificationChannel(TRANSIENT_NTC_CHANNEL_ID) == null) { if (notificationManager.getNotificationChannel(TRANSIENT_NTC_CHANNEL_ID) == null) {
NotificationChannel channel = new NotificationChannel(TRANSIENT_NTC_CHANNEL_ID, getClass().getSimpleName(), NotificationManager.IMPORTANCE_LOW); NotificationChannel channel = new NotificationChannel(TRANSIENT_NTC_CHANNEL_ID, getClass().getSimpleName(), NotificationManager.IMPORTANCE_LOW);
channel.enableLights(false); channel.enableLights(false);
@ -255,16 +264,17 @@ public class CIMPushService extends Service {
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);
} }
Notification notification = new Notification.Builder(this,TRANSIENT_NTC_CHANNEL_ID) startForeground(NOTIFICATION_ID, new Notification.Builder(this,TRANSIENT_NTC_CHANNEL_ID)
.setContentTitle(CIMPushService.class.getSimpleName()) .setContentTitle(CIMPushService.class.getSimpleName())
.build(); .build());
startForeground(NOTIFICATION_ID, notification);
} }
private void createPersistNotification(String channelName ,String message,int icon) { private void createPersistNotification(String channelName ,String message,int icon) {
CIMCacheManager.putString(this,CIMCacheManager.KEY_NTC_CHANNEL_NAME,channelName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) == null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) == null) {
NotificationChannel channel = new NotificationChannel(PERSIST_NTC_CHANNEL_ID,channelName, NotificationManager.IMPORTANCE_DEFAULT); NotificationChannel channel = new NotificationChannel(PERSIST_NTC_CHANNEL_ID,channelName, NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(false); channel.enableLights(false);
@ -272,6 +282,7 @@ public class CIMPushService extends Service {
channel.enableVibration(false); channel.enableVibration(false);
channel.setSound(null, null); channel.setSound(null, null);
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);
notificationManager.deleteNotificationChannel(TRANSIENT_NTC_CHANNEL_ID);
} }
@ -294,7 +305,7 @@ public class CIMPushService extends Service {
.setContentTitle(channelName) .setContentTitle(channelName)
.setContentText(message); .setContentText(message);
startForeground(PERSIST_NOTIFICATION_ID, builder.build()); startForeground(NOTIFICATION_ID, builder.build());
} }
private class KeepAliveBroadcastReceiver extends BroadcastReceiver { private class KeepAliveBroadcastReceiver extends BroadcastReceiver {