diff --git a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMConnectorManager.java b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMConnectorManager.java
index 6439f67..d4df1ba 100644
--- a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMConnectorManager.java
+++ b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMConnectorManager.java
@@ -144,7 +144,7 @@ class CIMConnectorManager {
/*
*read 返回 <= 0的情况,发生了意外需要断开重链
*/
- closeSession();
+ close();
} catch (ConnectException | SocketTimeoutException ignore) {
handleConnectAbortedEvent();
@@ -154,11 +154,7 @@ class CIMConnectorManager {
});
}
- public void destroy() {
- closeSession();
- }
-
- public void closeSession() {
+ public void close() {
if (!isConnected()) {
return;
@@ -168,7 +164,7 @@ class CIMConnectorManager {
socketChannel.close();
} catch (IOException ignore) {
} finally {
- this.sessionClosed();
+ this.onSessionClosed();
}
}
@@ -176,6 +172,9 @@ class CIMConnectorManager {
return socketChannel != null && socketChannel.isConnected();
}
+ public void sendHeartbeat() {
+ send(HeartbeatResponse.getInstance());
+ }
public void send(final Protobufable body) {
@@ -197,9 +196,9 @@ class CIMConnectorManager {
} finally {
if (result <= 0) {
- closeSession();
+ close();
} else {
- messageSent(body);
+ onMessageSent(body);
}
}
});
@@ -207,7 +206,7 @@ class CIMConnectorManager {
}
- private void sessionCreated() {
+ private void onSessionCreated() {
LOGGER.sessionCreated(socketChannel);
Intent intent = new Intent();
@@ -217,7 +216,7 @@ class CIMConnectorManager {
}
- private void sessionClosed() {
+ private void onSessionClosed() {
idleHandler.removeMessages(0);
@@ -230,15 +229,15 @@ class CIMConnectorManager {
}
- private void sessionIdle() {
+ private void onSessionIdle() {
LOGGER.sessionIdle(socketChannel);
- closeSession();
+ close();
}
- private void messageReceived(Object obj) {
+ private void onMessageReceived(Object obj) {
if (obj instanceof Message) {
@@ -260,7 +259,7 @@ class CIMConnectorManager {
}
- private void messageSent(Object message) {
+ private void onMessageSent(Object message) {
LOGGER.messageSent(socketChannel, message);
@@ -276,12 +275,12 @@ class CIMConnectorManager {
private final Handler idleHandler = new Handler(IDLE_HANDLER_THREAD.getLooper()) {
@Override
public void handleMessage(android.os.Message m) {
- sessionIdle();
+ onSessionIdle();
}
};
private void handleDisconnectedEvent() {
- closeSession();
+ close();
}
private void handleConnectAbortedEvent() {
@@ -302,7 +301,7 @@ class CIMConnectorManager {
closeCountDown();
- sessionCreated();
+ onSessionCreated();
}
@@ -319,7 +318,7 @@ class CIMConnectorManager {
return;
}
- this.messageReceived(message);
+ this.onMessageReceived(message);
}
diff --git a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushService.java b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushService.java
index 3c7d99d..9328afd 100644
--- a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushService.java
+++ b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushService.java
@@ -21,10 +21,7 @@
*/
package com.farsunset.cim.sdk.android;
-import android.app.Notification;
-import android.app.NotificationChannel;
-import android.app.NotificationManager;
-import android.app.Service;
+import android.app.*;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -35,8 +32,8 @@ import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
-import com.farsunset.cim.sdk.android.logger.CIMLogger;
import com.farsunset.cim.sdk.android.constant.CIMConstant;
+import com.farsunset.cim.sdk.android.logger.CIMLogger;
import com.farsunset.cim.sdk.android.model.SentBody;
/**
@@ -45,19 +42,21 @@ import com.farsunset.cim.sdk.android.model.SentBody;
* @author 3979434
*/
public class CIMPushService extends Service {
+
public final static String KEY_DELAYED_TIME = "KEY_DELAYED_TIME";
public final static String KEY_LOGGER_ENABLE = "KEY_LOGGER_ENABLE";
private final static int NOTIFICATION_ID = Integer.MAX_VALUE;
- private CIMConnectorManager manager;
+ private CIMConnectorManager connectorManager;
private KeepAliveBroadcastReceiver keepAliveReceiver;
private ConnectivityManager connectivityManager;
+ private NotificationManager notificationManager;
@Override
public void onCreate() {
- manager = CIMConnectorManager.getManager(this.getApplicationContext());
-
+ connectorManager = CIMConnectorManager.getManager(this.getApplicationContext());
+ notificationManager = getSystemService(NotificationManager.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
keepAliveReceiver = new KeepAliveBroadcastReceiver();
@@ -66,14 +65,14 @@ public class CIMPushService extends Service {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
- connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
+ connectivityManager = getSystemService(ConnectivityManager.class);
connectivityManager.registerDefaultNetworkCallback(networkCallback);
}
}
- final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
+ private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Intent intent = new Intent();
@@ -92,14 +91,14 @@ public class CIMPushService extends Service {
};
- final Handler connectHandler = new Handler() {
+ private final Handler connectHandler = new Handler() {
@Override
public void handleMessage(android.os.Message message) {
- connect();
+ prepareConnect();
}
};
- final Handler notificationHandler = new Handler() {
+ private final Handler notificationHandler = new Handler() {
@Override
public void handleMessage(android.os.Message message) {
stopForeground(true);
@@ -109,32 +108,20 @@ public class CIMPushService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- NotificationChannel channel = new NotificationChannel(getClass().getSimpleName(), getClass().getSimpleName(), NotificationManager.IMPORTANCE_LOW);
- channel.enableLights(false);
- channel.enableVibration(false);
- channel.setSound(null, null);
- notificationManager.createNotificationChannel(channel);
- Notification notification = new Notification.Builder(this, channel.getId())
- .setContentTitle("Push service")
- .setContentText("Push service is running")
- .build();
- startForeground(NOTIFICATION_ID, notification);
- }
+ createNotification();
String action = intent == null ? CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE : intent.getAction();
if (CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
- connect(intent.getLongExtra(KEY_DELAYED_TIME, 0));
+ this.prepareConnect(intent.getLongExtra(KEY_DELAYED_TIME, 0));
}
if (CIMPushManager.ACTION_SEND_REQUEST_BODY.equals(action)) {
- manager.send((SentBody) intent.getSerializableExtra(CIMPushManager.KEY_SEND_BODY));
+ connectorManager.send((SentBody) intent.getSerializableExtra(CIMPushManager.KEY_SEND_BODY));
}
if (CIMPushManager.ACTION_CLOSE_CIM_CONNECTION.equals(action)) {
- manager.closeSession();
+ connectorManager.close();
}
if (CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action)) {
@@ -147,29 +134,28 @@ public class CIMPushService extends Service {
}
if (CIMPushManager.ACTION_DESTROY_CIM_SERVICE.equals(action)) {
- manager.destroy();
- stopSelf();
+ connectorManager.close();
+ this.stopSelf();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationHandler.sendEmptyMessageDelayed(0, 1000);
}
- return super.onStartCommand(intent, flags, startId);
+ return super.onStartCommand(intent,flags,startId);
}
- private void connect(long delayMillis) {
+ private void prepareConnect(long delayMillis) {
if (delayMillis <= 0) {
- connect();
+ this.prepareConnect();
return;
}
connectHandler.sendEmptyMessageDelayed(0, delayMillis);
-
}
- private void connect() {
+ private void prepareConnect() {
if (CIMPushManager.isDestroyed(this) || CIMPushManager.isStopped(this)) {
return;
@@ -183,19 +169,20 @@ public class CIMPushService extends Service {
return;
}
- manager.connect(host, port);
+ connectorManager.connect(host, port);
}
private void handleKeepAlive() {
- if (manager.isConnected()) {
- CIMLogger.getLogger().connectState(true,CIMPushManager.isStopped(this),CIMPushManager.isDestroyed(this));
+ CIMLogger.getLogger().connectState(true, CIMPushManager.isStopped(this), CIMPushManager.isDestroyed(this));
+
+ if (connectorManager.isConnected()) {
+ connectorManager.sendHeartbeat();
return;
}
- connect();
-
+ this.prepareConnect();
}
@Override
@@ -203,9 +190,16 @@ public class CIMPushService extends Service {
return null;
}
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ release();
+ }
+
+ private void release() {
- public void release() {
connectHandler.removeMessages(0);
+
notificationHandler.removeMessages(0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -217,18 +211,34 @@ public class CIMPushService extends Service {
}
}
+ private void createNotification() {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
+ return;
+ }
- @Override
- public void onDestroy() {
- super.onDestroy();
- release();
+ String channelId = getClass().getName();
+
+ if (notificationManager.getNotificationChannel(channelId) == null) {
+ NotificationChannel channel = new NotificationChannel(channelId, getClass().getSimpleName(), NotificationManager.IMPORTANCE_LOW);
+ channel.enableLights(false);
+ channel.enableVibration(false);
+ channel.setSound(null, null);
+ notificationManager.createNotificationChannel(channel);
+ }
+
+ Notification notification = new Notification.Builder(this,channelId)
+ .setContentTitle(CIMPushService.class.getSimpleName())
+ .build();
+
+ startForeground(NOTIFICATION_ID, notification);
}
- public class KeepAliveBroadcastReceiver extends BroadcastReceiver {
+
+ private class KeepAliveBroadcastReceiver extends BroadcastReceiver {
@Override
- public void onReceive(Context arg0, Intent arg1) {
+ public void onReceive(Context context, Intent intent) {
handleKeepAlive();
}
diff --git a/cim-server-sdk/cim-server-sdk-netty/cim-server-sdk-netty.iml b/cim-server-sdk/cim-server-sdk-netty/cim-server-sdk-netty.iml
index 78b2cc5..cb8ebf2 100644
--- a/cim-server-sdk/cim-server-sdk-netty/cim-server-sdk-netty.iml
+++ b/cim-server-sdk/cim-server-sdk-netty/cim-server-sdk-netty.iml
@@ -1,2 +1,22 @@
-
\ No newline at end of file
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file