添加android sdk 常驻通知栏 接口

This commit is contained in:
远方夕阳 2020-07-30 23:49:43 +08:00
parent b4e206ff92
commit 2cfb492b4b
8 changed files with 161 additions and 9 deletions

9
cim-client-sdk/.idea/cim-client-sdk.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
cim-client-sdk/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

8
cim-client-sdk/.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/cim-client-sdk.iml" filepath="$PROJECT_DIR$/.idea/cim-client-sdk.iml" />
</modules>
</component>
</project>

6
cim-client-sdk/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

44
cim-client-sdk/.idea/workspace.xml generated Normal file
View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="04eddcc3-7421-4308-9205-110aa7d3651c" name="Default Changelist" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component>
<component name="ProjectId" id="1eWPY2i4aHwLXLXLL2yHHjU4bvH" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="editor.preferences.fonts.default" />
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="04eddcc3-7421-4308-9205-110aa7d3651c" name="Default Changelist" comment="" />
<created>1594434419483</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1594434419483</updated>
<workItem from="1594434420828" duration="56000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="2" />
</component>
</project>

View File

@ -52,6 +52,10 @@ public class CIMPushManager {
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 KEY_SEND_BODY = "KEY_SEND_BODY";
/**
@ -87,6 +91,20 @@ public class CIMPushManager {
startService(context, serviceIntent);
}
public static void startForeground(Context context,int icon, String channel , String message) {
Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.putExtra(CIMPushService.KEY_NOTIFICATION_MESSAGE, message);
serviceIntent.putExtra(CIMPushService.KEY_NOTIFICATION_CHANNEL, channel);
serviceIntent.putExtra(CIMPushService.KEY_NOTIFICATION_ICON, icon);
serviceIntent.setAction(ACTION_SHOW_PERSIST_NOTIFICATION);
startService(context, serviceIntent);
}
public static void cancelForeground(Context context) {
Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ACTION_HIDE_PERSIST_NOTIFICATION);
startService(context, serviceIntent);
}
/**
* 设置一个账号登录到服务端

View File

@ -36,6 +36,8 @@ 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 java.util.concurrent.atomic.AtomicBoolean;
/**
* 与服务端连接服务
*
@ -45,13 +47,23 @@ 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";
public final static String KEY_NOTIFICATION_MESSAGE = "KEY_NOTIFICATION_MESSAGE";
public final static String KEY_NOTIFICATION_CHANNEL = "KEY_NOTIFICATION_CHANNEL";
public final static String KEY_NOTIFICATION_ICON = "KEY_NOTIFICATION_ICON";
private final static String TRANSIENT_NTC_CHANNEL_ID = "CIM_PUSH_TRANSIENT_NTC_ID";
private final static String PERSIST_NTC_CHANNEL_ID = "CIM_PUSH_PERSIST_NTC_ID";
private final static int NOTIFICATION_ID = Integer.MAX_VALUE;
private final static int PERSIST_NOTIFICATION_ID = Integer.MIN_VALUE;
private CIMConnectorManager connectorManager;
private KeepAliveBroadcastReceiver keepAliveReceiver;
private ConnectivityManager connectivityManager;
private NotificationManager notificationManager;
private final AtomicBoolean persistHolder = new AtomicBoolean(false);
@Override
public void onCreate() {
@ -101,6 +113,9 @@ public class CIMPushService extends Service {
private final Handler notificationHandler = new Handler() {
@Override
public void handleMessage(android.os.Message message) {
if (persistHolder.get()){
return;
}
stopForeground(true);
}
};
@ -108,10 +123,12 @@ public class CIMPushService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
createNotification();
String action = intent == null ? CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE : intent.getAction();
if (!persistHolder.get()) {
createNotification();
}
if (CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
this.prepareConnect(intent.getLongExtra(KEY_DELAYED_TIME, 0));
}
@ -138,8 +155,20 @@ public class CIMPushService extends Service {
this.stopSelf();
}
if (CIMPushManager.ACTION_SHOW_PERSIST_NOTIFICATION.equals(action)) {
createPersistNotification(intent.getStringExtra(KEY_NOTIFICATION_CHANNEL),
intent.getStringExtra(KEY_NOTIFICATION_MESSAGE),
intent.getIntExtra(KEY_NOTIFICATION_ICON,0));
persistHolder.set(true);
}
if (CIMPushManager.ACTION_HIDE_PERSIST_NOTIFICATION.equals(action)) {
stopForeground(true);
persistHolder.set(false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationHandler.sendEmptyMessageDelayed(0, 1000);
notificationHandler.sendEmptyMessageDelayed(0, 200);
}
return super.onStartCommand(intent,flags,startId);
@ -200,7 +229,9 @@ public class CIMPushService extends Service {
connectHandler.removeMessages(0);
notificationHandler.removeMessages(0);
stopForeground(true);
persistHolder.set(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
unregisterReceiver(keepAliveReceiver);
@ -217,17 +248,15 @@ public class CIMPushService extends Service {
return;
}
String channelId = getClass().getName();
if (notificationManager.getNotificationChannel(channelId) == null) {
NotificationChannel channel = new NotificationChannel(channelId, getClass().getSimpleName(), NotificationManager.IMPORTANCE_LOW);
if (notificationManager.getNotificationChannel(TRANSIENT_NTC_CHANNEL_ID) == null) {
NotificationChannel channel = new NotificationChannel(TRANSIENT_NTC_CHANNEL_ID, 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)
Notification notification = new Notification.Builder(this,TRANSIENT_NTC_CHANNEL_ID)
.setContentTitle(CIMPushService.class.getSimpleName())
.build();
@ -235,6 +264,38 @@ public class CIMPushService extends Service {
}
private void createPersistNotification(String channelName ,String message,int icon) {
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);
channel.enableLights(false);
channel.enableVibration(false);
channel.setSound(null, null);
notificationManager.createNotificationChannel(channel);
}
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setPackage(getPackageName());
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
builder = new Notification.Builder(this,PERSIST_NTC_CHANNEL_ID);
}else {
builder = new Notification.Builder(this);
}
builder.setAutoCancel(false)
.setOngoing(false)
.setSmallIcon(icon)
.setWhen(System.currentTimeMillis())
.setContentIntent(PendingIntent.getActivity(this, 0, intent, 0))
.setContentTitle(channelName)
.setContentText(message);
startForeground(PERSIST_NOTIFICATION_ID, builder.build());
}
private class KeepAliveBroadcastReceiver extends BroadcastReceiver {
@Override