diff --git a/cim-client-sdk/.idea/cim-client-sdk.iml b/cim-client-sdk/.idea/cim-client-sdk.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/cim-client-sdk/.idea/cim-client-sdk.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/cim-client-sdk/.idea/misc.xml b/cim-client-sdk/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/cim-client-sdk/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/cim-client-sdk/.idea/modules.xml b/cim-client-sdk/.idea/modules.xml new file mode 100644 index 0000000..e563934 --- /dev/null +++ b/cim-client-sdk/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/cim-client-sdk/.idea/vcs.xml b/cim-client-sdk/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/cim-client-sdk/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/cim-client-sdk/.idea/workspace.xml b/cim-client-sdk/.idea/workspace.xml new file mode 100644 index 0000000..e4dba17 --- /dev/null +++ b/cim-client-sdk/.idea/workspace.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + 1594434419483 + + + + + + \ No newline at end of file diff --git a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushManager.java b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushManager.java index 7d80369..2efc722 100644 --- a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushManager.java +++ b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushManager.java @@ -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); + } /** * 设置一个账号登录到服务端 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 2e2054e..0a71f36 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 @@ -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 diff --git a/cim-use-examples/cim-client-android/app/libs/cim-android-sdk-3.8.0.jar b/cim-use-examples/cim-client-android/app/libs/cim-android-sdk-3.8.0.jar index 91f1eac..216bd86 100644 Binary files a/cim-use-examples/cim-client-android/app/libs/cim-android-sdk-3.8.0.jar and b/cim-use-examples/cim-client-android/app/libs/cim-android-sdk-3.8.0.jar differ