修改android12中可能出现ForegroundServiceStartNotAllowedException的问题

This commit is contained in:
远方夕阳 2022-08-27 23:01:15 +08:00
parent 69cbb31d9e
commit 7a9a369960
18 changed files with 316 additions and 191 deletions

View File

@ -189,5 +189,5 @@ https://www.yuque.com/yuanfangxiyang/ma4ytb/vvy3iz/edit#nnzKN
android端sdk引用 android端sdk引用
``` ```
implementation "com.farsunset:cim-android-sdk:4.2.0" implementation "com.farsunset:cim-android-sdk:4.2.5"
``` ```

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.0</version> <version>4.2.5</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name> <name>${project.groupId}:${project.artifactId}</name>

View File

@ -27,6 +27,7 @@ import android.os.Handler;
import com.farsunset.cim.sdk.android.coder.ClientMessageDecoder; import com.farsunset.cim.sdk.android.coder.ClientMessageDecoder;
import com.farsunset.cim.sdk.android.coder.ClientMessageEncoder; import com.farsunset.cim.sdk.android.coder.ClientMessageEncoder;
import com.farsunset.cim.sdk.android.constant.CIMConstant; import com.farsunset.cim.sdk.android.constant.CIMConstant;
import com.farsunset.cim.sdk.android.constant.IntentAction;
import com.farsunset.cim.sdk.android.logger.CIMLogger; import com.farsunset.cim.sdk.android.logger.CIMLogger;
import com.farsunset.cim.sdk.android.model.*; import com.farsunset.cim.sdk.android.model.*;
@ -95,7 +96,7 @@ class CIMConnectorManager {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setPackage(context.getPackageName()); intent.setPackage(context.getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_CONNECT_FAILED); intent.setAction(IntentAction.ACTION_CONNECT_FAILED);
context.sendBroadcast(intent); context.sendBroadcast(intent);
return; return;
@ -205,7 +206,7 @@ class CIMConnectorManager {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setPackage(context.getPackageName()); intent.setPackage(context.getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_CONNECT_FINISHED); intent.setAction(IntentAction.ACTION_CONNECT_FINISHED);
context.sendBroadcast(intent); context.sendBroadcast(intent);
} }
@ -218,7 +219,7 @@ class CIMConnectorManager {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setPackage(context.getPackageName()); intent.setPackage(context.getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED); intent.setAction(IntentAction.ACTION_CONNECTION_CLOSED);
context.sendBroadcast(intent); context.sendBroadcast(intent);
} }
@ -237,7 +238,7 @@ class CIMConnectorManager {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setPackage(context.getPackageName()); intent.setPackage(context.getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_MESSAGE_RECEIVED); intent.setAction(IntentAction.ACTION_MESSAGE_RECEIVED);
intent.putExtra(Message.class.getName(), (Message) obj); intent.putExtra(Message.class.getName(), (Message) obj);
context.sendBroadcast(intent); context.sendBroadcast(intent);
@ -246,7 +247,7 @@ class CIMConnectorManager {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setPackage(context.getPackageName()); intent.setPackage(context.getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_REPLY_RECEIVED); intent.setAction(IntentAction.ACTION_REPLY_RECEIVED);
intent.putExtra(ReplyBody.class.getName(), (ReplyBody) obj); intent.putExtra(ReplyBody.class.getName(), (ReplyBody) obj);
context.sendBroadcast(intent); context.sendBroadcast(intent);
} }
@ -260,7 +261,7 @@ class CIMConnectorManager {
if (message instanceof SentBody) { if (message instanceof SentBody) {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setPackage(context.getPackageName()); intent.setPackage(context.getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_SEND_FINISHED); intent.setAction(IntentAction.ACTION_SEND_FINISHED);
intent.putExtra(SentBody.class.getName(), (SentBody) message); intent.putExtra(SentBody.class.getName(), (SentBody) message);
context.sendBroadcast(intent); context.sendBroadcast(intent);
} }
@ -288,7 +289,7 @@ class CIMConnectorManager {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setPackage(context.getPackageName()); intent.setPackage(context.getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_CONNECT_FAILED); intent.setAction(IntentAction.ACTION_CONNECT_FAILED);
intent.putExtra("interval", interval); intent.putExtra("interval", interval);
context.sendBroadcast(intent); context.sendBroadcast(intent);

View File

@ -25,8 +25,10 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.os.Build; import com.farsunset.cim.sdk.android.constant.BundleKey;
import com.farsunset.cim.sdk.android.constant.CIMConstant; import com.farsunset.cim.sdk.android.constant.CIMConstant;
import com.farsunset.cim.sdk.android.constant.IntentAction;
import com.farsunset.cim.sdk.android.constant.ServiceAction;
import com.farsunset.cim.sdk.android.model.Message; import com.farsunset.cim.sdk.android.model.Message;
import com.farsunset.cim.sdk.android.model.ReplyBody; import com.farsunset.cim.sdk.android.model.ReplyBody;
import com.farsunset.cim.sdk.android.model.SentBody; import com.farsunset.cim.sdk.android.model.SentBody;
@ -38,7 +40,6 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
protected Context context; protected Context context;
@SuppressWarnings("deprecation")
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@ -50,6 +51,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
* 操作事件广播用于提高service存活率 * 操作事件广播用于提高service存活率
*/ */
if (Intent.ACTION_USER_PRESENT.equals(action) if (Intent.ACTION_USER_PRESENT.equals(action)
|| Intent.ACTION_BOOT_COMPLETED.equals(action)
|| Intent.ACTION_POWER_CONNECTED.equals(action) || Intent.ACTION_POWER_CONNECTED.equals(action)
|| Intent.ACTION_POWER_DISCONNECTED.equals(action)) { || Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
startPushService(); startPushService();
@ -58,7 +60,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
/* /*
* 设备网络状态变化事件 * 设备网络状态变化事件
*/ */
if (CIMConstant.IntentAction.ACTION_NETWORK_CHANGED.equals(action) if (IntentAction.ACTION_NETWORK_CHANGED.equals(action)
|| ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) { || ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
onDevicesNetworkChanged(); onDevicesNetworkChanged();
@ -67,14 +69,14 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
/* /*
* cim断开服务器事件 * cim断开服务器事件
*/ */
if (CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED.equals(action)) { if (IntentAction.ACTION_CONNECTION_CLOSED.equals(action)) {
onInnerConnectionClosed(); onInnerConnectionClosed();
} }
/* /*
* cim连接服务器失败事件 * cim连接服务器失败事件
*/ */
if (CIMConstant.IntentAction.ACTION_CONNECT_FAILED.equals(action)) { if (IntentAction.ACTION_CONNECT_FAILED.equals(action)) {
long interval = intent.getLongExtra("interval", CIMConstant.RECONNECT_INTERVAL_TIME); long interval = intent.getLongExtra("interval", CIMConstant.RECONNECT_INTERVAL_TIME);
onInnerConnectFailed(interval); onInnerConnectFailed(interval);
} }
@ -82,21 +84,21 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
/* /*
* cim连接服务器成功事件 * cim连接服务器成功事件
*/ */
if (CIMConstant.IntentAction.ACTION_CONNECT_FINISHED.equals(action)) { if (IntentAction.ACTION_CONNECT_FINISHED.equals(action)) {
onInnerConnectFinished(); onInnerConnectFinished();
} }
/* /*
* 收到推送消息事件 * 收到推送消息事件
*/ */
if (CIMConstant.IntentAction.ACTION_MESSAGE_RECEIVED.equals(action)) { if (IntentAction.ACTION_MESSAGE_RECEIVED.equals(action)) {
onInnerMessageReceived((Message) intent.getSerializableExtra(Message.class.getName()), intent); onInnerMessageReceived((Message) intent.getSerializableExtra(Message.class.getName()), intent);
} }
/* /*
* 获取收到replyBody成功事件 * 获取收到replyBody成功事件
*/ */
if (CIMConstant.IntentAction.ACTION_REPLY_RECEIVED.equals(action)) { if (IntentAction.ACTION_REPLY_RECEIVED.equals(action)) {
onReplyReceived((ReplyBody) intent.getSerializableExtra(ReplyBody.class.getName())); onReplyReceived((ReplyBody) intent.getSerializableExtra(ReplyBody.class.getName()));
} }
@ -104,27 +106,26 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
/* /*
* 获取sendBody发送成功事件 * 获取sendBody发送成功事件
*/ */
if (CIMConstant.IntentAction.ACTION_SEND_FINISHED.equals(action)) { if (IntentAction.ACTION_SEND_FINISHED.equals(action)) {
onSentSucceed((SentBody) intent.getSerializableExtra(SentBody.class.getName())); onSentSucceed((SentBody) intent.getSerializableExtra(SentBody.class.getName()));
} }
/* /*
* 重新连接如果断开的话 * 重新连接如果断开的话
*/ */
if (CIMConstant.IntentAction.ACTION_CONNECTION_RECOVERY.equals(action)) { if (IntentAction.ACTION_CONNECTION_RECOVERY.equals(action)) {
connect(0); connect(0);
} }
} }
private void startPushService() { private void startPushService() {
try {
Intent intent = new Intent(context, CIMPushService.class); Intent intent = new Intent(context, CIMPushService.class);
intent.setAction(CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE); intent.setAction(ServiceAction.ACTION_ACTIVATE_PUSH_SERVICE);
CIMPushManager.startService(context,intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { }catch (Exception ignore){
context.startForegroundService(intent); context.sendBroadcast(new Intent(IntentAction.ACTION_CONNECTION_RECOVERY));
} else {
context.startService(intent);
} }
} }
@ -167,8 +168,8 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
private void connect(long delay) { private void connect(long delay) {
Intent serviceIntent = new Intent(context, CIMPushService.class); Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.putExtra(CIMPushService.KEY_DELAYED_TIME, delay); serviceIntent.putExtra(BundleKey.KEY_DELAYED_TIME, delay);
serviceIntent.setAction(CIMPushManager.ACTION_CREATE_CIM_CONNECTION); serviceIntent.setAction(ServiceAction.ACTION_CREATE_CIM_CONNECTION);
CIMPushManager.startService(context, serviceIntent); CIMPushManager.startService(context, serviceIntent);
} }
@ -181,12 +182,11 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
} }
private boolean isForceOfflineMessage(String action) { private boolean isForceOfflineMessage(String action) {
return CIMConstant.MessageAction.ACTION_999.equals(action); return CIMConstant.ACTION_999.equals(action);
} }
/** /**
* 接收消息实现方法 * 接收消息实现方法
*
* @param message * @param message
* @param intent * @param intent
*/ */

View File

@ -30,7 +30,9 @@ import android.net.NetworkInfo;
import android.os.Build; 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.CIMConstant; import com.farsunset.cim.sdk.android.constant.BundleKey;
import com.farsunset.cim.sdk.android.constant.RequestKey;
import com.farsunset.cim.sdk.android.constant.ServiceAction;
import com.farsunset.cim.sdk.android.logger.CIMLogger; import com.farsunset.cim.sdk.android.logger.CIMLogger;
import com.farsunset.cim.sdk.android.model.SentBody; import com.farsunset.cim.sdk.android.model.SentBody;
@ -43,24 +45,6 @@ import java.util.UUID;
public class CIMPushManager { public class CIMPushManager {
protected static final String ACTION_CREATE_CIM_CONNECTION = "ACTION_CREATE_CIM_CONNECTION";
protected static final String ACTION_DESTROY_CIM_SERVICE = "ACTION_DESTROY_CIM_SERVICE";
protected static final String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE";
protected static final String ACTION_SEND_REQUEST_BODY = "ACTION_SEND_REQUEST_BODY";
protected static final String ACTION_CLOSE_CIM_CONNECTION = "ACTION_CLOSE_CIM_CONNECTION";
protected static final String ACTION_SET_LOGGER_EATABLE = "ACTION_SET_LOGGER_EATABLE";
protected static final String ACTION_SHOW_PERSIST_NOTIFICATION = "ACTION_SHOW_PERSIST_NOTIFICATION";
protected static final String ACTION_HIDE_PERSIST_NOTIFICATION = "ACTION_HIDE_PERSIST_NOTIFICATION";
protected static final String ACTION_CIM_CONNECTION_PONG = "ACTION_CIM_CONNECTION_PONG";
/** /**
* 初始化,连接服务端在程序启动页或者 在Application里调用 * 初始化,连接服务端在程序启动页或者 在Application里调用
* @param context * @param context
@ -82,7 +66,7 @@ public class CIMPushManager {
Intent serviceIntent = new Intent(context, CIMPushService.class); Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ACTION_CREATE_CIM_CONNECTION); serviceIntent.setAction(ServiceAction.ACTION_CREATE_CIM_CONNECTION);
startService(context, serviceIntent); startService(context, serviceIntent);
} }
@ -94,8 +78,8 @@ public class CIMPushManager {
*/ */
public static void setLoggerEnable(Context context, boolean enable) { public static void setLoggerEnable(Context context, boolean enable) {
Intent serviceIntent = new Intent(context, CIMPushService.class); Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.putExtra(CIMPushService.KEY_LOGGER_ENABLE, enable); serviceIntent.putExtra(BundleKey.KEY_LOGGER_ENABLE, enable);
serviceIntent.setAction(ACTION_SET_LOGGER_EATABLE); serviceIntent.setAction(ServiceAction.ACTION_SET_LOGGER_EATABLE);
startService(context, serviceIntent); startService(context, serviceIntent);
} }
@ -109,10 +93,10 @@ public class CIMPushManager {
*/ */
public static void startForeground(Context context,int icon, String channel , String message) { public static void startForeground(Context context,int icon, String channel , String message) {
Intent serviceIntent = new Intent(context, CIMPushService.class); Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.putExtra(CIMPushService.KEY_NOTIFICATION_MESSAGE, message); serviceIntent.putExtra(BundleKey.KEY_NOTIFICATION_MESSAGE, message);
serviceIntent.putExtra(CIMPushService.KEY_NOTIFICATION_CHANNEL, channel); serviceIntent.putExtra(BundleKey.KEY_NOTIFICATION_CHANNEL, channel);
serviceIntent.putExtra(CIMPushService.KEY_NOTIFICATION_ICON, icon); serviceIntent.putExtra(BundleKey.KEY_NOTIFICATION_ICON, icon);
serviceIntent.setAction(ACTION_SHOW_PERSIST_NOTIFICATION); serviceIntent.setAction(ServiceAction.ACTION_SHOW_PERSIST_NOTIFICATION);
startService(context, serviceIntent); startService(context, serviceIntent);
} }
@ -122,7 +106,7 @@ public class CIMPushManager {
*/ */
public static void cancelForeground(Context context) { public static void cancelForeground(Context context) {
Intent serviceIntent = new Intent(context, CIMPushService.class); Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ACTION_HIDE_PERSIST_NOTIFICATION); serviceIntent.setAction(ServiceAction.ACTION_HIDE_PERSIST_NOTIFICATION);
startService(context, serviceIntent); startService(context, serviceIntent);
} }
@ -159,7 +143,7 @@ public class CIMPushManager {
public static void setTag(Context context, String tag) { public static void setTag(Context context, String tag) {
SentBody sent = new SentBody(); SentBody sent = new SentBody();
sent.setKey(CIMConstant.RequestKey.CLIENT_SET_TAG); sent.setKey(RequestKey.CLIENT_SET_TAG);
sent.put("tag", tag); sent.put("tag", tag);
sendRequest(context, sent); sendRequest(context, sent);
@ -172,7 +156,7 @@ public class CIMPushManager {
public static void removeTag(Context context) { public static void removeTag(Context context) {
SentBody sent = new SentBody(); SentBody sent = new SentBody();
sent.setKey(CIMConstant.RequestKey.CLIENT_REMOVE_TAG); sent.setKey(RequestKey.CLIENT_REMOVE_TAG);
sendRequest(context, sent); sendRequest(context, sent);
} }
@ -187,7 +171,7 @@ public class CIMPushManager {
} }
Intent serviceIntent = new Intent(context, CIMPushService.class); Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ACTION_CIM_CONNECTION_PONG); serviceIntent.setAction(ServiceAction.ACTION_CIM_CONNECTION_PONG);
startService(context, serviceIntent); startService(context, serviceIntent);
} }
@ -199,7 +183,7 @@ public class CIMPushManager {
CIMCacheManager.putString(context, CIMCacheManager.KEY_UID, uid); CIMCacheManager.putString(context, CIMCacheManager.KEY_UID, uid);
SentBody sent = new SentBody(); SentBody sent = new SentBody();
sent.setKey(CIMConstant.RequestKey.CLIENT_BIND); sent.setKey(RequestKey.CLIENT_BIND);
sent.put("uid", String.valueOf(uid)); sent.put("uid", String.valueOf(uid));
sent.put("channel", "android"); sent.put("channel", "android");
sent.put("deviceId", getDeviceId(context)); sent.put("deviceId", getDeviceId(context));
@ -237,8 +221,8 @@ public class CIMPushManager {
} }
Intent serviceIntent = new Intent(context, CIMPushService.class); Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.putExtra(CIMPushService.KEY_SEND_BODY, body); serviceIntent.putExtra(BundleKey.KEY_SEND_BODY, body);
serviceIntent.setAction(ACTION_SEND_REQUEST_BODY); serviceIntent.setAction(ServiceAction.ACTION_SEND_REQUEST_BODY);
startService(context, serviceIntent); startService(context, serviceIntent);
} }
@ -255,7 +239,7 @@ public class CIMPushManager {
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_MANUAL_STOP, true); CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_MANUAL_STOP, true);
Intent serviceIntent = new Intent(context, CIMPushService.class); Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ACTION_CLOSE_CIM_CONNECTION); serviceIntent.setAction(ServiceAction.ACTION_CLOSE_CIM_CONNECTION);
startService(context, serviceIntent); startService(context, serviceIntent);
} }
@ -269,7 +253,7 @@ public class CIMPushManager {
CIMCacheManager.remove(context, CIMCacheManager.KEY_UID); CIMCacheManager.remove(context, CIMCacheManager.KEY_UID);
Intent serviceIntent = new Intent(context, CIMPushService.class); Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ACTION_DESTROY_CIM_SERVICE); serviceIntent.setAction(ServiceAction.ACTION_DESTROY_CIM_SERVICE);
startService(context, serviceIntent); startService(context, serviceIntent);
} }
@ -337,6 +321,7 @@ public class CIMPushManager {
} }
} }
private static String getVersionName(Context context) { private static String getVersionName(Context context) {
try { try {

View File

@ -32,7 +32,9 @@ 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.constant.CIMConstant; import com.farsunset.cim.sdk.android.constant.BundleKey;
import com.farsunset.cim.sdk.android.constant.IntentAction;
import com.farsunset.cim.sdk.android.constant.ServiceAction;
import com.farsunset.cim.sdk.android.logger.CIMLogger; import com.farsunset.cim.sdk.android.logger.CIMLogger;
import com.farsunset.cim.sdk.android.model.Pong; import com.farsunset.cim.sdk.android.model.Pong;
import com.farsunset.cim.sdk.android.model.SentBody; import com.farsunset.cim.sdk.android.model.SentBody;
@ -46,13 +48,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/ */
public class CIMPushService extends Service { public class CIMPushService extends Service {
static final String KEY_SEND_BODY = "KEY_SEND_BODY";
static final String KEY_DELAYED_TIME = "KEY_DELAYED_TIME";
static final String KEY_LOGGER_ENABLE = "KEY_LOGGER_ENABLE";
static final String KEY_NOTIFICATION_MESSAGE = "KEY_NOTIFICATION_MESSAGE";
static final String KEY_NOTIFICATION_CHANNEL = "KEY_NOTIFICATION_CHANNEL";
static final String KEY_NOTIFICATION_ICON = "KEY_NOTIFICATION_ICON";
private static final String TRANSIENT_NTC_CHANNEL_ID = "CIM_PUSH_TRANSIENT_NTC_ID"; private static final String TRANSIENT_NTC_CHANNEL_ID = "CIM_PUSH_TRANSIENT_NTC_ID";
private static final String PERSIST_NTC_CHANNEL_ID = "CIM_PUSH_PERSIST_NTC_ID"; private static final String PERSIST_NTC_CHANNEL_ID = "CIM_PUSH_PERSIST_NTC_ID";
@ -89,20 +84,13 @@ public class CIMPushService extends Service {
private 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(); sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED));
intent.setPackage(getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED);
sendBroadcast(intent);
} }
@Override @Override
public void onLost(Network network) { public void onLost(Network network) {
Intent intent = new Intent(); sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED));
intent.setPackage(getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED);
sendBroadcast(intent);
} }
}; };
private final Handler connectHandler = new Handler() { private final Handler connectHandler = new Handler() {
@ -125,50 +113,52 @@ 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) {
String action = intent == null ? CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE : intent.getAction(); Intent newIntent = intent == null ? new Intent(ServiceAction.ACTION_ACTIVATE_PUSH_SERVICE) : intent;
String action = newIntent.getAction();
if (!persistHolder.get()) { if (!persistHolder.get()) {
createNotification(); createNotification();
} }
if (CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action)) { if (ServiceAction.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
this.prepareConnect(intent.getLongExtra(KEY_DELAYED_TIME, 0)); this.prepareConnect(newIntent.getLongExtra(BundleKey.KEY_DELAYED_TIME, 0));
} }
if (CIMPushManager.ACTION_SEND_REQUEST_BODY.equals(action)) { if (ServiceAction.ACTION_SEND_REQUEST_BODY.equals(action)) {
connectorManager.send((SentBody) intent.getSerializableExtra(KEY_SEND_BODY)); connectorManager.send((SentBody) newIntent.getSerializableExtra(BundleKey.KEY_SEND_BODY));
} }
if (CIMPushManager.ACTION_CLOSE_CIM_CONNECTION.equals(action)) { if (ServiceAction.ACTION_CLOSE_CIM_CONNECTION.equals(action)) {
connectorManager.close(); connectorManager.close();
} }
if (CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE.equals(action)) { if (ServiceAction.ACTION_ACTIVATE_PUSH_SERVICE.equals(action)) {
handleKeepAlive(); handleKeepAlive();
} }
if (CIMPushManager.ACTION_DESTROY_CIM_SERVICE.equals(action)) { if (ServiceAction.ACTION_DESTROY_CIM_SERVICE.equals(action)) {
connectorManager.close(); connectorManager.close();
this.stopSelf(); this.stopSelf();
} }
if (CIMPushManager.ACTION_CIM_CONNECTION_PONG.equals(action)) { if (ServiceAction.ACTION_CIM_CONNECTION_PONG.equals(action)) {
connectorManager.send(Pong.getInstance()); connectorManager.send(Pong.getInstance());
} }
if (CIMPushManager.ACTION_SET_LOGGER_EATABLE.equals(action)) { if (ServiceAction.ACTION_SET_LOGGER_EATABLE.equals(action)) {
boolean enable = intent.getBooleanExtra(KEY_LOGGER_ENABLE, true); boolean enable = newIntent.getBooleanExtra(BundleKey.KEY_LOGGER_ENABLE, true);
CIMLogger.getLogger().debugMode(enable); CIMLogger.getLogger().debugMode(enable);
} }
if (CIMPushManager.ACTION_SHOW_PERSIST_NOTIFICATION.equals(action)) { if (ServiceAction.ACTION_SHOW_PERSIST_NOTIFICATION.equals(action)) {
createPersistNotification(intent.getStringExtra(KEY_NOTIFICATION_CHANNEL), createPersistNotification(newIntent.getStringExtra(BundleKey.KEY_NOTIFICATION_CHANNEL),
intent.getStringExtra(KEY_NOTIFICATION_MESSAGE), newIntent.getStringExtra(BundleKey.KEY_NOTIFICATION_MESSAGE),
intent.getIntExtra(KEY_NOTIFICATION_ICON,0)); newIntent.getIntExtra(BundleKey.KEY_NOTIFICATION_ICON,0));
persistHolder.set(true); persistHolder.set(true);
} }
if (CIMPushManager.ACTION_HIDE_PERSIST_NOTIFICATION.equals(action)) { if (ServiceAction.ACTION_HIDE_PERSIST_NOTIFICATION.equals(action)) {
stopForeground(true); stopForeground(true);
persistHolder.set(false); persistHolder.set(false);
} }
@ -177,7 +167,7 @@ public class CIMPushService extends Service {
notificationHandler.sendEmptyMessageDelayed(0, 200); notificationHandler.sendEmptyMessageDelayed(0, 200);
} }
return super.onStartCommand(intent,flags,startId); return Service.START_REDELIVER_INTENT;
} }
private void prepareConnect(long delayMillis) { private void prepareConnect(long delayMillis) {
@ -319,6 +309,8 @@ public class CIMPushService extends Service {
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED); intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
intentFilter.addAction(Intent.ACTION_USER_PRESENT); intentFilter.addAction(Intent.ACTION_USER_PRESENT);
intentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
intentFilter.addAction(IntentAction.ACTION_CONNECTION_RECOVERY);
return intentFilter; return intentFilter;
} }

View File

@ -22,7 +22,7 @@
package com.farsunset.cim.sdk.android.coder; package com.farsunset.cim.sdk.android.coder;
import com.farsunset.cim.sdk.android.constant.CIMConstant; import com.farsunset.cim.sdk.android.constant.ProtobufType;
import com.farsunset.cim.sdk.android.model.Message; import com.farsunset.cim.sdk.android.model.Message;
import com.farsunset.cim.sdk.android.model.Ping; import com.farsunset.cim.sdk.android.model.Ping;
import com.farsunset.cim.sdk.android.model.ReplyBody; import com.farsunset.cim.sdk.android.model.ReplyBody;
@ -72,11 +72,11 @@ public class ClientMessageDecoder {
/* /*
消息读取完成后通过type来解析成对应的消息体 消息读取完成后通过type来解析成对应的消息体
*/ */
if (CIMConstant.ProtobufType.PING == type) { if (ProtobufType.PING == type) {
return Ping.getInstance(); return Ping.getInstance();
} }
if (CIMConstant.ProtobufType.REPLY_BODY == type) { if (ProtobufType.REPLY_BODY == type) {
ReplyBodyProto.Model bodyProto = ReplyBodyProto.Model.parseFrom(bodyBuffer.array()); ReplyBodyProto.Model bodyProto = ReplyBodyProto.Model.parseFrom(bodyBuffer.array());
ReplyBody body = new ReplyBody(); ReplyBody body = new ReplyBody();
body.setKey(bodyProto.getKey()); body.setKey(bodyProto.getKey());

View File

@ -0,0 +1,38 @@
/*
* Copyright 2013-2019 Xia Jun(3979434@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************************
* *
* Website : http://www.farsunset.com *
* *
***************************************************************************************
*/
package com.farsunset.cim.sdk.android.constant;
public interface BundleKey {
String KEY_SEND_BODY = "KEY_SEND_BODY";
String KEY_DELAYED_TIME = "KEY_DELAYED_TIME";
String KEY_LOGGER_ENABLE = "KEY_LOGGER_ENABLE";
String KEY_NOTIFICATION_MESSAGE = "KEY_NOTIFICATION_MESSAGE";
String KEY_NOTIFICATION_CHANNEL = "KEY_NOTIFICATION_CHANNEL";
String KEY_NOTIFICATION_ICON = "KEY_NOTIFICATION_ICON";
}

View File

@ -30,84 +30,9 @@ public interface CIMConstant {
*/ */
int DATA_HEADER_LENGTH = 3; int DATA_HEADER_LENGTH = 3;
interface ProtobufType {
/*
客户端->服务端 发送的心跳响应
*/
byte PONG = 0;
/*
服务端->客户端 发送的心跳请求
*/
byte PING = 1;
byte MESSAGE = 2;
byte SENT_BODY = 3;
byte REPLY_BODY = 4;
}
interface RequestKey {
String CLIENT_BIND = "client_bind";
String CLIENT_SET_TAG = "client_set_tag";
String CLIENT_REMOVE_TAG = "client_remove_tag";
}
interface MessageAction {
/* /*
被其他设备登录挤下线消息 被其他设备登录挤下线消息
*/ */
String ACTION_999 = "999"; String ACTION_999 = "999";
}
interface IntentAction {
/*
消息广播action
*/
String ACTION_MESSAGE_RECEIVED = "com.farsunset.cim.MESSAGE_RECEIVED";
/*
发送sendBody成功广播
*/
String ACTION_SEND_FINISHED = "com.farsunset.cim.SEND_FINISHED";
/*
链接意外关闭广播
*/
String ACTION_CONNECTION_CLOSED = "com.farsunset.cim.CONNECTION_CLOSED";
/*
链接失败广播
*/
String ACTION_CONNECT_FAILED = "com.farsunset.cim.CONNECT_FAILED";
/*
链接成功广播
*/
String ACTION_CONNECT_FINISHED = "com.farsunset.cim.CONNECT_FINISHED";
/*
发送sendBody成功后获得replayBody回应广播
*/
String ACTION_REPLY_RECEIVED = "com.farsunset.cim.REPLY_RECEIVED";
/*
网络变化广播
*/
String ACTION_NETWORK_CHANGED = "com.farsunset.cim.NETWORK_CHANGED";
/*
重试连接
*/
String ACTION_CONNECTION_RECOVERY = "com.farsunset.cim.CONNECTION_RECOVERY";
}
} }

View File

@ -0,0 +1,66 @@
/*
* Copyright 2013-2019 Xia Jun(3979434@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************************
* *
* Website : http://www.farsunset.com *
* *
***************************************************************************************
*/
package com.farsunset.cim.sdk.android.constant;
public interface IntentAction {
/*
消息广播action
*/
String ACTION_MESSAGE_RECEIVED = "com.farsunset.cim.MESSAGE_RECEIVED";
/*
发送sendBody成功广播
*/
String ACTION_SEND_FINISHED = "com.farsunset.cim.SEND_FINISHED";
/*
链接意外关闭广播
*/
String ACTION_CONNECTION_CLOSED = "com.farsunset.cim.CONNECTION_CLOSED";
/*
链接失败广播
*/
String ACTION_CONNECT_FAILED = "com.farsunset.cim.CONNECT_FAILED";
/*
链接成功广播
*/
String ACTION_CONNECT_FINISHED = "com.farsunset.cim.CONNECT_FINISHED";
/*
发送sendBody成功后获得replayBody回应广播
*/
String ACTION_REPLY_RECEIVED = "com.farsunset.cim.REPLY_RECEIVED";
/*
网络变化广播
*/
String ACTION_NETWORK_CHANGED = "com.farsunset.cim.NETWORK_CHANGED";
/*
重试连接
*/
String ACTION_CONNECTION_RECOVERY = "com.farsunset.cim.CONNECTION_RECOVERY";
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 2013-2019 Xia Jun(3979434@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************************
* *
* Website : http://www.farsunset.com *
* *
***************************************************************************************
*/
package com.farsunset.cim.sdk.android.constant;
public interface ProtobufType {
/*
客户端->服务端 发送的心跳响应
*/
byte PONG = 0;
/*
服务端->客户端 发送的心跳请求
*/
byte PING = 1;
byte MESSAGE = 2;
byte SENT_BODY = 3;
byte REPLY_BODY = 4;
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2013-2019 Xia Jun(3979434@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************************
* *
* Website : http://www.farsunset.com *
* *
***************************************************************************************
*/
package com.farsunset.cim.sdk.android.constant;
public interface RequestKey {
String CLIENT_BIND = "client_bind";
String CLIENT_SET_TAG = "client_set_tag";
String CLIENT_REMOVE_TAG = "client_remove_tag";
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2013-2019 Xia Jun(3979434@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************************
* *
* Website : http://www.farsunset.com *
* *
***************************************************************************************
*/
package com.farsunset.cim.sdk.android.constant;
public interface ServiceAction {
String ACTION_CREATE_CIM_CONNECTION = "ACTION_CREATE_CIM_CONNECTION";
String ACTION_DESTROY_CIM_SERVICE = "ACTION_DESTROY_CIM_SERVICE";
String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE";
String ACTION_SEND_REQUEST_BODY = "ACTION_SEND_REQUEST_BODY";
String ACTION_CLOSE_CIM_CONNECTION = "ACTION_CLOSE_CIM_CONNECTION";
String ACTION_SET_LOGGER_EATABLE = "ACTION_SET_LOGGER_EATABLE";
String ACTION_SHOW_PERSIST_NOTIFICATION = "ACTION_SHOW_PERSIST_NOTIFICATION";
String ACTION_HIDE_PERSIST_NOTIFICATION = "ACTION_HIDE_PERSIST_NOTIFICATION";
String ACTION_CIM_CONNECTION_PONG = "ACTION_CIM_CONNECTION_PONG";
}

View File

@ -21,7 +21,7 @@
*/ */
package com.farsunset.cim.sdk.android.model; package com.farsunset.cim.sdk.android.model;
import com.farsunset.cim.sdk.android.constant.CIMConstant; import com.farsunset.cim.sdk.android.constant.ProtobufType;
import java.io.Serializable; import java.io.Serializable;
@ -56,7 +56,7 @@ public class Pong implements Serializable, BinaryBody {
@Override @Override
public byte getType() { public byte getType() {
return CIMConstant.ProtobufType.PONG; return ProtobufType.PONG;
} }
} }

View File

@ -21,7 +21,7 @@
*/ */
package com.farsunset.cim.sdk.android.model; package com.farsunset.cim.sdk.android.model;
import com.farsunset.cim.sdk.android.constant.CIMConstant; import com.farsunset.cim.sdk.android.constant.ProtobufType;
import com.farsunset.cim.sdk.android.model.proto.SentBodyProto; import com.farsunset.cim.sdk.android.model.proto.SentBodyProto;
import java.io.Serializable; import java.io.Serializable;
@ -108,7 +108,7 @@ public class SentBody implements Serializable, BinaryBody {
@Override @Override
public byte getType() { public byte getType() {
return CIMConstant.ProtobufType.SENT_BODY; return ProtobufType.SENT_BODY;
} }
} }

View File

@ -34,11 +34,11 @@ android {
} }
dependencies { dependencies {
implementation "com.farsunset:cim-android-sdk:4.2.0" implementation "com.farsunset:cim-android-sdk:4.2.5"
implementation 'androidx.appcompat:appcompat:1.5.0' implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.6.1' implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.annotation:annotation:1.3.0' implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.protobuf:protobuf-lite:3.0.1' implementation 'com.google.protobuf:protobuf-lite:3.0.1'
implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

View File

@ -19,6 +19,7 @@ import com.farsunset.cim.sdk.android.CIMEventListener;
import com.farsunset.cim.sdk.android.CIMListenerManager; import com.farsunset.cim.sdk.android.CIMListenerManager;
import com.farsunset.cim.sdk.android.CIMPushManager; import com.farsunset.cim.sdk.android.CIMPushManager;
import com.farsunset.cim.sdk.android.constant.CIMConstant; import com.farsunset.cim.sdk.android.constant.CIMConstant;
import com.farsunset.cim.sdk.android.constant.RequestKey;
import com.farsunset.cim.sdk.android.model.Message; import com.farsunset.cim.sdk.android.model.Message;
import com.farsunset.cim.sdk.android.model.ReplyBody; import com.farsunset.cim.sdk.android.model.ReplyBody;
import com.farsunset.cim.sdk.android.model.SentBody; import com.farsunset.cim.sdk.android.model.SentBody;
@ -96,7 +97,7 @@ public class LoginActivity extends AppCompatActivity implements CIMEventListener
/* /*
*第三步 用户id绑定成功可以接收消息了 *第三步 用户id绑定成功可以接收消息了
*/ */
if (replyBody.getKey().equals(CIMConstant.RequestKey.CLIENT_BIND)) { if (replyBody.getKey().equals(RequestKey.CLIENT_BIND)) {
ballsView.runaway(); ballsView.runaway();
Intent intent = new Intent(this,MessageActivity.class); Intent intent = new Intent(this,MessageActivity.class);
intent.putExtra("uid",uidEdit.getText().toString().trim()); intent.putExtra("uid",uidEdit.getText().toString().trim());