如何通过单次调用FCM API向多个用户发送不同消息?批量高频个性化推送场景咨询
Great question—this is a super common pain point when you need personalized, dynamic FCM notifications at scale. Let’s break down the best solution that fits all your constraints:
Why This Works
FCM's HTTP v1 API supports batch requests, which let you pack multiple independent FCM messages (each with unique content and target tokens) into a single HTTP call. This cuts your API requests from 1000 per minute to just 2 (since FCM caps batch requests at 500 sub-requests each) — way more efficient, and perfect for your dynamic user groups and personalized content needs.
Step-by-Step Implementation
1. Prepare Your Batch Request Structure
Each batch request uses a multipart/mixed format with a unique boundary string to separate individual FCM message requests. Here’s a simplified example:
POST /batch HTTP/1.1 Host: fcm.googleapis.com Authorization: Bearer <YOUR_SERVICE_ACCOUNT_ACCESS_TOKEN> Content-Type: multipart/mixed; boundary=unique_batch_boundary_123 --unique_batch_boundary_123 Content-Type: application/http Content-Transfer-Encoding: binary POST /v1/projects/your-firebase-project-id/messages:send HTTP/1.1 Content-Type: application/json { "message": { "token": "user_1_fcm_token", "notification": { "title": "Hey User 1!", "body": "Your personalized update: Your order #123 is on its way!" }, "data": { "order_id": "123", "tracking_url": "app://track/123" } } } --unique_batch_boundary_123 Content-Type: application/http Content-Transfer-Encoding: binary POST /v1/projects/your-firebase-project-id/messages:send HTTP/1.1 Content-Type: application/json { "message": { "token": "user_2_fcm_token", "notification": { "title": "Hi User 2!", "body": "Your personalized update: New discount just for you!" }, "data": { "discount_code": "USER2_SAVE20" } } } --unique_batch_boundary_123--
2. Handle Batch Limits & Splitting
- FCM allows a maximum of 500 sub-requests per batch. For 1000 users, split your target list into two separate batch requests each minute.
- Generate a fresh boundary string for each batch to avoid parsing errors.
3. Key Optimizations & Notes
- Dynamic User Filtering: Each minute, run your logic to fetch the 1000 target users, then generate a personalized message payload for each user’s token.
- Access Token Management: Use a service account to generate OAuth2 access tokens (valid for 1 hour) — cache these tokens to avoid re-generating them for every batch request.
- Error Handling: Batch responses include results for each sub-request. Parse the response to catch failures (e.g., invalid tokens, quota issues) and implement retry logic for transient errors.
- Quota Compliance: Batch requests don’t bypass FCM’s sending quotas, but they reduce HTTP overhead significantly, making your workflow more efficient.
Why Other Options Don’t Fit
- Topics/Groups: These require static user groups, which doesn’t work for your dynamic, minute-by-minute user selection.
- Single API Calls: 1000 calls per minute is inefficient, increases latency, and raises the risk of hitting rate limits.
内容的提问来源于stack exchange,提问作者Dhruv Pahuja

