系统通知

更新时间: 2024/10/10 09:59:37

系统通知指系统下发的通知,不属于会话内消息,例如单聊场景中的 对方正在输入 功能,及群聊场景中的入群申请、入群邀请等。系统通知包括广播通知和自定义系统通知两种类型。

本文介绍自定义系统通知收发相关 API。更多系统通知相关功能请参考开发指南文档 系统通知概述

API 概览

系统通知监听

API 说明 起始版本
add 注册系统通知相关监听器 v10.3.0
cancel 取消注册系统通知相关监听器 v10.3.0

系统通知发送

API 说明 起始版本
sendCustomNotification 发送自定义系统通知 v10.3.0

接口类

NotificationService 类提供自定义通知相关接口。

add

接口描述

注册系统通知相关监听。

注册成功后,当事件发生时,SDK 会返回对应的回调。

  • 建议在初始化后调用该方法。
  • 全局只需注册一次。
  • 该方法为同步。

回调事件

  • onReceiveCustomNotifications:自定义通知接收回调,返回自定义通知列表,包括本端接收的自定义通知及其他端同步的自定义通知。
  • onReceiveBroadcastNotifications:服务端下发的广播通知接收回调,返回广播通知列表。

示例代码

dartsubsriptions.add(
    NimCore.instance.notificationService.onReceiveBroadcastNotifications.listen((notify){
      //do something
    })
  );

  subsriptions.add(
      NimCore.instance.notificationService.onReceiveCustomNotifications.listen((notify){
        //do something
      })
  );

cancel

接口描述

取消注册系统通知相关监听。

该方法为同步。

示例代码

dartsubsriptions.forEach((subsription) {
      subsription.cancel();
    });

sendCustomNotification

接口描述

发送一条自定义通知。

本端发送自定义通知成功后,其他登录客户端和通知接收方会收到自定义通知接收回调 onReceiveCustomNotifications

自定义通知仅适用于单聊和群聊场景。

参数说明

dartFuture<NIMResult<void>> sendCustomNotification(String conversationId,
    String content, NIMSendCustomNotificationParams params) {
  return _platform.sendCustomNotification(conversationId, content, params);
}
参数名称 类型 是否必填 说明
conversationId String 会话 ID,通过调用 ConversationIdUtil 的对应函数创建。
  • 组成方式:用户账号(accountId)|会话类型(NIMConversationType)|聊天对象账号(accountId)或群组 ID。
  • 如果为空或不存在则返回 191004 参数错误。
  • content String 自定义通知内容。必须封装为 JSON 格式,长度上限为 4096 字节。
    params NIMSendCustomNotificationParams 自定义通知发送配置参数,包括发送、推送、抄送、反垃圾等配置。

    示例代码

    dartawait NimCore.instance.notificationService.sendCustomNotification(conversationId, content, params);
    

    返回值

    NIMResult<void>

    相关回调

    onReceiveCustomNotifications:自定义通知接收回调

    此文档是否对你有帮助?
    有帮助
    去反馈
    • API 概览
    • 系统通知监听
    • 系统通知发送
    • 接口类
    • add
    • cancel
    • sendCustomNotification