Firebase SendAllAsync返回404错误,SendAsync却正常工作
.NET中Firebase Admin SDK的SendAllAsync返回404 Not Found问题
在.NET应用中使用Firebase Admin SDK通过FCM发送推送通知时,SendAsync可以正常运行,但批量发送的SendAllAsync始终返回404 Not Found错误,即使令牌有效、消息结构正确,错误信息如下:
Unknown error while making a remote service call: Response status code does not indicate success: 404 (Not Found).
相关代码如下:
public async Task SendNotificationWithJob(List<NotificationDto> input, string url = "") { var data = new Dictionary<string, string>() { { "click_action", "FLUTTER_NOTIFICATION_CLICK" }, { "url", $"{url}" } }; string type = WebhookAppService.NOTIFCATION_TYPE_SUCCESS; var notificationTitle = input[0].Title; var notificationMessage = input[0].Body; List<Message> messages = new List<Message>(); foreach (var notificationDto in input) { if (!string.IsNullOrEmpty(notificationDto.FirebaseToken)) { var messageObj = new Message() { Notification = new FirebaseAdmin.Messaging.Notification() { Title = notificationTitle, Body = notificationMessage, }, Android = new AndroidConfig() { Notification = new AndroidNotification() { Title = notificationTitle, Body = notificationMessage, Sound = "default" } }, Apns = new ApnsConfig() { Aps = new Aps() { Alert = new ApsAlert() { Title = notificationTitle, Body = notificationMessage, }, Sound = "default" } }, Data = data, Token = notificationDto.FirebaseToken, }; messages.Add(messageObj); } } if (messages.Count == 0) { Logger.Warn("No valid tokens found for sending notifications."); return; } int batchSize = 100; int totalBatches = (messages.Count + batchSize - 1) / batchSize; for (int i = 0; i < totalBatches; i++) { int startIndex = i * batchSize; int length = Math.Min(batchSize, messages.Count - startIndex); var msgs = messages.GetRange(startIndex, length); try { Logger.Debug($"Sending request to Firebase with payload: {JsonConvert.SerializeObject(msgs, Formatting.Indented)}"); var response = await FirebaseMessaging.DefaultInstance.SendAllAsync(msgs); Logger.Debug($"SendAllAsync Response: SuccessCount={response.SuccessCount}, FailureCount={response.FailureCount}"); } catch (FirebaseMessagingException ex) { Logger.Error($"FirebaseMessagingException: {ex.Message}"); } catch (Exception ex) { Logger.Error($"Exception: {ex.Message}"); } } }
可能的解决方案
- 升级Firebase Admin SDK版本:部分旧版本的SDK中
SendAllAsync存在端点URL配置错误,通过NuGet将FirebaseAdmin包更新到最新稳定版,修复已知的API端点问题。 - 验证服务账号权限:确保你的服务账号已启用Firebase Cloud Messaging API,并且拥有
Cloud Messaging Admin或Editor角色,避免因权限不足导致端点访问被拒绝(表现为404)。 - 检查网络/代理配置:
SendAllAsync调用的是FCM批量发送端点,而SendAsync使用的是单条发送端点,若应用在代理环境下运行,需确认代理规则允许访问该批量端点。 - 确认服务账号密钥有效性:检查服务账号密钥是否过期、是否关联正确的Firebase项目,密钥配置错误可能导致SDK无法正确解析API端点地址。
- 排查批次消息内容:虽然代码中已处理空列表,但可在批次发送前添加日志,确认当前批次的
msgs集合不为空,且每条消息的Token格式正确。
内容的提问来源于stack exchange,提问作者Батсүрэн
相关产品推荐
相关产品推荐

