diff --git a/README.md b/README.md index 4e9cf9f..975acab 100644 --- a/README.md +++ b/README.md @@ -62,14 +62,14 @@ android { - 启动服务 ```kotlin -PushSdk.instance.register(userId: String) +PushSdk.instance.register(userId) ``` - 注册消息监听(二选一) 1. 注册callback,自己处理消息分发 ```kotlin -PushSdk.instance.setMsgCallback(callback: CallbackListener) +PushSdk.instance.setMsgCallback(callback) ``` 2. 在需要监听消息的页面,使用eventbus订阅。 @@ -80,7 +80,7 @@ PushSdk.instance.setMsgCallback(callback: CallbackListener) - 发送消息 ```kotlin -PushSdk.instance.sendTextMessage(toUserId:String, msg: String) +SendMessageHelper.sendTextMessage(toUserId, msg) ``` diff --git a/app/src/main/java/cn/org/bjca/trust/pushdemo/MainActivity.kt b/app/src/main/java/cn/org/bjca/trust/pushdemo/MainActivity.kt index b8ce038..f44f505 100644 --- a/app/src/main/java/cn/org/bjca/trust/pushdemo/MainActivity.kt +++ b/app/src/main/java/cn/org/bjca/trust/pushdemo/MainActivity.kt @@ -4,6 +4,8 @@ import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import cn.org.bjca.trust.push.PushSdk +import cn.org.bjca.trust.push.common.FileHelper +import cn.org.bjca.trust.push.common.SendMessageHelper class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -11,9 +13,10 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) PushSdk.instance.register("xuqinmin") findViewById(R.id.register).setOnClickListener { + FileHelper.openFile() } findViewById(R.id.send).setOnClickListener { - PushSdk.instance.sendTextMessage("xuqinmin", "你好啊${System.currentTimeMillis()}") + SendMessageHelper.sendTextMessage("xuqinmin", "你好啊${System.currentTimeMillis()}") } } diff --git a/gradle.properties b/gradle.properties index b747a56..87026b2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -23,9 +23,10 @@ kotlin.code.style=official # thereby reducing the size of the R class for that library android.nonTransitiveRClass=true systemProp.sonar.projectKey=PushDemo -sonar.sources=src/main +systemProp.sonar.sources=src/main systemProp.sonar.host.url=http://127.0.0.1:9000 systemProp.sonar.login=sqp_ec473b95ed821e1bb108acf9b9909f46a1b037b6 +systemProp.sonar.projectVersion=1.0.0.002 sonar.pdf.password=xuqinmin1022 sonar.pdf.username=xuqinmin12 #sonar.androidLint.reportPaths=./build/slf/lint-results.xml diff --git a/push/src/main/java/cn/org/bjca/trust/push/CrashHandler.kt b/push/src/main/java/cn/org/bjca/trust/push/CrashHandler.kt index a87afb9..158d6f4 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/CrashHandler.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/CrashHandler.kt @@ -1,6 +1,9 @@ package cn.org.bjca.trust.push -import cn.org.bjca.trust.push.common.LogHelper +import cn.org.bjca.trust.push.common.SendMessageHelper +import java.io.PrintWriter +import java.io.StringWriter +import java.io.Writer /** * 日志收集 @@ -23,11 +26,17 @@ object CrashHandler : Thread.UncaughtExceptionHandler { mDefaultHandler?.uncaughtException(t, e) } - private fun handleException(e: Throwable?) { - e?.let { - LogHelper.e(BuildConfig.versionCode.toString()) - LogHelper.e(BuildConfig.versionName) + private fun handleException(ex: Throwable) { + val writer: Writer = StringWriter() + val printWriter = PrintWriter(writer) + ex.printStackTrace(printWriter) + var cause: Throwable? = ex.cause + while (cause != null) { + cause.printStackTrace(printWriter) + cause = cause.cause } + printWriter.close() + SendMessageHelper.sendLogMessage(writer.toString()) } diff --git a/push/src/main/java/cn/org/bjca/trust/push/common/FileHelper.kt b/push/src/main/java/cn/org/bjca/trust/push/common/FileHelper.kt index efa4a5f..bb25a61 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/common/FileHelper.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/common/FileHelper.kt @@ -6,5 +6,6 @@ package cn.org.bjca.trust.push.common object FileHelper { @JvmStatic fun openFile() { + throw Throwable("************************") } } \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/common/MessageHelper.kt b/push/src/main/java/cn/org/bjca/trust/push/common/MessageHelper.kt index 646c57d..44a6891 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/common/MessageHelper.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/common/MessageHelper.kt @@ -3,9 +3,7 @@ package cn.org.bjca.trust.push.common import cn.org.bjca.trust.push.db.DbHelper import cn.org.bjca.trust.push.db.message.SzyxMessage import cn.org.bjca.trust.push.enums.MsgType -import cn.org.bjca.trust.push.message.ImManager -import cn.org.bjca.trust.push.message.bean.TextMessage -import cn.org.bjca.trust.push.message.bean.UnknownMessage +import cn.org.bjca.trust.push.message.bean.* import cn.org.bjca.trust.push.message.msg.Message import cn.org.bjca.trust.push.message.msg.SendMessage import org.json.JSONException @@ -20,6 +18,27 @@ object MessageHelper { MsgType.Text -> { TextMessage::class.java } + MsgType.Voice -> { + VoiceMessage::class.java + } + MsgType.Image -> { + PictureMessage::class.java + } + MsgType.Location -> { + LocationMessage::class.java + } + MsgType.Video -> { + VideoMessage::class.java + } + MsgType.File -> { + FileMessage::class.java + } + MsgType.At -> { + AtMessage::class.java + } + MsgType.Other -> { + OtherMessage::class.java + } else -> { UnknownMessage::class.java } @@ -55,20 +74,6 @@ object MessageHelper { return parseMessage(sendMessage) } - @JvmStatic - fun sendMessage(clientId: String, message: Message) { - LogHelper.d("------app端调用发送消息-----") - val sendMessage = SendMessage() - sendMessage.messageId = message.messageId ?: CommonHelper.generateMessageId() - sendMessage.timestamp = System.currentTimeMillis() - sendMessage.fromClientId = clientId - sendMessage.toClientId = message.toClientId - sendMessage.target = clientId - sendMessage.messageType = message.msgType.value - sendMessage.message = message.createContentJsonStr() - ImManager.instance.sendMessage(sendMessage) - } - fun changeStatus(msgId: String, status: Boolean): Message? { val m = DbHelper.getDataBase().messageDao().getMessage(msgId) if (m.isNotEmpty()) { diff --git a/push/src/main/java/cn/org/bjca/trust/push/common/SendMessageHelper.kt b/push/src/main/java/cn/org/bjca/trust/push/common/SendMessageHelper.kt new file mode 100644 index 0000000..bd1e5d2 --- /dev/null +++ b/push/src/main/java/cn/org/bjca/trust/push/common/SendMessageHelper.kt @@ -0,0 +1,88 @@ +package cn.org.bjca.trust.push.common + +import cn.org.bjca.trust.push.BuildConfig +import cn.org.bjca.trust.push.db.DbHelper +import cn.org.bjca.trust.push.message.ImManager +import cn.org.bjca.trust.push.message.bean.* +import cn.org.bjca.trust.push.message.msg.Message +import cn.org.bjca.trust.push.message.msg.SendMessage + +object SendMessageHelper { + private fun sendMessage(message: Message) { + LogHelper.d("------app端调用发送消息-----") + val sendMessage = SendMessage() + sendMessage.messageId = message.messageId ?: CommonHelper.generateMessageId() + sendMessage.timestamp = System.currentTimeMillis() + sendMessage.toClientId = message.toClientId + sendMessage.messageType = message.msgType.value + sendMessage.message = message.createContentJsonStr() + ImManager.instance.sendMessage(sendMessage) + } + + @JvmStatic + fun sendTextMessage(toUserId: String, msg: String) { + sendMessage(TextMessage().apply { + toClientId = toUserId + text = msg + }) + } + + @JvmStatic + fun sendImageMessage( + toUserId: String, + imageStorageId: String, + thumbnail: String, + thumbnailWidth: Int, + thumbnailHeight: Int + ) { + sendMessage( + PictureMessage( + ).apply { + toClientId = toUserId + this.imageStorageId = imageStorageId + this.thumbnail = thumbnail + this.thumbnailWidth = thumbnailWidth + this.thumbnailHeight = thumbnailHeight + }) + } + + @JvmStatic + fun sendAtMessage( + toUserId: String, text: String, atNos: List + ) { + sendMessage( + AtMessage().apply { + this.text = text + this.atNos = atNos + toClientId = toUserId + }) + } + + @JvmStatic + fun sendOtherMessage( + toUserId: String, msg: String + ) { + sendMessage( + OtherMessage().apply { + toClientId = toUserId + this.msg = msg + }) + } + + @JvmStatic + fun sendLogMessage( + msg: String, + ) { + val devices = DbHelper.getDataBase().deviceDao().getAll() + if (devices.isNotEmpty()) { + sendMessage( + LogMessage().apply { + text = msg + versionName = BuildConfig.versionName + versionCode = BuildConfig.versionCode + device = devices[0] + } + ) + } + } +} \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/constant/Constants.kt b/push/src/main/java/cn/org/bjca/trust/push/constant/Constants.kt index c03577b..cbc1d94 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/constant/Constants.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/constant/Constants.kt @@ -6,6 +6,7 @@ const val ERROR_DEVICE = "10001" const val ERROR_MSG_SEND_1 = "10011" const val ERROR_MSG_SEND_2 = "10012" const val ERROR_MSG_SEND_3 = "10013" +const val ERROR_MSG_SEND_4 = "10014" const val ERROR_CONNECTED = "10021" const val SUCCESS_CONNECTED = "20001" diff --git a/push/src/main/java/cn/org/bjca/trust/push/enums/MsgType.kt b/push/src/main/java/cn/org/bjca/trust/push/enums/MsgType.kt index 3fcfc20..b080cf2 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/enums/MsgType.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/enums/MsgType.kt @@ -1,8 +1,36 @@ package cn.org.bjca.trust.push.enums enum class MsgType(val value: Int) { - UNKNOWN(-1), - Text(1); + UNKNOWN(-1) /* 未知 */, + Text(1) /* 文本 */, + Voice(2) /* 语音 */, + Image(3) /* 图片 */, + Location(4) /* 位置 */, + Video(5) /* 视频 */, + File(6) /* 文件 */, + At(7) /* @我 */, + Log(8) /* 日志 */, + Other(10010), /* 自定义消息 */ + /** + * pc 登陆 end + */ + /** + * 音频/视频消息 start + */ + AVMeetingInvite(250),/*邀请通知*/ + AVMeetingRefuse(251),/*拒绝通知*/ + AVMeetingCancel(252),/*取消通知*/ + AVMeetingTimeout(253),/*超时通知*/ + AVMeetingBusy(254),/*占线通知*/ + AVMeetingSwitch(255),/*切换音频通知*/ + AVMeetingConnected(256),/* 聊天已连接通知*/ + MulAVMeetingInvite(260), /*群视频邀请*/ + MulAVMeetingRefuse(261), /*群成员拒绝通知*/ + MulAVMeetingCancel(262),/*取消通知*/ + MulAVMeetingTimeOut(263),/*超时通知 */ + + + Notify(999); companion object { fun getMsgType(value: Int): MsgType { diff --git a/push/src/main/java/cn/org/bjca/trust/push/kit/ImClientInterface.kt b/push/src/main/java/cn/org/bjca/trust/push/kit/ImClientInterface.kt index ad4371f..e18fc63 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/kit/ImClientInterface.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/kit/ImClientInterface.kt @@ -11,4 +11,5 @@ interface ImClientInterface { fun isConnect(): Boolean fun isConnecting(): Boolean fun sendMessage(msg: SendMessage) + fun imConnectOptions(): ImConnectOptions } \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/kit/SdkInterface.kt b/push/src/main/java/cn/org/bjca/trust/push/kit/SdkInterface.kt index 01f019e..6be2aea 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/kit/SdkInterface.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/kit/SdkInterface.kt @@ -14,5 +14,4 @@ interface SdkInterface { * 消息相关 */ fun setMsgCallback(callback: CallbackListener) - fun sendTextMessage(toUserId:String, msg: String) } \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/manager/PushSdkManager.kt b/push/src/main/java/cn/org/bjca/trust/push/manager/PushSdkManager.kt index b1bf1b5..71a50df 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/manager/PushSdkManager.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/manager/PushSdkManager.kt @@ -3,12 +3,10 @@ package cn.org.bjca.trust.push.manager import android.content.Context import cn.org.bjca.trust.push.BuildConfig import cn.org.bjca.trust.push.common.DeviceHelper -import cn.org.bjca.trust.push.common.MessageHelper import cn.org.bjca.trust.push.enums.OsType import cn.org.bjca.trust.push.kit.SdkInterface import cn.org.bjca.trust.push.message.ImCallback import cn.org.bjca.trust.push.message.ImManager -import cn.org.bjca.trust.push.message.bean.TextMessage import cn.org.bjca.trust.push.message.callback.CallbackListener import cn.org.bjca.trust.push.message.client.ImConnectOptions @@ -59,13 +57,4 @@ class PushSdkManager : SdkInterface { override fun setMsgCallback(callback: CallbackListener) { imConnectOptions.callback = callback } - - override fun sendTextMessage(toUserId: String, msg: String) { - MessageHelper.sendMessage(imConnectOptions.clientId, - TextMessage().apply { - toClientId = toUserId - text = msg - }) - } - } \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/AtMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/AtMessage.kt new file mode 100644 index 0000000..20294fa --- /dev/null +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/AtMessage.kt @@ -0,0 +1,50 @@ +package cn.org.bjca.trust.push.message.bean + +import cn.org.bjca.trust.push.common.LogHelper +import cn.org.bjca.trust.push.enums.MsgType +import cn.org.bjca.trust.push.message.msg.Message +import cn.org.bjca.trust.push.message.msg.SendMessage +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +class AtMessage : Message() { + var text: String? = null + var atNos: List? = null + + init { + this.msgType = MsgType.At + } + + override fun parse(type: MsgType, sendMessage: SendMessage) { + super.parse(type, sendMessage) + try { + val json = sendMessage.message?.let { JSONObject(it) } + if (json != null) { + this.text = json.optString("text") + val arr = json.optJSONArray("atNos") + val atNos = arrayListOf() + if (arr != null) { + for (index in 0 until arr.length()) { + atNos.add(arr.optString(index)) + } + } + this.atNos = atNos + } + } catch (e: Exception) { + LogHelper.e("消息组装失败", e) + } + } + + override fun createContentJsonStr(): String { + val json = JSONObject() + try { + json.put("text", this.text) + val jsonArray = JSONArray(this.atNos) + json.put("atNos", jsonArray) + } catch (e: JSONException) { + LogHelper.e("消息组装失败", e) + } + return json.toString() + } +} \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/FileMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/FileMessage.kt new file mode 100644 index 0000000..14866a9 --- /dev/null +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/FileMessage.kt @@ -0,0 +1,45 @@ +package cn.org.bjca.trust.push.message.bean + +import cn.org.bjca.trust.push.common.LogHelper +import cn.org.bjca.trust.push.enums.MsgType +import cn.org.bjca.trust.push.message.msg.Message +import cn.org.bjca.trust.push.message.msg.SendMessage +import org.json.JSONException +import org.json.JSONObject + +class FileMessage : Message() { + var fileStorageId: String? = null + var fileName: String? = null + var fileSize: Long? = null + + init { + this.msgType = MsgType.File + } + + override fun parse(type: MsgType, sendMessage: SendMessage) { + super.parse(type, sendMessage) + try { + val json = sendMessage.message?.let { JSONObject(it) } + if (json != null) { + this.fileStorageId = json.optString("fileStorageId") + this.fileName = json.optString("fileName") + this.fileSize = json.optLong("fileSize") + } + } catch (e: Exception) { + LogHelper.e("消息组装失败", e) + } + } + + override fun createContentJsonStr(): String { + + val json = JSONObject() + try { + json.put("fileStorageId", this.fileStorageId) + json.put("fileName", this.fileName) + json.put("fileSize", this.fileSize) + } catch (e: JSONException) { + LogHelper.e("消息组装失败", e) + } + return json.toString() + } +} \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/LocationMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/LocationMessage.kt new file mode 100644 index 0000000..8021450 --- /dev/null +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/LocationMessage.kt @@ -0,0 +1,54 @@ +package cn.org.bjca.trust.push.message.bean + +import cn.org.bjca.trust.push.common.LogHelper +import cn.org.bjca.trust.push.enums.MsgType +import cn.org.bjca.trust.push.message.msg.Message +import cn.org.bjca.trust.push.message.msg.SendMessage +import org.json.JSONException +import org.json.JSONObject + +class LocationMessage : Message() { + var address: String? = null + var title: String? = null + var thumbnail: String? = null + var longitude: Double? = null + var latitude: Double? = null + var scale: Int? = null + + init { + this.msgType = MsgType.Location + } + + override fun parse(type: MsgType, sendMessage: SendMessage) { + super.parse(type, sendMessage) + try { + val json = sendMessage.message?.let { JSONObject(it) } + if (json != null) { + this.address = json.optString("imageStorageId") + this.title = json.optString("imageStorageId") + this.thumbnail = json.optString("imageStorageId") + this.longitude = json.optDouble("imageStorageId") + this.latitude = json.optDouble("imageStorageId") + this.scale = json.optInt("imageStorageId") + } + } catch (e: Exception) { + LogHelper.e("消息组装失败", e) + } + } + + override fun createContentJsonStr(): String { + + val json = JSONObject() + try { + json.put("address", this.address) + json.put("title", this.title) + json.put("thumbnail", this.thumbnail) + json.put("longitude", this.longitude) + json.put("latitude", this.latitude) + json.put("scale", this.scale) + } catch (e: JSONException) { + LogHelper.e("消息组装失败", e) + } + return json.toString() + } +} \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/LogMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/LogMessage.kt new file mode 100644 index 0000000..47cf242 --- /dev/null +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/LogMessage.kt @@ -0,0 +1,52 @@ +package cn.org.bjca.trust.push.message.bean + +import cn.org.bjca.trust.push.common.LogHelper +import cn.org.bjca.trust.push.common.json.GsonImplHelp +import cn.org.bjca.trust.push.db.device.Device +import cn.org.bjca.trust.push.enums.MsgType +import cn.org.bjca.trust.push.message.msg.Message +import cn.org.bjca.trust.push.message.msg.SendMessage +import org.json.JSONException +import org.json.JSONObject + +class LogMessage() : Message() { + var text: String? = null + var versionName: String? = null + var versionCode: Int? = null + var device: Device? = null + + init { + this.msgType = MsgType.Log + } + + override fun parse(type: MsgType, sendMessage: SendMessage) { + super.parse(type, sendMessage) + try { + val json = sendMessage.message?.let { JSONObject(it) } + if (json != null) { + this.text = json.optString("text") + this.versionName = json.optString("versionName") + this.versionCode = json.optInt("versionCode") + this.device = json.optString("device")?.let { + GsonImplHelp.get().toObject(it, Device::class.java) + } + } + } catch (e: Exception) { + LogHelper.e("消息组装失败", e) + } + } + + override fun createContentJsonStr(): String { + + val json = JSONObject() + try { + json.put("text", this.text) + json.put("versionName", this.versionName) + json.put("versionCode", this.versionCode) + json.put("device", GsonImplHelp.get().toJson(this.device)) + } catch (e: JSONException) { + LogHelper.e("消息组装失败", e) + } + return json.toString() + } +} \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/OtherMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/OtherMessage.kt new file mode 100644 index 0000000..22ca247 --- /dev/null +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/OtherMessage.kt @@ -0,0 +1,29 @@ +package cn.org.bjca.trust.push.message.bean + +import cn.org.bjca.trust.push.common.LogHelper +import cn.org.bjca.trust.push.enums.MsgType +import cn.org.bjca.trust.push.message.msg.Message +import cn.org.bjca.trust.push.message.msg.SendMessage + +class OtherMessage : Message() { + var msg: String? = null + + init { + this.msgType = MsgType.Other + } + + override fun parse(type: MsgType, sendMessage: SendMessage) { + super.parse(type, sendMessage) + try { + sendMessage.message?.let { + this.msg = it + } + } catch (e: Exception) { + LogHelper.e("消息组装失败", e) + } + } + + override fun createContentJsonStr(): String { + return this.msg ?: "" + } +} \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/PictureMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/PictureMessage.kt new file mode 100644 index 0000000..4adb98f --- /dev/null +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/PictureMessage.kt @@ -0,0 +1,48 @@ +package cn.org.bjca.trust.push.message.bean + +import cn.org.bjca.trust.push.common.LogHelper +import cn.org.bjca.trust.push.enums.MsgType +import cn.org.bjca.trust.push.message.msg.Message +import cn.org.bjca.trust.push.message.msg.SendMessage +import org.json.JSONException +import org.json.JSONObject + +class PictureMessage : Message() { + var imageStorageId: String? = null + var thumbnail: String? = null + var thumbnailWidth: Int? = null + var thumbnailHeight: Int? = null + + init { + this.msgType = MsgType.Image + } + + override fun parse(type: MsgType, sendMessage: SendMessage) { + super.parse(type, sendMessage) + try { + val json = sendMessage.message?.let { JSONObject(it) } + if (json != null) { + this.imageStorageId = json.optString("imageStorageId") + this.thumbnail = json.optString("imageStorageId") + this.thumbnailWidth = json.optInt("imageStorageId") + this.thumbnailHeight = json.optInt("imageStorageId") + } + } catch (e: Exception) { + LogHelper.e("消息组装失败", e) + } + } + + override fun createContentJsonStr(): String { + + val json = JSONObject() + try { + json.put("imageStorageId", this.imageStorageId) + json.put("thumbnail", this.thumbnail) + json.put("thumbnailWidth", this.thumbnailWidth) + json.put("thumbnailHeight", this.thumbnailHeight) + } catch (e: JSONException) { + LogHelper.e("消息组装失败", e) + } + return json.toString() + } +} \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/TextMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/TextMessage.kt index 266db16..5eb3dda 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/message/bean/TextMessage.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/TextMessage.kt @@ -19,7 +19,7 @@ class TextMessage : Message() { try { val json = sendMessage.message?.let { JSONObject(it) } if (json != null) { - this.text = json.getString("text") + this.text = json.optString("text") } } catch (e: Exception) { LogHelper.e("消息组装失败", e) diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/UnknownMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/UnknownMessage.kt index 92a6efe..308ef6b 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/message/bean/UnknownMessage.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/UnknownMessage.kt @@ -1,14 +1,11 @@ package cn.org.bjca.trust.push.message.bean -import cn.org.bjca.trust.push.common.LogHelper import cn.org.bjca.trust.push.enums.MsgType import cn.org.bjca.trust.push.message.msg.Message import cn.org.bjca.trust.push.message.msg.SendMessage -import org.json.JSONException -import org.json.JSONObject class UnknownMessage : Message() { - private var unkonown: String? = null + var unkonown: String? = null init { this.msgType = MsgType.UNKNOWN @@ -16,17 +13,10 @@ class UnknownMessage : Message() { override fun parse(type: MsgType, sendMessage: SendMessage) { super.parse(type, sendMessage) - this.unkonown = sendMessage.message + sendMessage.message?.let { this.unkonown = it } } override fun createContentJsonStr(): String { - - val json = JSONObject() - try { - json.put("unkonown", this.unkonown) - } catch (e: JSONException) { - LogHelper.e("消息组装失败", e) - } - return json.toString() + return unkonown ?: "" } } \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/VideoMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/VideoMessage.kt new file mode 100644 index 0000000..0135c8c --- /dev/null +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/VideoMessage.kt @@ -0,0 +1,57 @@ +package cn.org.bjca.trust.push.message.bean + +import cn.org.bjca.trust.push.common.LogHelper +import cn.org.bjca.trust.push.enums.MsgType +import cn.org.bjca.trust.push.message.msg.Message +import cn.org.bjca.trust.push.message.msg.SendMessage +import org.json.JSONException +import org.json.JSONObject + +class VideoMessage : Message() { + var videoStorageId: String? = null + var fileName: String? = null + var fileSize: Long? = null + var duration: Long? = null + var firstFrame: String? = null + var firstFrameWidth: Int? = null + var firstFrameHeight: Int? = null + + init { + this.msgType = MsgType.Video + } + + override fun parse(type: MsgType, sendMessage: SendMessage) { + super.parse(type, sendMessage) + try { + val json = sendMessage.message?.let { JSONObject(it) } + if (json != null) { + this.videoStorageId = json.optString("videoStorageId") + this.fileName = json.optString("fileName") + this.firstFrame = json.optString("firstFrame") + this.fileSize = json.optLong("fileSize") + this.duration = json.optLong("duration") + this.firstFrameWidth = json.optInt("firstFrameWidth") + this.firstFrameHeight = json.optInt("firstFrameHeight") + } + } catch (e: Exception) { + LogHelper.e("消息组装失败", e) + } + } + + override fun createContentJsonStr(): String { + + val json = JSONObject() + try { + json.put("videoStorageId", this.videoStorageId) + json.put("fileName", this.fileName) + json.put("firstFrame", this.firstFrame) + json.put("fileSize", this.fileSize) + json.put("duration", this.duration) + json.put("firstFrameWidth", this.firstFrameWidth) + json.put("firstFrameHeight", this.firstFrameHeight) + } catch (e: JSONException) { + LogHelper.e("消息组装失败", e) + } + return json.toString() + } +} \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/bean/VoiceMessage.kt b/push/src/main/java/cn/org/bjca/trust/push/message/bean/VoiceMessage.kt new file mode 100644 index 0000000..4cda77c --- /dev/null +++ b/push/src/main/java/cn/org/bjca/trust/push/message/bean/VoiceMessage.kt @@ -0,0 +1,42 @@ +package cn.org.bjca.trust.push.message.bean + +import cn.org.bjca.trust.push.common.LogHelper +import cn.org.bjca.trust.push.enums.MsgType +import cn.org.bjca.trust.push.message.msg.Message +import cn.org.bjca.trust.push.message.msg.SendMessage +import org.json.JSONException +import org.json.JSONObject + +class VoiceMessage : Message() { + var voiceStorageId: String? = null + var duration: Long? = null + + init { + this.msgType = MsgType.Voice + } + + override fun parse(type: MsgType, sendMessage: SendMessage) { + super.parse(type, sendMessage) + try { + val json = sendMessage.message?.let { JSONObject(it) } + if (json != null) { + this.voiceStorageId = json.optString("voiceStorageId") + this.duration = json.optLong("duration") + } + } catch (e: Exception) { + LogHelper.e("消息组装失败", e) + } + } + + override fun createContentJsonStr(): String { + + val json = JSONObject() + try { + json.put("voiceStorageId", this.voiceStorageId) + json.put("duration", this.duration) + } catch (e: JSONException) { + LogHelper.e("消息组装失败", e) + } + return json.toString() + } +} \ No newline at end of file diff --git a/push/src/main/java/cn/org/bjca/trust/push/message/client/ImClient.kt b/push/src/main/java/cn/org/bjca/trust/push/message/client/ImClient.kt index 00b21a5..08f7f9d 100644 --- a/push/src/main/java/cn/org/bjca/trust/push/message/client/ImClient.kt +++ b/push/src/main/java/cn/org/bjca/trust/push/message/client/ImClient.kt @@ -8,10 +8,7 @@ import cn.org.bjca.trust.push.common.DeviceHelper import cn.org.bjca.trust.push.common.LogHelper import cn.org.bjca.trust.push.common.MessageHelper import cn.org.bjca.trust.push.common.json.GsonImplHelp -import cn.org.bjca.trust.push.constant.ERROR_CONNECTED -import cn.org.bjca.trust.push.constant.ERROR_MSG_SEND_1 -import cn.org.bjca.trust.push.constant.ERROR_MSG_SEND_2 -import cn.org.bjca.trust.push.constant.ERROR_MSG_SEND_3 +import cn.org.bjca.trust.push.constant.* import cn.org.bjca.trust.push.db.DbHelper import cn.org.bjca.trust.push.db.message.SzyxMessage import cn.org.bjca.trust.push.enums.OsType @@ -30,6 +27,9 @@ class ImClient : ImClientInterface { private lateinit var options: MqttConnectOptions private var isConnecting = false private var startConnect = false + + override fun imConnectOptions(): ImConnectOptions = connectOptions + override fun connect(mImConnectOptions: ImConnectOptions) { startConnect = true connectOptions = mImConnectOptions @@ -194,6 +194,17 @@ class ImClient : ImClientInterface { } override fun sendMessage(msg: SendMessage) { + if (!::connectOptions.isInitialized) { + MessageHelper.parseMessage(msg)?.let { + connectOptions.callback?.sendError( + ERROR_MSG_SEND_4, it + ) + } + return + } + msg.fromClientId = connectOptions.clientId + msg.target = connectOptions.clientId + DbHelper.getDataBase().messageDao().insertAll( SzyxMessage( msg.messageId, diff --git a/错误码对照表.md b/错误码对照表.md index 4c12d28..76818a9 100644 --- a/错误码对照表.md +++ b/错误码对照表.md @@ -1,16 +1,16 @@ -| 序号 | 错误码 | 说明 | -| ---- | ------ | ---------------------------- | -| 1 | 10001 | 获取设备信息失败 | -| 2 | | | -| 3 | 10011 | 服务端应答超时,发送信息失败 | -| 4 | 10012 | IM未连接,发送消息失败 | -| 5 | 10013 | im客户端已连接,发送消息失败 | -| 6 | | | -| 7 | 20001 | im连接成功 | -| 8 | | | -| 9 | 10021 | im连接失败 | -| 10 | | | -| 11 | | | -| 12 | | | -| 13 | | | +| 序号 | 错误码 | 说明 | +| ---- |-------|-----------------| +| 1 | 10001 | 获取设备信息失败 | +| 2 | | | +| 3 | 10011 | 服务端应答超时,发送信息失败 | +| 4 | 10012 | IM未连接,发送消息失败 | +| 5 | 10013 | im客户端已连接,发送消息失败 | +| 6 | 10014 | sdk未初始化 | +| 7 | 20001 | im连接成功 | +| 8 | | | +| 9 | 10021 | im连接失败 | +| 10 | | | +| 11 | | | +| 12 | | | +| 13 | | |