mirror of
https://gitee.com/farsunset/cim.git
synced 2025-08-02 20:45:46 +08:00
android8的服务通知优化细节
This commit is contained in:
parent
cb5e2acde0
commit
2782d50241
@ -144,7 +144,7 @@ class CIMConnectorManager {
|
|||||||
/*
|
/*
|
||||||
*read 返回 <= 0的情况,发生了意外需要断开重链
|
*read 返回 <= 0的情况,发生了意外需要断开重链
|
||||||
*/
|
*/
|
||||||
closeSession();
|
close();
|
||||||
|
|
||||||
} catch (ConnectException | SocketTimeoutException ignore) {
|
} catch (ConnectException | SocketTimeoutException ignore) {
|
||||||
handleConnectAbortedEvent();
|
handleConnectAbortedEvent();
|
||||||
@ -154,11 +154,7 @@ class CIMConnectorManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void destroy() {
|
public void close() {
|
||||||
closeSession();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void closeSession() {
|
|
||||||
|
|
||||||
if (!isConnected()) {
|
if (!isConnected()) {
|
||||||
return;
|
return;
|
||||||
@ -168,7 +164,7 @@ class CIMConnectorManager {
|
|||||||
socketChannel.close();
|
socketChannel.close();
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
} finally {
|
} finally {
|
||||||
this.sessionClosed();
|
this.onSessionClosed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,6 +172,9 @@ class CIMConnectorManager {
|
|||||||
return socketChannel != null && socketChannel.isConnected();
|
return socketChannel != null && socketChannel.isConnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendHeartbeat() {
|
||||||
|
send(HeartbeatResponse.getInstance());
|
||||||
|
}
|
||||||
|
|
||||||
public void send(final Protobufable body) {
|
public void send(final Protobufable body) {
|
||||||
|
|
||||||
@ -197,9 +196,9 @@ class CIMConnectorManager {
|
|||||||
} finally {
|
} finally {
|
||||||
|
|
||||||
if (result <= 0) {
|
if (result <= 0) {
|
||||||
closeSession();
|
close();
|
||||||
} else {
|
} else {
|
||||||
messageSent(body);
|
onMessageSent(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -207,7 +206,7 @@ class CIMConnectorManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void sessionCreated() {
|
private void onSessionCreated() {
|
||||||
LOGGER.sessionCreated(socketChannel);
|
LOGGER.sessionCreated(socketChannel);
|
||||||
|
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
@ -217,7 +216,7 @@ class CIMConnectorManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sessionClosed() {
|
private void onSessionClosed() {
|
||||||
|
|
||||||
idleHandler.removeMessages(0);
|
idleHandler.removeMessages(0);
|
||||||
|
|
||||||
@ -230,15 +229,15 @@ class CIMConnectorManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sessionIdle() {
|
private void onSessionIdle() {
|
||||||
|
|
||||||
LOGGER.sessionIdle(socketChannel);
|
LOGGER.sessionIdle(socketChannel);
|
||||||
|
|
||||||
closeSession();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void messageReceived(Object obj) {
|
private void onMessageReceived(Object obj) {
|
||||||
|
|
||||||
if (obj instanceof Message) {
|
if (obj instanceof Message) {
|
||||||
|
|
||||||
@ -260,7 +259,7 @@ class CIMConnectorManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void messageSent(Object message) {
|
private void onMessageSent(Object message) {
|
||||||
|
|
||||||
LOGGER.messageSent(socketChannel, message);
|
LOGGER.messageSent(socketChannel, message);
|
||||||
|
|
||||||
@ -276,12 +275,12 @@ class CIMConnectorManager {
|
|||||||
private final Handler idleHandler = new Handler(IDLE_HANDLER_THREAD.getLooper()) {
|
private final Handler idleHandler = new Handler(IDLE_HANDLER_THREAD.getLooper()) {
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(android.os.Message m) {
|
public void handleMessage(android.os.Message m) {
|
||||||
sessionIdle();
|
onSessionIdle();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private void handleDisconnectedEvent() {
|
private void handleDisconnectedEvent() {
|
||||||
closeSession();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleConnectAbortedEvent() {
|
private void handleConnectAbortedEvent() {
|
||||||
@ -302,7 +301,7 @@ class CIMConnectorManager {
|
|||||||
|
|
||||||
closeCountDown();
|
closeCountDown();
|
||||||
|
|
||||||
sessionCreated();
|
onSessionCreated();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -319,7 +318,7 @@ class CIMConnectorManager {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.messageReceived(message);
|
this.onMessageReceived(message);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,10 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.farsunset.cim.sdk.android;
|
package com.farsunset.cim.sdk.android;
|
||||||
|
|
||||||
import android.app.Notification;
|
import android.app.*;
|
||||||
import android.app.NotificationChannel;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@ -35,8 +32,8 @@ import android.os.Build;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
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.constant.CIMConstant;
|
||||||
|
import com.farsunset.cim.sdk.android.logger.CIMLogger;
|
||||||
import com.farsunset.cim.sdk.android.model.SentBody;
|
import com.farsunset.cim.sdk.android.model.SentBody;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,19 +42,21 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
|||||||
* @author 3979434
|
* @author 3979434
|
||||||
*/
|
*/
|
||||||
public class CIMPushService extends Service {
|
public class CIMPushService extends Service {
|
||||||
|
|
||||||
public final static String KEY_DELAYED_TIME = "KEY_DELAYED_TIME";
|
public final static String KEY_DELAYED_TIME = "KEY_DELAYED_TIME";
|
||||||
public final static String KEY_LOGGER_ENABLE = "KEY_LOGGER_ENABLE";
|
public final static String KEY_LOGGER_ENABLE = "KEY_LOGGER_ENABLE";
|
||||||
|
|
||||||
private final static int NOTIFICATION_ID = Integer.MAX_VALUE;
|
private final static int NOTIFICATION_ID = Integer.MAX_VALUE;
|
||||||
|
|
||||||
private CIMConnectorManager manager;
|
private CIMConnectorManager connectorManager;
|
||||||
private KeepAliveBroadcastReceiver keepAliveReceiver;
|
private KeepAliveBroadcastReceiver keepAliveReceiver;
|
||||||
private ConnectivityManager connectivityManager;
|
private ConnectivityManager connectivityManager;
|
||||||
|
private NotificationManager notificationManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
|
||||||
keepAliveReceiver = new KeepAliveBroadcastReceiver();
|
keepAliveReceiver = new KeepAliveBroadcastReceiver();
|
||||||
@ -66,14 +65,14 @@ public class CIMPushService extends Service {
|
|||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
|
||||||
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
connectivityManager = getSystemService(ConnectivityManager.class);
|
||||||
|
|
||||||
connectivityManager.registerDefaultNetworkCallback(networkCallback);
|
connectivityManager.registerDefaultNetworkCallback(networkCallback);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onAvailable(Network network) {
|
public void onAvailable(Network network) {
|
||||||
Intent intent = new Intent();
|
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
|
@Override
|
||||||
public void handleMessage(android.os.Message message) {
|
public void handleMessage(android.os.Message message) {
|
||||||
connect();
|
prepareConnect();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
final Handler notificationHandler = new Handler() {
|
private final Handler notificationHandler = new Handler() {
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(android.os.Message message) {
|
public void handleMessage(android.os.Message message) {
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
@ -109,32 +108,20 @@ public class CIMPushService extends Service {
|
|||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
createNotification();
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
String action = intent == null ? CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE : intent.getAction();
|
String action = intent == null ? CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE : intent.getAction();
|
||||||
|
|
||||||
if (CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
|
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)) {
|
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)) {
|
if (CIMPushManager.ACTION_CLOSE_CIM_CONNECTION.equals(action)) {
|
||||||
manager.closeSession();
|
connectorManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action)) {
|
if (CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action)) {
|
||||||
@ -147,8 +134,8 @@ public class CIMPushService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (CIMPushManager.ACTION_DESTROY_CIM_SERVICE.equals(action)) {
|
if (CIMPushManager.ACTION_DESTROY_CIM_SERVICE.equals(action)) {
|
||||||
manager.destroy();
|
connectorManager.close();
|
||||||
stopSelf();
|
this.stopSelf();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
@ -158,18 +145,17 @@ public class CIMPushService extends Service {
|
|||||||
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) {
|
if (delayMillis <= 0) {
|
||||||
connect();
|
this.prepareConnect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
connectHandler.sendEmptyMessageDelayed(0, delayMillis);
|
connectHandler.sendEmptyMessageDelayed(0, delayMillis);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connect() {
|
private void prepareConnect() {
|
||||||
|
|
||||||
if (CIMPushManager.isDestroyed(this) || CIMPushManager.isStopped(this)) {
|
if (CIMPushManager.isDestroyed(this) || CIMPushManager.isStopped(this)) {
|
||||||
return;
|
return;
|
||||||
@ -183,19 +169,20 @@ public class CIMPushService extends Service {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
manager.connect(host, port);
|
connectorManager.connect(host, port);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleKeepAlive() {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
connect();
|
this.prepareConnect();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -203,9 +190,16 @@ public class CIMPushService extends Service {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void release() {
|
||||||
|
|
||||||
public void release() {
|
|
||||||
connectHandler.removeMessages(0);
|
connectHandler.removeMessages(0);
|
||||||
|
|
||||||
notificationHandler.removeMessages(0);
|
notificationHandler.removeMessages(0);
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
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) {
|
||||||
@Override
|
return;
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class KeepAliveBroadcastReceiver extends BroadcastReceiver {
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private class KeepAliveBroadcastReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context arg0, Intent arg1) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
handleKeepAlive();
|
handleKeepAlive();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,2 +1,22 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module type="JAVA_MODULE" version="4" />
|
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||||
|
<output url="file://$MODULE_DIR$/target/classes" />
|
||||||
|
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.44.Final" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.44.Final" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: io.netty:netty-codec-http:4.1.44.Final" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.44.Final" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.44.Final" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.44.Final" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.44.Final" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.11.1" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" />
|
||||||
|
</component>
|
||||||
|
</module>
|
Loading…
x
Reference in New Issue
Block a user