如何实现消息不折叠展示
在移动应用推送场景中,消息折叠问题会直接影响用户体验和信息触达效率。本文基于AppPush推送产品,从通知消息(Notification)和自定义消息(Custom Message)两种类型出发,详细说明如何通过技术配置实现消息展示不折叠的效果。
一、消息类型与展示规则对比
通知消息(Notification)
- 系统默认行为:
App状态 | 展示规则 |
---|---|
前台运行 | 可通过 group_id 控制折叠逻辑 |
后台运行 | 由手机系统接管,默认从通知栏弹出,展示形式受限(如折叠、数量限制等),难以自定义样式 |
- 适用场景: 需通过系统通知栏触达用户的标准推送。
自定义消息(Custom Message)
核心优势:
无论 App 处于前台、后台或被一键清理状态,均可通过客户端代码完全控制消息展示形式(如弹窗位置、折叠逻辑等)。适用场景:
需要定制化交互的高优先级消息(如订单状态提醒、即时聊天)。
二、实现消息不折叠的两种方案
方案1:通过通知消息实现
(FCM 通道需 App 在前台,EngageLab 通道不区分前后台)
实现原理
利用 group_id
字段控制消息分组逻辑:
- 不折叠:为每条消息分配唯一的
group_id
操作步骤
支持版本:Android SDK V5.0.1及以上版本支持
推送 API 参数配置(请求体)示例:
{
"notification": {
"android": {
"group_id": "UNIQUE_GROUP_123" // 唯一值实现不折叠
}
}
}
{
"notification": {
"android": {
"group_id": "UNIQUE_GROUP_123" // 唯一值实现不折叠
}
}
}
此代码块在浮窗中显示
兼容性说明
- 仅支持 FCM 和 EngageLab 推送通道。
- 若 App 在后台,消息展示仍受系统限制。
方案2:通过自定义消息实现(全场景支持)
实现原理
通过客户端主动调用 showNotification
接口,完全掌控消息渲染逻辑,规避系统限制。
操作步骤
1. 发送自定义消息
{
"message": {
"msg_content": "您的订单已发货",
"content_type": "text",
"extras": {
"type": "priority_alert",
"style": "popup_bottom" // 自定义展示样式
}
}
}
{
"message": {
"msg_content": "您的订单已发货",
"content_type": "text",
"extras": {
"type": "priority_alert",
"style": "popup_bottom" // 自定义展示样式
}
}
}
此代码块在浮窗中显示
2. 客户端处理消息(Android 示例)
public class UserReceiver extends MTCommonReceiver {
@Override
public void onCustomMessage(Context context, CustomMessage customMessage) {
ExampleLogger.i(TAG, "onCustomMessage:" + customMessage.toString());
showNotification(context, customMessage); // 调用展示逻辑
}
}
public class UserReceiver extends MTCommonReceiver {
@Override
public void onCustomMessage(Context context, CustomMessage customMessage) {
ExampleLogger.i(TAG, "onCustomMessage:" + customMessage.toString());
showNotification(context, customMessage); // 调用展示逻辑
}
}
此代码块在浮窗中显示
3. 自定义展示逻辑示例(实现不折叠)
public void showNotification(Context context, CustomMessage customMessage) {
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId(customMessage.getMessageId())
.setNotificationId("你自定义一个,但每个通知不要一样")
.setTitle(customMessage.getTitle())
.setContent(customMessage.getContent())
.setgroup_id(customMessage.getMessageId()); // 不折叠关键字段
MTPushPrivatesApi.showNotification(context, notificationMessage);
}
public void showNotification(Context context, CustomMessage customMessage) {
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId(customMessage.getMessageId())
.setNotificationId("你自定义一个,但每个通知不要一样")
.setTitle(customMessage.getTitle())
.setContent(customMessage.getContent())
.setgroup_id(customMessage.getMessageId()); // 不折叠关键字段
MTPushPrivatesApi.showNotification(context, notificationMessage);
}
此代码块在浮窗中显示
优势对比
能力 | 通知消息 | 自定义消息 |
---|---|---|
支持 App 状态 | FCM 仅前台,EngageLab 前后台皆可 | 全场景(前台/后台) |
样式自由度 | 受限(依赖系统) | 完全自定义 |
通道依赖性 | 需特定通道(FCM、EngageLab) | 无限制 |
三、注意事项
通道兼容性
group_id
功能目前仅支持 FCM 和 EngageLab 通道,其他通道暂不支持。
性能优化
- 高频发送独立
group_id
消息时,需监控系统通知栏缓存压力(建议单设备每小时 ≤30 条)。
- 高频发送独立
四、总结
- 轻量级需求: 优先使用
group_id
控制通知消息折叠逻辑,开发成本低。 - 高定制需求: 选择自定义消息方案,通过客户端代码实现全场景可控展示。
- 混合策略: 对重要消息(如支付成功)使用自定义消息,普通通知建议使用
group_id
管理。
通过合理选择方案,可显著提升关键消息的触达率和用户交互体验。