You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断HmsMessageService接收的RemoteMessage是否携带通知

HMS RemoteMessage消息类型判断方法

判断逻辑

调用RemoteMessage实例的getNotification()方法即可快速区分两类消息:

  • 若返回值非空:当前为带通知的消息,消息内置了通知栏展示所需的标题、内容、图标等配置,默认会由HMS SDK自动弹出通知栏提示
  • 若返回值为null:当前为纯数据消息,仅携带开发者自定义的payload数据,SDK不会做自动处理,所有业务逻辑需要开发者自行实现

完整代码示例

public class HmsService extends HmsMessageService {

  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    // 判断消息类型
    if (remoteMessage.getNotification() != null) {
        // 带通知的消息处理逻辑
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        // 可按需获取通知相关属性
        String notifyTitle = notification.getTitle();
        String notifyContent = notification.getBody();
    } else {
        // 纯数据消息处理逻辑
        Map<String, String> customData = remoteMessage.getData();
        // 读取自定义字段执行业务逻辑
    }
  }
}

补充说明

当应用处于后台运行状态时,带通知的消息默认会由系统直接渲染弹出通知,不会触发onMessageReceived回调,仅在应用前台运行时才会回调该方法。

内容的提问来源于stack exchange,提问作者ccd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 14:39:03