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

Spring Boot服务端是否支持生成FCM token?

Spring Boot服务端FCM相关令牌生成实现方案

常规业务场景下终端用户设备的FCM推送令牌是由安卓/iOS/Web客户端集成FCM SDK后生成,再上报到服务端存储用于推送。如果你需要的是服务端生成调用FCM官方API所需的OAuth2访问令牌,可以参考以下实现方案。

1. 前置准备

  • 先在Firebase控制台创建项目,进入项目设置的「服务账号」tab,生成Java语言对应的服务账号私钥JSON文件,放到Spring Boot项目的src/main/resources目录下
  • 引入Firebase Admin SDK依赖,Maven配置如下:
<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>9.2.0</version>
</dependency>

2. Firebase初始化配置

编写配置类初始化Firebase App实例:

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.io.InputStream;

@Configuration
public class FirebaseConfig {

    @Bean
    public FirebaseApp firebaseApp() throws IOException {
        // 替换为你的服务账号私钥文件名
        InputStream serviceAccount = getClass().getResourceAsStream("/firebase-service-account.json");
        FirebaseOptions options = FirebaseOptions.builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .build();
        return FirebaseApp.initializeApp(options);
    }
}

3. 生成FCM调用用的访问令牌

编写工具类生成访问令牌,令牌默认有效期1小时,建议本地缓存避免重复生成:

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

@Component
public class FcmTokenGenerator {
    // FCM API的OAuth2作用域固定值
    private static final String FCM_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
    // 本地缓存令牌,提前10分钟过期避免调用时失效
    private final Cache<String, String> tokenCache = CacheBuilder.newBuilder()
            .expireAfterWrite(50, TimeUnit.MINUTES)
            .build();

    @Autowired
    private FirebaseApp firebaseApp;

    public String getFcmAccessToken() throws IOException {
        String cachedToken = tokenCache.getIfPresent("fcm_access_token");
        if (cachedToken != null) {
            return cachedToken;
        }
        GoogleCredentials credentials = firebaseApp.getOptions().getCredentials();
        credentials = credentials.createScoped(Collections.singletonList(FCM_SCOPE));
        credentials.refreshIfExpired();
        String accessToken = credentials.getAccessToken().getTokenValue();
        tokenCache.put("fcm_access_token", accessToken);
        return accessToken;
    }
}

4. 注意事项

  • 生成的访问令牌仅用于服务端调用FCM v1 API时放在请求头的Authorization字段,格式为Bearer {访问令牌},不能代替终端用户设备的推送令牌使用
  • 服务账号私钥文件不要提交到公开代码仓库,生产环境建议通过配置中心或环境变量注入敏感信息
  • 不推荐在服务端模拟生成终端设备的FCM推送令牌,该场景本身存在账号权限泄露的安全风险,统一由客户端生成上报即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 21:18:02