免打扰设置
更新时间: 2024/11/18 15:53:45
网易云信即时通讯 SDK(NetEase IM SDK,简称 NIM SDK)支持消息免打扰功能,即关闭离线推送消息和在线提醒。
本文介绍如何通过 NIM SDK 实现单聊、群聊消息、以及全局免打扰功能。
支持平台
本文内容适用的开发平台或框架如下表所示:
Android | iOS | macOS/Windows | Web/uni-app/小程序 | Node.js/Electron | HarmonyOS | Flutter |
---|---|---|---|---|---|---|
✔️️️ | ✔️️️ | ✔️️️ | ✔️️️ | ✔️️️ | ✔️️️ | ✔️️️ |
单聊消息免打扰
网易云信支持对用户设置免打扰,即设置与指定用户的单聊会话消息的免打扰。
如果将用户设置为免打扰,当收到该用户发来的消息时,不会触发在线消息提醒和离线推送的通知栏。
例如:A 用户将 B 用户设置为免打扰,B 给 A 发消息,A 不会触发在线消息提醒和离线推送的通知栏。
注册单聊消息免打扰监听
在进行单聊消息免打扰模式设置前,您可以调用接口注册监听单聊消息免打扰模式变更事件。监听后,在单聊消息免打扰模式变更时,会收到对应的通知。
Android/iOS/macOS/Windows:addSettingListener
JavaNIMClient.getService(V2NIMSettingService.class).addSettingListener(new V2NIMSettingListener() {
@Override
public void onP2PMessageMuteModeChanged(String accountId, V2NIMP2PMessageMuteMode muteMode) {
}
});
Objective-C[[[NIMSDK sharedSDK] v2SettingService] addSettingListener:self];
C++V2NIMTeamListener listener;
listener.onP2PMessageMuteModeChanged = [](std::string accountId, V2NIMP2PMessageMuteMode muteMode) {
// handle p2p message mute mode changed
};
settingService.addSettingListener(listener);
Web/uni-app/小程序/Node.js/Electron/HarmonyOS:on("EventName")
TypeScriptnim.V2NIMSettingService.on("onP2PMessageMuteModeChanged", function (accountId: string, muteMode: V2NIMP2PMessageMuteMode)
TypeScriptv2.settingService.on("p2PMessageMuteModeChanged", function (accountId: string, muteMode: V2NIMP2PMessageMuteMode)
TypeScriptnim.settingService.on('onP2PMessageMuteModeChanged', (accountId: string, muteMode: V2NIMP2PMessageMuteMode) => {})
Flutter:add
dartsubsriptions.add(NimCore.instance.settingsService.onP2PMessageMuteModeChanged.listen((e) {
// do something
}));
设置单聊消息免打扰模式
通过调用 setP2PMessageMuteMode
方法来设置单聊消息免打扰模式。
示例代码:
JavaNIMClient.getService(V2NIMSettingService.class).setP2PMessageMuteMode(accountId, muteMode,
new V2NIMSuccessCallback<Void>() {
@Override
public void onSuccess(Void unused) {
}
},
new V2NIMFailureCallback() {
@Override
public void onFailure(V2NIMError error) {
}
});
Objective-C[[[NIMSDK sharedSDK] v2SettingService] setP2PMessageMuteMode:@"accountId" muteMode:V2NIM_P2P_MESSAGE_MUTE_MODE_OFF success:^{
// 成功回调
} failure:^(V2NIMError * _Nonnull error) {
// 失败回调
}];
C++settingService.setP2PMessageMuteMode(
"accountId",
V2NIM_P2P_MESSAGE_MUTE_MODE_ON,
[]() {
// set p2p mute mode succeeded
},
[](V2NIMError error) {
// set p2p mute mode failed, handle error
});
TypeScriptnim.V2NIMSettingService.setP2PMessageMuteMode('accountId', 1)
TypeScriptawait v2.settingService.setP2PMessageMuteMode('accountId', 1)
TypeScriptawait nim.settingService.setP2PMessageMuteMode('accountId', V2NIMP2PMessageMuteMode.V2NIM_P2P_MESSAGE_MUTE_MODE_ON)
dartawait NimCore.instance.settingsService.setP2PMessageMuteMode(accountId, muteMode);
获取单聊消息免打扰状态
通过调用 getP2PMessageMuteMode
方法获取指定聊天对象的单聊会话免打扰状态。
示例代码:
JavaV2NIMP2PMessageMuteMode muteMode = NIMClient.getService(V2NIMSettingService.class).getP2PMessageMuteMode(accountId);
Objective-C[[[NIMSDK sharedSDK] v2SettingService] getP2PMessageMuteMode:@"accountId"];
C++auto muteMode = settingService.getP2PMessageMuteMode("accountId");
TypeScriptconst mode = nim.V2NIMSettingService.getP2PMessageMuteMode('accountId')
TypeScriptconst mode = v2.settingService.getP2PMessageMuteMode('accountId')
TypeScriptconst mode = await nim.settingService.getP2PMessageMuteMode('accountId')
dartawait NimCore.instance.settingsService.getP2PMessageMuteMode(accountId);
群聊消息免打扰
SDK 支持对群消息设置免打扰。设置群消息免打扰后,该群组的消息不会触发在线消息提醒和离线推送的通知栏。
群消息免打扰模式分为以下三种:
- MUTE_MODE_OFF:(默认)群消息免打扰关闭,即所有群消息均推送或提醒。
- MUTE_MODE_ON:群消息免打扰开启,即所有群消息均不推送或不提醒。
- MUTE_MODE_NORMAL_ON:普通成员群消息免打扰开启,即仅 群主和管理员 的消息推送或提醒。
以上群消息的推送和提醒指网易云信支持的 离线推送 和 在线提醒。
群消息的通知模式不影响群消息的接收和未读数的变化。若将通知模式设置为免打扰,仍然能接收到群消息,未读数仍会变化,只是不会触发在线消息提醒和离线推送的通知栏。
注册群消息免打扰监听
在进行群消息免打扰模式设置前,您可以调用 addSettingListener
方法注册监听相关事件。监听后,在群消息免打扰模式变更时,会收到对应的通知。
示例代码如下:
JavaNIMClient.getService(V2NIMSettingService.class).addSettingListener(new V2NIMSettingListener() {
@Override
public void onTeamMessageMuteModeChanged(String teamId, V2NIMTeamType teamType, V2NIMTeamMessageMuteMode muteMode) {
// handle team message mute mode changed
}
});
Objective-C[[[NIMSDK sharedSDK] v2SettingService] addSettingListener:self];
C++V2NIMTeamListener listener;
listener.onTeamMessageMuteModeChanged = [](std::string teamId, V2NIMTeamType teamType, V2NIMTeamMessageMuteMode muteMode) {
// handle team message mute mode changed
};
settingService.addSettingListener(listener);
Web/uni-app/小程序/Node.js/Electron/HarmonyOS
在进行群消息免打扰模式设置前,您可以调用 on("EventName")
注册监听群消息免打扰模式变更事件。监听后,在群消息免打扰模式变更时,会收到对应的通知。
TypeScriptnim.V2NIMSettingService.on("onTeamMessageMuteModeChanged", function (teamId: string, teamType: V2NIMTeamType, muteMode: V2NIMTeamMessageMuteMode)
TypeScriptv2.settingService.on("teamMessageMuteModeChanged", function (teamId: string, teamType: V2NIMTeamType, muteMode: V2NIMTeamMessageMuteMode)
TypeScriptnim.settingService.on('onTeamMessageMuteModeChanged', (teamId: string, teamType: V2NIMTeamType, muteMode: V2NIMTeamMessageMuteMode) => {})
Flutter:add
dartsubsriptions.add(NimCore.instance.settingsService.onTeamMessageMuteModeChanged.listen((e) {
// do something
}));
设置群消息免打扰
通过调用 setTeamMessageMuteMode
方法来设置群消息免打扰模式。
示例代码:
JavaNIMClient.getService(V2NIMSettingService.class).setTeamMessageMuteMode(teamId, teamType, muteMode,
new V2NIMSuccessCallback<Void>() {
@Override
public void onSuccess(Void unused) {
// success
}
},
new V2NIMFailureCallback() {
@Override
public void onFailure(V2NIMError error) {
// failed, handle error
}
});
Objective-C[[[NIMSDK sharedSDK] v2SettingService] setTeamMessageMuteMode:@"teamId" teamType:V2NIM_TEAM_TYPE_INVALID muteMode:V2NIM_TEAM_MESSAGE_MUTE_MODE_OFF success:^{
// 成功回调
} failure:^(V2NIMError * _Nonnull error) {
// 失败回调
}];
C++settingService.setTeamMessageMuteMode(
"teamId",
V2NIM_TEAM_TYPE_NORMAL,
V2NIM_TEAM_MESSAGE_MUTE_MODE_ON,
[]() {
// set team mute mode succeeded
},
[](V2NIMError error) {
// set team mute mode failed, handle error
}
);
TypeScriptnim.V2NIMSettingService.setTeamMessageMuteMode('teamId', 1, 1)
TypeScriptawait v2.settingService.setTeamMessageMuteMode('teamId', 1)
TypeScriptawait nim.settingService.setTeamMessageMuteMode('teamId', V2NIMTeamType.V2NIM_TEAM_TYPE_NORMAL, V2NIMTeamMessageMuteMode.V2NIM_TEAM_MESSAGE_MUTE_MODE_ON)
dartawait NimCore.instance.settingsService.setTeamMessageMuteMode(teamId, teamType, muteMode);
获取群消息免打扰状态
通过调用 getTeamMessageMuteMode
方法获取指定群的免打扰状态。
示例代码:
JavaV2NIMTeamMessageMuteMode muteMode = NIMClient.getService(V2NIMSettingService.class).getTeamMessageMuteMode(teamId, teamType);
Objective-C[[[NIMSDK sharedSDK] v2SettingService] getTeamMessageMuteMode:@"teamId" teamType:V2NIM_TEAM_TYPE_NORMAL];
C++auto muteMode = settingService.getTeamMessageMuteMode("teamId", V2NIM_TEAM_TYPE_NORMAL);
TypeScriptconst muteMode = nim.V2NIMSettingService.getTeamMessageMuteMode('teamId', 1)
TypeScriptconst muteMode = v2.settingService.getTeamMessageMuteMode('teamId', 1)
TypeScriptconst muteMode = await nim.settingService.getTeamMessageMuteMode('teamId', V2NIMTeamType.V2NIM_TEAM_TYPE_NORMAL)
dartawait NimCore.instance.settingsService.getTeamMessageMuteMode(teamId, teamType);
获取会话消息免打扰状态
通过调用 getConversationMuteStatus
方法获取指定会话的免打扰状态。
如果该方法返回值不满足界面显示需求,可以调用 getP2PMessageMuteMode
或 getTeamMessageMuteMode
方法获取单聊或群聊消息的具体免打扰模式。
Javaboolean isMute = NIMClient.getService(V2NIMSettingService.class).getConversationMuteStatus(conversationId);
Objective-C[[NIMSDK sharedSDK] v2SettingService] getConversationMuteStatus:@"conversationId"];
C++auto mute = settingService.getConversationMuteStatus(conversationId);
TypeScriptnim.V2NIMSettingService.getConversationMuteStatus('sender|1|receiver')
TypeScriptconst status = v2.settingService.getConversationMuteStatus('conversationId')
TypeScriptawait nim.settingService.getConversationMuteStatus('sender|1|receiver')
dartawait NimCore.instance.settingsService.getConversationMuteStatus(conversationId);
推送/提醒全局免打扰
网易云信 NIM SDK 支持全局的推送属性设置,用于设置推送/提醒的全局免打扰。
对于 Android 端而言,设置推送包括离线推送和在线提醒的免打扰。
当用户不需要在接收第三方推送服务的离线推送或在线提醒,或者只需要接收某段时间的离线推送或在线提醒时,用户可以通过配置推送属性来实现。
前提条件
已实现第三方离线推送或在线提醒。
实现方法
在 开启第三方离线推送服务或在线提醒 的情况下,通过 setDndConfig
方法设置全局的免打扰时间段。
示例代码:
JavaV2NIMDndConfig dndConfig = V2NIMDndConfig.V2NIMDndConfigBuilder.builder(fromH, fromM, toH, toM).build();
NIMClient.getService(V2NIMSettingService.class).setDndConfig(dndConfig,
new V2NIMSuccessCallback<Void>() {
@Override
public void onSuccess(Void unused) {
}
},
new V2NIMFailureCallback() {
@Override
public void onFailure(V2NIMError error) {
}
});
Objective-C[[[NIMSDK sharedSDK] v2SettingService] setDndConfig:config];
C++V2NIMDndConfig config;
settingService.setDndConfig(
config,
[]() {
// set dnd config succeeded
},
[](V2NIMError error) {
// set dnd config failed, handle error
});
);
TypeScriptconst res = await nim.settingService.setDndConfig({
showDetail: true,
dndOn: true,
fromH: 22,
fromM: 30,
toH: 6,
toM: 30
} as V2NIMDndConfig)
dartawait NimCore.instance.settingsService.setDndConfig(config);
- 设置完成后,可以调用
getDndConfig
方法获取推送服务免打扰的时间。 - 对于 Android 端,您也可以调用
enable
方法关闭第三方推送服务,关闭后,将不再接收离线推送消息。 - 对于 Android 端,您也可以通过初始化参数
SDKOptions.statusBarNotificationConfig
来控制通知栏消息提醒功能,将其设置为 null,即 SDK 不提供通知栏提醒功能。
涉及接口
API | 说明 |
---|---|
addSettingListener |
注册系统设置相关监听器 |
removeSettingListener |
取消注册系统设置相关监听器 |
setP2PMessageMuteMode |
设置单聊消息免打扰模式 |
getP2PMessageMuteMode |
获取单聊消息免打扰模式 |
setTeamMessageMuteMode |
设置群消息免打扰模式 |
getTeamMessageMuteMode |
获取群消息免打扰模式 |
getConversationMuteStatus |
获取会话消息免打扰状态 |
setDndConfig |
设置推送全局免打扰 |
getDndConfig |
获取推送全局免打扰配置信息 |
API | 说明 |
---|---|
on("EventName") |
注册系统设置相关监听器 |
off("EventName") |
取消注册系统设置相关监听器 |
setP2PMessageMuteMode |
设置单聊消息免打扰模式 |
getP2PMessageMuteMode |
获取单聊消息免打扰模式 |
setTeamMessageMuteMode |
设置群消息免打扰模式 |
getTeamMessageMuteMode |
获取群消息免打扰模式 |
getConversationMuteStatus |
获取会话消息免打扰状态 |
setDndConfig |
设置推送全局免打扰 |
getDndConfig |
获取推送全局免打扰配置信息 |
API | 说明 |
---|---|
add |
注册系统设置相关监听器 |
cancel |
取消注册系统设置相关监听器 |
setP2PMessageMuteMode |
设置单聊消息免打扰模式 |
getP2PMessageMuteMode |
获取单聊消息免打扰模式 |
setTeamMessageMuteMode |
设置群消息免打扰模式 |
getTeamMessageMuteMode |
获取群消息免打扰模式 |
getConversationMuteStatus |
获取会话消息免打扰状态 |
setDndConfig |
设置推送全局免打扰 |
getDndConfig |
获取推送全局免打扰配置信息 |