FCM HTTPS V1网络超时问题:请求超时10000ms
问题分析与解决方案
升级FCM HTTPS v1 API后,添加apns配置触发推送时出现10秒超时,但仅使用notification字段推送正常,核心排查方向如下:
1. APNs payload格式不符合FCM v1规范
FCM v1对APNs payload的结构要求更严格,冗余或错误的嵌套会导致FCM转发请求到苹果服务器时处理延迟。你当前的apns配置重复定义了alert内容(顶层notification已声明),尝试简化结构:
"apns": { "payload": { "aps": { "sound": "default", "badge": 1 } } }
若需自定义APNs的alert内容,避免与顶层notification重复,或直接移除顶层notification,完全通过apns.payload.aps定义。
2. 默认超时时间不足以处理APNs请求
带APNs特定配置的请求需要额外与苹果服务器交互,默认10秒超时可能不够。可调整Firebase Admin的HTTP请求超时:
const admin = require('firebase-admin'); const https = require('https'); admin.initializeApp({ credential: admin.credential.applicationDefault(), httpAgent: new https.Agent({ timeout: 30000 }) // 调整为30秒 });
或在调用send方法时单独设置:
admin.messaging().send(message, { timeout: 30000 });
3. 云函数资源配置不足
默认256MB内存可能导致复杂请求处理缓慢触发超时。尝试提升云函数内存配置(如512MB/1GB),同时在控制台将云函数超时时间调至最大值90秒。
4. APNs证书/密钥有效性问题
无效的APNs证书或密钥会导致FCM与苹果服务器的请求卡住,间接引发超时。检查Firebase控制台项目设置 > 云消息传递 > Apple应用配置,确保证书/密钥有效且包含推送通知权限。
5. 异步操作未正确返回Promise
云函数触发器(如Firestore onCreate)必须等待推送操作完成,否则会提前终止导致超时。修改代码确保返回Promise:
function sendToDeviceWithPayload(token, payload, options) { const message = { "notification": { "title": payload.notification.title, "body": payload.notification.body }, "apns": { "payload": { "aps": { "sound": "default", "badge": 1 } } }, "token": token }; // 返回Promise确保云函数等待推送完成 return admin.messaging().send(message) .then(response => { console.log("Push notification sent succesfully to:", token, response); return response; }) .catch(e => { console.log("Error sending payload to token:", token, e); if (e.code === 'messaging/invalid-registration-token' || e.code === 'messaging/registration-token-not-registered') { return db.collection(TABLE_FCM).doc(token).delete() .then(() => console.log("Deleted token.")); } throw e; // 抛出错误让云函数捕获 }); } // 触发器函数需返回Promise exports.sendPushOnPostFirestore = functions.firestore .document('posts/{postId}') .onCreate((snap, context) => { // 业务逻辑处理... return sendToDeviceWithPayload(token, payload, options); });
内容的提问来源于stack exchange,提问作者TM Lynch
相关产品推荐
相关产品推荐

