flutter_nim 0.1.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 72

flutter_nim #

用于Flutter的网易云信SDK

目前功能 #

  • 登录
  • 自动登录
  • 退出登录
  • 获取最近会话列表
  • 删除一个最近会话列表
  • 开启新会话
  • 关闭打开的会话
  • 获取会话消息
  • 发送接收文本、图片、视频、音频消息
  • 发送接收自定义消息
  • 标记音频已读
  • 调用NIMSDK能力实现语音消息的录制、发送

部分示例 #

初始化 #

使用前,先进行初始化:

void main() async {
  FlutterNIM().init(
    appKey: "123456",
    apnsCername: "ABCDEFG", // iOS 生产环境证书名称
    apnsCernameDevelop: "ABCDEFG", // iOS 测试环境证书名称
    imAccount: "123456",
    imToken: "123456",
  );

  runApp(MyApp());
}

由于 SDK 限制,Android 平台依然需要去 Application 中初始化,可以参照以下代码:

public class MyApplication extends FlutterApplication {
    @Override
    public void onCreate() {
        super.onCreate();

        FlutterNIMPreferences.setContext(this);
        // SDK初始化(启动后台服务,若已经存在用户登录信息, SDK 将完成自动登录)
        NIMClient.init(this, FlutterNIMPreferences.getLoginInfo(), buildSDKOptions());
    }

    // 网易云信配置(此处也可以使用自己的配置)
    private SDKOptions buildSDKOptions() {
        return FlutterNIMSDKOptionConfig.getSDKOptions(this, "123456", buildMixPushConfig());
    }

    // 网易云信第三方推送配置
    private MixPushConfig buildMixPushConfig() {

        MixPushConfig config = new MixPushConfig();

        // 小米推送
        config.xmAppId = "123456";
        config.xmAppKey = "123456";
        config.xmCertificateName = "ABCDEFG";

        // 华为推送
        config.hwCertificateName = "ABCDEFG";
        ...
        
        return config;
    }
}

登录 #

bool isSuccess = await FlutterNIM().login(imAccount, imToken);

退出登录 #

FlutterNIM().logout();

最近会话列表 #

手动获取最近会话列表:

FlutterNIM().loadRecentSessions();

监听最近会话,手动调用获取最近会话列表或最近会话列表有变更(新增、更新、删除、标记已读)都会触发此回调。

FlutterNIM().recentSessionsResponse.listen((recentSessions) {
    List<NIMSession> _recentSessions = recentSessions;

    // 在此可计算最近会话总未读数
    int unreadNum = recentSessions
        .map((s) => s.unreadCount)
        .fold(0, (curr, next) => curr + next);
    });

删除某项会话(只删除本地缓存,不会删除云端记录)

FlutterNIM().deleteRecentSession(session.sessionId);

会话 #

开启一个新会话

bool isSuccess = await FlutterNIM().startChat(session.sessionId);

监听会话消息

FlutterNIM().messagesResponse.listen((messages) {
    List<NIMMessage> _messages = messages;

    // 求余,如果结果等于零则可能还有更多数据
    int _remainder = this._messages.length % 20;
    bool _hasMoreData = remainder == 0;
    });

退出聊天页面时,需要将会话关闭

FlutterNIM().exitChat();

发送消息

// 发送文本消息
FlutterNIM().sendText(content);

// 发送图片消息
FlutterNIM().sendImage(file.path);

// 发送视频消息
FlutterNIM().sendVideo(file.path);

语音消息调用了原生云信SDK的录音能力

// 开始录音
FlutterNIM().onStartRecording();

// 结束录音,结束后自动发送语音消息
FlutterNIM().onStopRecording();

// 取消录音,不会发送
FlutterNIM().onCancelRecording();

自定义消息 #

把自定义消息内容放在Map中:


    // 随便自己定义
    Map<String, dynamic> customObject = {
      "type": "自定义",
      "url": "xxx",
      ...
    };

    FlutterNIM().sendCustomMessage(
      customObject,
      apnsContent: "[发来了一条自定义消息]",
    );
    

接收到的自定义消息体以JSON字符串的格式存在NIMMessagecustomMessageContent中,然后自己去解析:

final customObjectMap = json.decode(customMessageContent);
// 自定义
var model = CustomModel.fromJson(customObjectMap);

0.1.3 #

  • Fix bugs.

0.1.2 #

  • Fix bugs.

0.1.1 #

  • Initial release.

example/README.md

flutter_nim_example #

Demonstrates how to use the flutter_nim plugin.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_nim: ^0.1.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_nim/flutter_nim.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
43
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
72
Learn more about scoring.

We analyzed this package on Oct 11, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test