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

我的Android应用无法接收通过REST调用发送的FCM通知

Troubleshooting FCM REST API Notification Delivery Issues

Since you can receive notifications via the Firebase Console but not through your REST call, the issue is almost certainly tied to how your REST request is structured or authenticated. Let’s walk through the most common fixes and checks to resolve this:

1. Validate the Authentication Header

FCM’s REST API requires a valid server key in the authorization header. Double-check that your request includes this exact line:

Authorization: key=YOUR_SERVER_KEY
  • You can grab your server key from the Firebase Console under Project Settings > Cloud Messaging > Server key.
  • Even a single typo or extra space in the key will cause authentication failures—copy it directly to avoid mistakes.

2. Fix the Request Body Structure

Topic messaging has specific formatting rules for the JSON body. A valid example looks like this (adjust values to match your use case):

{
  "to": "/topics/YOUR_TOPIC_NAME",
  "notification": {
    "title": "Test REST Notification",
    "body": "This is a message sent via FCM REST API"
  },
  "data": {
    "custom_field": "custom_value"
  }
}
  • Ensure you’re using the correct to format for topics (/topics/[your-topic])—not a device token.
  • If your app relies on data payloads, include the data object (some client implementations require both notification and data to trigger a visible alert).
  • Use a JSON validator to check for syntax errors like missing commas or unclosed quotes—invalid JSON will be silently rejected.

3. Confirm the API Endpoint

Make sure you’re sending the request to the correct FCM endpoint:

POST https://fcm.googleapis.com/fcm/send

Using legacy endpoints (like gcm.googleapis.com) will result in delivery failures.

4. Inspect the REST Response

FCM returns detailed error messages in the response body that pinpoint issues. Common errors to look for:

  • InvalidRegistration: The topic name is misspelled or the client isn’t subscribed to it.
  • MissingRegistration: The to field is missing or empty.
  • AuthenticationError: Your server key is invalid or missing from the header.
    If you’re still stuck, sharing the full response will help narrow down the problem instantly.

5. Verify Client Subscription Status

Even though console notifications work, confirm your Android client is properly subscribed to the topic. Check logcat for success messages after calling FirebaseMessaging.getInstance().subscribeToTopic("YOUR_TOPIC")—subscription failures here will block REST-delivered messages.

6. Include the Content-Type Header

Your request must have this header to ensure FCM parses the JSON correctly:

Content-Type: application/json

Omitting this often leads to silent failures, even if the body is valid.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 06:42:44