返回 使用
使用 Fcm Server API 的
iOS多设备高效推送:FCM批量与主题订阅
IOS
2025-01-18 17:28:19
iOS 多设备推送:一种高效实现方案
移动应用经常需要向多个用户设备发送推送通知。这种需求,在例如社交网络、多人游戏等场景中尤为常见。 直接通过 Firebase V1 API 单独处理每个设备 token 的方式效率不高,特别是当设备数量很多的时候,这会造成延迟并且增加服务器负载。这里将探讨几种能够提升效率的方法。
使用 registration_ids
批量发送
Firebase Cloud Messaging (FCM) API 支持通过 registration_ids
参数批量发送通知。可以将多个设备 token 放在一个数组中,一次性发送到 FCM 服务器,无需循环调用。这样减少了网络请求次数,提高了推送效率。
代码示例 (JSON 请求体):
{
"message": {
"notification": {
"title": "通知标题",
"body": "通知内容"
},
"android": {
"priority": "high"
},
"apns":{
"headers": {
"apns-priority": "10"
}
},
"registration_ids": [
"设备Token1",
"设备Token2",
"设备Token3",
...
]
}
}
操作步骤:
- 构建一个包含推送信息的 JSON 对象。
- 在
message
对象中,使用registration_ids
键,值为设备 token 组成的数组。 - 通过 HTTP POST 请求发送到
https://fcm.googleapis.com/v1/projects/{your-project-id}/messages:send
, 其中{your-project-id}
需要替换为你的 firebase 项目 id。 - 确保在 HTTP 请求头中包含正确的
Authorization
和Content-Type
参数。Authorization
参数的值需要使用 firebase 的 Server Key.
重要提示:
registration_ids
数组的大小存在上限,需要注意,firebase 最大支持500个设备token, 需要分批发送。- 可以使用服务器端的循环语句或其他分批处理机制来处理超过上限数量的 token。
使用 Topic 主题订阅机制
另一种方法是利用 FCM 的主题 (Topic) 订阅功能。用户可以选择订阅特定的主题。当向一个主题发送通知时,所有订阅了该主题的设备都会收到通知。这种方法适合发送给同一群体的通知,如:应用内公告。
步骤如下:
- 客户端设备需使用
FirebaseMessaging.subscribe(toTopic:)
或等效 API 订阅相关的主题。例如:
Messaging.messaging().subscribe(toTopic: "news") { error in
if let error = error {
print("订阅主题失败: \(error)")
} else {
print("成功订阅主题: news")
}
}
- 发送通知时,不再需要传入 device token,而改为传入 topic 的名称。
代码示例 (JSON 请求体):
{
"message": {
"topic": "news",
"notification": {
"title": "新闻标题",
"body": "最新新闻报道"
},
"android": {
"priority": "high"
},
"apns":{
"headers": {
"apns-priority": "10"
}
}
}
}
操作步骤:
- 构建包含推送信息以及
topic
的 JSON 对象。 - 通过 HTTP POST 请求发送到 FCM 服务器。
- 像使用 registration_ids 方法一样,请注意 authorization 头。
安全建议:
- 对于敏感的通知,不要使用主题广播方式,以防信息泄露,应该使用目标设备令牌(device token)精准推送。
- 始终验证并处理 FCM 返回的结果,检测是否存在错误。例如检查是否有推送失败的情况,方便后续的故障排除或重发。
- 设备 Token 与 用户数据最好建立安全存储和维护的方案,避免信息泄漏风险。
使用 Fcm Server API 的 send
接口
firebase 允许通过server端的sdk 进行设备批量推送,使用这种方式可以将一些复杂度转移到server端进行处理。
from firebase_admin import credentials, messaging, initialize_app
cred = credentials.Certificate("path/to/your/serviceAccountKey.json")
initialize_app(cred)
def send_multiple_notifications(device_tokens, title, body):
message = messaging.MulticastMessage(
tokens=device_tokens,
notification=messaging.Notification(
title=title,
body=body,
),
android = messaging.AndroidConfig(
priority='high',
),
apns = messaging.APNSConfig(
headers = {"apns-priority":"10"}
)
)
response = messaging.send_multicast(message)
if response.failure_count > 0 :
print(f"send some push failure, failure count:{response.failure_count}")
print('Successfully sent message:', response)
#示例设备token,实际开发需要动态获取,
example_tokens = ['token1','token2']
send_multiple_notifications(example_tokens, "通知标题", "通知内容")
操作步骤:
- 需要在server端安装对应的 sdk。
- 创建
firebase_admin
初始化配置. - 调用
messaging.MulticastMessage
创建推送消息实体 - 通过
messaging.send_multicast
发送批量通知。
原理说明:
server sdk内部实现了将消息拆分为批量发送的过程,并封装好了与 firebase server 的交互, 可以简化多设备推送开发的过程。
总结来说,通过 registration_ids
批量发送或使用主题订阅机制,能够高效地向多个 iOS 设备发送推送通知,具体使用哪种方式取决于具体的需求和场景。 选择哪种策略时, 需要综合考量开发效率,通知及时性、以及安全性方面的考量。