设置多端推送/提醒策略
更新时间: 2024/08/08 17:10:33
云信 NIM SDK 支持配置多端推送策略,即当桌面端(包括 Windows,Web,macOS 等)在线时,SDK 支持设置是否需要推送给移动端。若移动端处于离线,则离线推送通知用户;若处于在线,则在线提醒用户。
前提条件
已实现第三方离线推送或在线提醒。
实现方法
-
通过调用
addSettingListener
方法注册系统设置事件观察者,监听多端推送/提醒配置的更新。示例代码如下:
AndroidJava
NIMClient.getService(V2NIMSettingService.class).addSettingListener(new V2NIMSettingListener() { @Override public void onTeamMessageMuteModeChanged(String teamId, V2NIMTeamType teamType, V2NIMTeamMessageMuteMode muteMode) { } @Override public void onP2PMessageMuteModeChanged(String accountId, V2NIMP2PMessageMuteMode muteMode) { } });
iOSObjective-C
[[[NIMSDK sharedSDK] v2SettingService] addSettingListener:self];
Windows/macOSc++
V2NIMTeamListener listener; listener.onTeamMessageMuteModeChanged = [](std::string teamId, V2NIMTeamType teamType, V2NIMTeamMessageMuteMode muteMode) { // handle team message mute mode changed }; listener.onP2PMessageMuteModeChanged = [](std::string accountId, V2NIMP2PMessageMuteMode muteMode) { // handle p2p message mute mode changed }; settingService.addSettingListener(listener);
Web/uni-app/小程序TypeScript
nim.V2NIMSettingService.on("onTeamMessageMuteModeChanged", function (teamId: string, teamType: V2NIMTeamType, muteMode: V2NIMTeamMessageMuteMode) nim.V2NIMSettingService.on("onP2PMessageMuteModeChanged", function (accountId: string, muteMode: V2NIMP2PMessageMuteMode)
HarmonyOSTypeScript
nim.settingService.on('onTeamMessageMuteModeChanged', (teamId: string, teamType: V2NIMTeamType, muteMode: V2NIMTeamMessageMuteMode) => {}) nim.settingService.on('onP2PMessageMuteModeChanged', (accountId: string, muteMode: V2NIMP2PMessageMuteMode) => {})
-
通过调用
setPushMobileOnDesktopOnline
方法来设置当桌面端在线时,是否需要推送给移动端。入参
need
为 true 表示桌面端在线时移动端需要推送。示例代码如下:
AndroidJava
NIMClient.getService(V2NIMSettingService.class).setPushMobileOnDesktopOnline(needPushMobileOnDesktopOnline, new V2NIMSuccessCallback<Void>() { @Override public void onSuccess(Void unused) { } }, new V2NIMFailureCallback() { @Override public void onFailure(V2NIMError error) { } });
iOSObjective-C
[[[NIMSDK sharedSDK] v2SettingService] setPushMobileOnDesktopOnline:YES success:^{ // 成功回调 } failure:^(V2NIMError * _Nonnull error) { // 失败回调 }];
Windows/macOSc++
settingService.setPushMobileOnDesktopOnline( true, []() { // set push mobile on desktop online succeeded }, [](V2NIMError error) { // set push mobile on desktop online failed, handle error });
Web/uni-app/小程序TypeScript
nim.V2NIMSettingService.setPushMobileOnDesktopOnline(true)
HarmonyOSTypeScript
await nim.settingService.setPushMobileOnDesktopOnline(true)
如果 SDK 已配置 多端登录,且已设置多端推送,则满足推送条件的各个客户端都会收到离线推送或在线提醒。如果不希望多个客户端同时收到推送或提醒,请关闭多端登录或者关闭多端推送/提醒功能。
此文档是否对你有帮助?