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

Java Spring Boot中Firebase组播推送通知遇404错误求助

Firebase sendMulticast 批量发送通知出现404错误

问题概述

开发新闻门户项目,需要给所有已获取的设备Token发送最新新闻通知。单Token发送通知功能正常,但调用Firebase的sendMulticast批量发送时触发404错误,提示请求的/batch路径不存在。已引入guava、Firebase Admin等必要依赖,代码取自Firebase控制台示例。

报错信息

Error Sending Notifications: Unexpected HTTP response with status: 404
404. That’s an error.
The requested URL /batch was not found on this server. That’s all we know.

相关代码

public class FireBaseMessagingService {
    
    private static final Logger log = LoggerFactory.getLogger(FireBaseMessagingService.class);

    @Autowired
    FirebaseMessaging firebaseMessaging;

    public String sendNotifications(NotificationMessagingFCM notifications) {

        String title = notifications.getTitle();
        String body = notifications.getBody();
        String image = notifications.getImage();
        List<String> recipientTokens = notifications.getRecipientToken();


        Notification notification = Notification.builder().setTitle(title).setBody(body).setImage(image).build();

        int batchSize = 500;
        int totalSuccess = 0;
        int totalFailure = 0;

        for (int i = 0; i < recipientTokens.size(); i += batchSize) {
            List<String> batchTokens = recipientTokens.subList(i, Math.min(i + batchSize, recipientTokens.size()));

            MulticastMessage message = MulticastMessage.builder()
                    .addAllTokens(batchTokens)
                    .setNotification(notification)
                    .build();

            try {
                BatchResponse response = firebaseMessaging.sendMulticast(message);
                totalSuccess += response.getSuccessCount();
                totalFailure += response.getFailureCount();

                if (response.getFailureCount() > 0) {
                    List<SendResponse> failures = response.getResponses();
                    for (int j = 0; j < failures.size(); j++) {
                        if (!failures.get(j).isSuccessful()) {
                            log.error("Failed to send notification to token: " + batchTokens.get(j) + " - "
                                    + failures.get(j).getException());
                        }
                    }
                }
            } catch (FirebaseMessagingException e) {
                log.error("Error sending notifications: " + e.getMessage(), e);
                return "Error Sending Notifications: " + e.getMessage();
            }
        }

        return "Success: " + totalSuccess + " messages were sent, Failures: " + totalFailure;
    }

}

解决方案

这个404错误通常由以下原因导致,对应解决步骤如下:

  1. Firebase Admin SDK版本过低
    旧版本的SDK可能使用已废弃的批量API端点,或者未支持/batch路径。将Firebase Admin SDK升级到最新稳定版本,比如在Maven中更新依赖:
<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>9.2.0</version> <!-- 替换为最新稳定版 -->
</dependency>
  1. Guava依赖冲突
    Firebase Admin SDK依赖特定版本的Guava,如果项目中引入的Guava版本与SDK要求的版本不一致,可能导致请求构造错误。通过mvn dependency:tree查看依赖树,排除冲突的Guava依赖,或者统一版本:
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version> <!-- 匹配Firebase SDK依赖的版本 -->
</dependency>
  1. Firebase初始化配置错误
    确认Firebase初始化时使用了正确的服务账号JSON文件,项目ID与Firebase控制台一致,没有自定义错误的API端点地址。初始化示例:
FirebaseOptions options = FirebaseOptions.builder()
        .setCredentials(GoogleCredentials.fromStream(new FileInputStream("path/to/serviceAccountKey.json")))
        .build();
FirebaseApp.initializeApp(options);
  1. 网络/代理限制
    检查服务器是否能正常访问Firebase的/batch端点(https://fcm.googleapis.com/batch),确保没有防火墙、代理或网络策略拦截该请求。可以通过curl命令测试连通性:
curl -v https://fcm.googleapis.com/batch

内容的提问来源于stack exchange,提问作者Alan Krishna K

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 23:19:51