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

如何使用Firebase Cloud Messaging Java Admin SDK发送本地化通知?是否支持该功能?

Firebase Cloud Messaging Java Admin SDK 本地化支持说明

Firebase Cloud Messaging(FCM)的Java Admin SDK完全支持本地化功能,对应REST接口中的body_loc_key、title_loc_key等字段,可通过SDK内置的Notification类相关方法实现。

核心实现方式

在构建推送通知时,使用Notification.Builder的以下方法设置本地化相关参数:

  • setTitleLocKey(String key):对应REST的title_loc_key,指定设备端本地化资源中标题的键值
  • setBodyLocKey(String key):对应REST的body_loc_key,指定设备端本地化资源中内容的键值
  • setTitleLocArgs(List<String> args):对应REST的title_loc_args,传递标题本地化所需的动态参数
  • setBodyLocArgs(List<String> args):对应REST的body_loc_args,传递内容本地化所需的动态参数

代码示例

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import java.util.Arrays;

public class FcmLocalizationExample {
    public static void main(String[] args) throws Exception {
        // 构建本地化通知实例
        Notification localizedNotification = Notification.builder()
                .setTitleLocKey("order_success_title")
                .setBodyLocKey("order_success_body")
                .setTitleLocArgs(Arrays.asList("12345")) // 订单号参数
                .setBodyLocArgs(Arrays.asList("2件", "次日达")) // 商品数量、配送时间参数
                .build();

        // 构建推送消息
        Message message = Message.builder()
                .setNotification(localizedNotification)
                .setToken("DEVICE_REGISTRATION_TOKEN") // 目标设备令牌
                .build();

        // 发送消息
        String messageId = FirebaseMessaging.getInstance().send(message);
        System.out.println("推送成功,消息ID: " + messageId);
    }
}

工作逻辑说明

设备端接收到包含本地化键的推送后,会自动根据当前系统语言,从本地字符串资源文件(如Android的strings.xml、iOS的Localizable.strings)中匹配对应键的文本,并替换参数内容,最终展示本地化后的通知。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 08:25:14