使用 ZIM SDK 实现离线推送
2026-04-02
ZIM 支持在发送单聊消息、发送群组消息、发起呼叫邀请等场景,使用离线推送消息的功能。
实现流程
场景 1:发送 “单聊消息” 或 “群组消息” 时使用离线推送功能
-
首先开发者需要通过 ZIMPushConfig 对象,设置离线推送标题、内容、以及其他自定义属性。
-
然后通过 ZIMMessageSendConfig 对象的
pushConfig参数,配置离线消息的相关配置等。 -
发送方调用 sendMessage,传入 “sentConfig”,向接收方发送单聊消息或者向群组发送群聊消息。
-
接收方或者群组内的用户,如果处于离线状态,将会在上线后,接收到发送方之前发送的离线消息。
const pushConfig: ZIMPushConfig = {
title = "离线推送标题";
content = "离线推送内容";
payload = "自定义透传字段,非必填";
resourcesID = "资源 ID,非必填";
}
const sendConfig: ZIMMessageSendConfig = {
priority: 2,
pushConfig: pushConfig
};
const toUserID = '';
const messageTextObj: ZIMMessage = { type: 1, message: '文本消息内容' };
zim.sendMessage(messageTextObj, toUserID, 0, sendConfig)
.then((res: ZIMMessageSentResult) => {
// 发送成功
})
.catch((err: ZIMError) => {
// 发送失败
});场景 2:发送呼叫邀请时使用离线推送功能
-
开发者通过 ZIMPushConfig 对象,设置离线推送标题、内容、以及其他自定义属性。
-
然后通过 ZIMCallInviteConfig 对象的
pushConfig参数,配置离线消息的相关配置等。 -
发送方调用 callInvite,传入 “callInviteConfig”,发起呼叫邀请。
-
被邀请的用户若处于离线状态,将会收到对应的离线推送,上线后,若呼叫邀请还未结束,将会收到 callInvitationReceived 的回调。
const pushConfig: ZIMPushConfig = {
title: "离线推送标题",
content: "离线推送内容",
payload: "自定义透传字段,非必填",
resourcesID: "资源 ID,非必填",
}
const callInviteConfig: ZIMCallInviteConfig = {
mode: 0,
timeout: 90,
extendedData: '',
pushConfig: pushConfig
}
const invitees = ['xxxx']; // 被邀请人ID列表
zim.callInvite(invitees, callInviteConfig)
.then((res: ZIMCallInvitationSentResult) => {
const callID = res.callID;
// 操作成功
// 此处的 callID 是用户发起呼叫后,SDK 内部生成的 ID,用于唯一标识一次呼叫邀请;之后发起人取消呼叫、被邀请人接受/拒绝呼叫,都会使用此 callID
})
.catch((err: ZIMError) => {
// 操作失败
})场景 3:iOS 通知分组
开发者在通知发送端配置 ZIMPushConfig 的 threadID 参数,即可实现 iOS 离线推送的通知分组效果。
const pushConfig: ZIMPushConfig = {
title: "离线推送标题",
content: "离线推送内容",
threadID: "通知分组标志",
}场景 4:iOS 通知徽标数量
开发者可通过调用 updateUserBadge 接口更新徽标数量。
常见使用场景为:多端登录时,如果某端清除了某个会话未读消息数后,iOS 端的通知徽标数量需要同步减掉该会话的未读消息数。 比如,当前徽标数量为 10,清除的会话未读数为 8,那么可调用此接口更新徽标数量为 2。
zim.updateUserBadge(2)
.then(() => {
// 操作成功
})
.catch((err: ZIMError) => {
// 操作失败
});