publish method
Implementation
void publish(String content, String type, Thread currentThread,
ExtraParam? extraParam) {
// if (currentThread == null) {
// debugPrint('连接客服失败,请退出页面重新进入。注意: 请在App启动的时候,调用init接口');
// Fluttertoast.showToast(msg: '连接客服失败,请退出页面重新进入');
// return;
// }
// https://pub.dev/packages/encrypt#aes
// final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
// final encrypted = encrypter.encrypt(content, iv: iv);
// BytedeskUtils.printLog(encrypted.base64);
// final decrypted = encrypter.decrypt(encrypted, iv: iv);
// BytedeskUtils.printLog(decrypted);
// thread
protothread.Thread thread = protothread.Thread();
thread.tid = currentThread.tid!;
thread.type = currentThread.type!;
thread.topic = currentThread.topic!;
thread.nickname = currentThread.nickname!;
thread.avatar = currentThread.avatar!;
thread.client = currentThread.client!;
thread.timestamp = BytedeskUtils.formatedDateNow(); // 没有必要填写,服务器端会填充
thread.unreadCount = 0;
var extra = {'top': false, 'undisturb': false};
thread.extra = jsonEncode(extra);
// user
protouser.User user = protouser.User();
user.uid = SpUtil.getString(BytedeskConstants.uid)!;
user.username = SpUtil.getString(BytedeskConstants.username)!;
user.nickname = SpUtil.getString(BytedeskConstants.nickname)!;
user.avatar = SpUtil.getString(BytedeskConstants.avatar)!;
// msg
protomsg.Message messageProto = protomsg.Message();
messageProto.mid = BytedeskUuid.uuid();
messageProto.type = type;
messageProto.timestamp = BytedeskUtils.formatedDateNow();
messageProto.client = BytedeskUtils.getClient(); //BytedeskConstants.client;
messageProto.version = '1';
messageProto.encrypted = false;
// 用来在发送之前显示到界面
Message message = Message();
message.mid = messageProto.mid;
message.type = messageProto.type;
message.timestamp = messageProto.timestamp;
message.client = messageProto.client;
message.nickname = user.nickname;
message.avatar = user.avatar;
message.topic = thread.topic;
message.status = BytedeskConstants.MESSAGE_STATUS_SENDING;
message.isSend = 1;
message.currentUid = currentUid;
message.answersJson = '';
message.thread = currentThread;
message.user =
User(uid: user.uid, avatar: user.avatar, nickname: user.nickname);
// 判断是否应该插入本地并显示
bool shouldInsertLocal = false;
// 发送protobuf
if (type == BytedeskConstants.MESSAGE_TYPE_TEXT) {
protomsg.Text text = protomsg.Text();
text.content = content;
messageProto.text = text;
//
thread.content = content;
//
shouldInsertLocal = true;
message.content = content;
} else if (type == BytedeskConstants.MESSAGE_TYPE_IMAGE) {
protomsg.Image image = protomsg.Image();
image.imageUrl = content;
messageProto.image = image;
//
thread.content = '[图片]';
//
shouldInsertLocal = true;
message.imageUrl = content;
} else if (type == BytedeskConstants.MESSAGE_TYPE_VOICE) {
protomsg.Voice voice = protomsg.Voice();
voice.voiceUrl = content;
// voice.length
// voice.format
messageProto.voice = voice;
//
thread.content = '[语音]';
//
shouldInsertLocal = true;
message.voiceUrl = content;
} else if (type == BytedeskConstants.MESSAGE_TYPE_FILE) {
protomsg.File file = protomsg.File();
file.fileUrl = content;
messageProto.file = file;
//
thread.content = '[文件]';
//
shouldInsertLocal = true;
message.fileUrl = content;
} else if (type == BytedeskConstants.MESSAGE_TYPE_VIDEO ||
type == BytedeskConstants.MESSAGE_TYPE_SHORT_VIDEO) {
protomsg.Video video = protomsg.Video();
video.videoOrShortUrl = content;
messageProto.video = video;
//
thread.content = '[视频]';
//
shouldInsertLocal = true;
message.videoUrl = content;
} else if (type == BytedeskConstants.MESSAGE_TYPE_COMMODITY) {
protomsg.Text text = protomsg.Text();
text.content = content;
messageProto.text = text;
//
thread.content = '[商品]';
//
shouldInsertLocal = true;
message.content = content;
} else if (type == BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_PREVIEW) {
protomsg.Preview preview = protomsg.Preview();
preview.content = content;
messageProto.preview = preview;
} else if (type == BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_RECEIPT) {
// 发送消息回执
protomsg.Receipt receipt = protomsg.Receipt();
receipt.mid = extraParam!.receiptMid!;
receipt.status = extraParam.receiptStatus!;
//
messageProto.receipt = receipt;
} else if (type == BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_RECALL) {
//
protomsg.Recall recall = protomsg.Recall();
recall.mid = extraParam!.recallMid!;
//
messageProto.recall = recall;
} else if (type == BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_TRANSFER) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_TRANSFER_ACCEPT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_TRANSFER_REJECT) {
//
return;
} else if (type == BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_INVITE) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_INVITE_ACCEPT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_INVITE_REJECT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_INVITE_RATE) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_RATE_RESULT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_INVITE_VIDEO) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_INVITE_AUDIO) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_CANCEL) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_OFFER_VIDEO) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_OFFER_AUDIO) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_ANSWER) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_CANDIDATE) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_ACCEPT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_REJECT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_READY) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_BUSY) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_WEBRTC_CLOSE) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_CREATE) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_UPDATE) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_ANNOUNCEMENT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_INVITE) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_INVITE_ACCEPT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_INVITE_REJECT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_APPLY) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_APPLY_APPROVE) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_APPLY_DENY) {
//
return;
} else if (type == BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_KICK) {
//
return;
} else if (type == BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_MUTE) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_TRANSFER) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_TRANSFER_ACCEPT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_TRANSFER_REJECT) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_WITHDRAW) {
//
return;
} else if (type ==
BytedeskConstants.MESSAGE_TYPE_NOTIFICATION_GROUP_DISMISS) {
//
return;
} else if (type == BytedeskConstants.MESSAGE_TYPE_LOCATION) {
//
return;
} else if (type == BytedeskConstants.MESSAGE_TYPE_LINK) {
//
return;
} else if (type == BytedeskConstants.MESSAGE_TYPE_EVENT) {
//
return;
} else if (type == BytedeskConstants.MESSAGE_TYPE_CUSTOM) {
//
return;
} else if (type == BytedeskConstants.MESSAGE_TYPE_RED_PACKET) {
//
return;
} else {
// 其他类型消息
debugPrint('other type:$type');
}
//
messageProto.user = user;
messageProto.thread = thread;
// 先通知界面本地显示聊天记录,然后再发送
if (shouldInsertLocal) {
// 插入本地数据库
// if (messageProvider != null) {
messageProvider.insert(message);
// }
bytedeskEventBus.fire(ReceiveMessageEventBus(message));
// midList.add(message.mid!);
}
//
final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
// 注意:此函数为自行添加,原先库中没有
builder.addProtobuf(messageProto.writeToBuffer());
mqttClient.publishMessage(
currentThread.topic, MqttQos.exactlyOnce, builder.payload);
// 播放发送消息提示音,放到SDK外实现,迁移到demo中
if (BytedeskKefu.getPlayAudioOnSendMessage()!) {
//
}
}