技术咨询:我当前使用的是FCM Legacy API还是HTTP v1 API?
确认Firebase Admin SDK使用的FCM API版本
代码示例
if (hisFcmToken) { // Send preload silent notification const dataNotification= { data: { name: "Some name", married : 'true' }, token: hisFcmToken }; try { await admin.messaging().send(dataNotification); console.log(`FCM data notification sent successfully to user ${document.id}`); } catch (error) { console.error(`Failed to send FCM data notification to user ${document.id}: ${error}`); } if (someCondition) { // Send user joined notification const userJoinedNotification = { notification: { title: `${newUserName} just joined `, image: someImageurl }, data: { newUserJoinNotification: 'true' }, android: { priority: "high" }, apns: { headers: { 'apns-priority': '10' } }, token: hisFcmToken }; await admin.messaging().send(userJoinedNotification) .then((response) => { console.log('Successfully sent user join notification:', response); }) .catch((error) => { console.log('Error sending user join notification:', error); if (error.code === 'messaging/registration-token-not-registered') { // Remove the invalid token from your database } }); }
问题描述
由于需在2024年6月20日前迁移至Firebase Cloud Messaging API(HTTP v1),我对当前使用的是Cloud Messaging API(Legacy)还是Firebase Cloud Messaging API(V1)存在困惑。文档说明:
HTTP v1发送请求需OAuth 2.0访问令牌,替代旧版请求使用的服务器密钥字符串;若使用Admin SDK发送消息,库会自动处理令牌。
但我仍不确定当前使用的API版本,特此咨询。
解答
你当前使用的Firebase Admin SDK已经在使用FCM HTTP v1 API,不需要额外迁移,原因如下:
- Firebase Admin SDK默认基于FCM HTTP v1 API构建,会自动处理OAuth 2.0令牌的获取与刷新,完全符合v1 API的要求。
- 只有直接调用Legacy HTTP API(例如用服务器密钥请求旧版接口)或使用旧版FCM服务器库的场景,才需要迁移至v1 API。
- 你代码中调用的
admin.messaging().send()方法,正是Admin SDK封装的v1 API调用,无需修改即可满足2024年6月20日的迁移要求。
另外,代码里存在一处语法错误:if (someCondition))多了一个闭合括号,需修正为if (someCondition)。
内容的提问来源于stack exchange,提问作者Kobita
相关产品推荐
相关产品推荐

