发送消息前添加配置参数

更新时间: 2024/09/27 10:50:00

本文主要介绍如何在发送消息前配置参数。

在初始化 store 时,可在 sendMsgBefore 中进行配置,在 sendMsgBefore 中可以通过 options 获取发送前的消息体,可在该消息体上挂载其他字段,通过 type 获取当前发送消息的类型,可根据不同消息类型进行处理,最后在 sendMsgBefore中return 最后挂载字段后的消息体即可。

const store = new RootStore(nim, {
        //...
        sendMsgBefore: async (options: any, type: IMMessage['type']) => {
          const pushInfo: any = {
            needPush: true,
            needPushBadge: true,
            pushPayload: '{}',
            pushContent,
            needForcePush,
            forcePushIDsList,
            forcePushContent: pushContent,
          }
          return { ...options, pushInfo }
        },
      })

例如,在发送消息前,对消息添加推送相关参数。

const store = new RootStore(nim, {
        //...
        sendMsgBefore: async (options: any, type: IMMessage['type']) => {
          const pushContent = getMsgContentTipByType({
            body: options.body,
            type,
          })
          const yxAitMsg = options.ext
            ? options.ext.yxAitMsg
            : { forcePushIDsList: '[]', needForcePush: false }

          // 如果是 at 消息,需要走离线强推
          // @ts-ignore
          const { forcePushIDsList, needForcePush } = yxAitMsg
            ? // @ts-ignore
              store.msgStore._formatExtAitToPushInfo(yxAitMsg, options.body)
            : { forcePushIDsList: '[]', needForcePush: false }

          console.log('forcePushIDsList: ', forcePushIDsList)

          // 不同产商的推送消息体
          const { scene, to } = options
          const pushPayload = JSON.stringify({
            // oppo
            oppoField: {
              click_action_type: 4, // 参考 oppo 官网
              click_action_activity: '', // 各端不一样 TODO
              action_parameters: { sessionId: scene, sessionType: to }, // 自定义
            },

            // vivo
            vivoField: {
              pushMode: 0, //推送模式 0:正式推送;1:测试推送,不填默认为0
            },

            // huawei
            hwField: {
              click_action: {
                type: 1,
                action: '', // 各端不一样 TODO
              },
              androidConfig: {
                category: 'IM',
                data: JSON.stringify({ sessionId: to, sessionType: scene }),
              },
            },

            // 通用
            sessionId: to,
            sessionType: scene,
          })

          const pushInfo: any = {
            needPush: true,
            needPushBadge: true,
            pushPayload: '{}',
            pushContent,
            needForcePush,
            forcePushIDsList,
            forcePushContent: pushContent,
          }
          return { ...options, pushInfo }
        },
      })
此文档是否对你有帮助?
有帮助
去反馈