
IM即时通讯
安全可靠、全球互通

实时音视频
流畅稳定、省钱省力
回答:
如果需要支持旧版本自定义消息,开发者需要在 demo 的 dart 层、Android 层、iOS 层分别进行处理。
详细代码可参考 example
## Dart 层处理
1. 首先需要开发者根据自己的自定义消息定义 Dart 层消息内容,代码参考 `example/lib/custom_message/poke_message.dart`
a. 导入头文件
```
import 'package:rongcloud_im_wrapper_plugin/rongcloud_im_wrapper_plugin.dart';
import 'dart:convert' as json_lib show json ;
```
b. 定义消息类
- 开发者的自定义消息类需要继承 `RCIMIWUserCustomMessage`
- 开发者需要定义 fromJson 的构造函数,并调用 suer.fromJson
```
RCIMDPokeMessage.fromJson(Map<String, dynamic> json) : super.fromJson(json);
```
- 开发者定义自己的构造方法, 需要调用父类的 RCIMIWUserCustomMessage(RCIMIWConversationType type, String targetId)
```
RCIMDPokeMessage(RCIMIWConversationType type, String targetId, this.pokeMessage) : super(type, targetId);
```
- 开发者需要实现父类的 decode/encode 方法
```
@override
void decode(String jsonStr) {
Map map = json_lib.json.decode(jsonStr.toString());
// 获取的 key 值要与原生传递的 key 值一样
pokeMessage = map['content'];
}
@override
String encode() {
Map map = {};
// 传递的 key 值要与原生传递的 key 值一样
map['content'] = pokeMessage;
return json_lib.json.encode(map);
}
```
- 开发者需要实现父类的 messageObjectName 来返回 objectName
注意:objectName 值要与原生完全一致
```
@override
String messageObjectName() {
return "ST:PokeMsg";
}
```
- 开发者需要实现父类的 toJson 方法,创建一个 Map 对象,并给设置 'content' 的 value 值为 encode 方法的返回值。
```
final Map<String, dynamic> json = super.toJson();
// 此处 'content' 不可修改
json['content'] = encode();
return json;
```
2. 注册自定义消息
调用引擎的 registerCustomMessage 方法进行注册,注册参考下面代码,只需要修改 objectName 和自定义消息类即可。
```
e.registerCustomMessage('ST:PokeMsg', (json){
RCIMDPokeMessage pokeMsg = RCIMDPokeMessage.fromJson(json);
// 此处 'content' 不可修改
pokeMsg.decode(json['content']);
return pokeMsg;
});
```
## Android 层处理
1. 开发者需要在项目工程中引入融云原生 SDK,且 SDK 版本需要和Flutter SDK中引入的版本号一致。
具体版本号可以在 android/build.gradle 中进行查看
2. 将原生的自定义消息文件放入工程中,并在 `MainActivity` 进行注册,确保注册放在 dart 层 init 之前。
```
import cn.rongcloud.im.wrapper.flutter.RCIMWrapperEngine;
```
```
List<Class<? extends MessageContent>> list = new ArrayList<>();
list.add(PokeMessage.class);
RCIMWrapperEngine.getInstance().messageContentClassList = list;
```
## iOS 层处理
1. 引入头文件
```
#import <rongcloud_im_wrapper_plugin/RCIMWrapperEngine.h>
```
2. 将原生的自定义消息导入到 iOS 工程中,并在 `AppDelegate` 中进行注册,确保注册放在 dart 层 init 之前。
```
NSMutableArray *marr = [NSMutableArray arrayWithObject:[RCDPokeMessage class]];
[RCIMWrapperEngine sharedInstance].messageContentClassList = marr.copy;
```