iOS端FCM本地化推送消息格式错误排查求助
解决FCM iOS本地化推送的TypeScript类型错误
你遇到的问题核心是Firebase Admin SDK的TypeScript类型定义对ApsAlert的属性采用了驼峰命名,而非Apple原生的下划线格式。虽然Apple官方文档里用的是loc_key和loc_args,但FCM的TypeScript接口要求你用驼峰式命名来匹配类型校验规则。
修正后的代码
把alert对象里的属性名改成驼峰式就能直接解决类型错误:
async function sendLocationLostNotification(notificationToken: string, newOwner: string, locationName: string) { const message = { name: "Location Lost Notification", token: notificationToken, apns: { payload: { aps: { alert: { // 将loc_key改为locKey,loc_args改为locArgs locKey: "location-lost-notification-body", locArgs: [newOwner, locationName] }, sound: "default" } } } }; return await admin.messaging().send(message) }
问题根源说明
Firebase Admin SDK的TypeScript类型定义(ApsAlert接口)将Apple原生的下划线风格属性映射为了驼峰式:
- Apple原生字段
loc_key→ FCM TypeScript对应locKey - Apple原生字段
loc_args→ FCM TypeScript对应locArgs - 同理,标题本地化的
title_loc_key对应titleLocKey,title_loc_args对应titleLocArgs
额外注意事项
- 请确保iOS项目的
Localizable.strings文件中已配置好对应的本地化字符串,示例:"location-lost-notification-body" = "%@ 接管了 %@"; - 如果需要添加标题的本地化,可以扩展alert对象:
alert: { titleLocKey: "location-lost-notification-title", titleLocArgs: [locationName], locKey: "location-lost-notification-body", locArgs: [newOwner, locationName] }
内容的提问来源于stack exchange,提问作者Angeria
相关产品推荐
相关产品推荐

