为移动应用设置推送通知(FCM V1 和 APNs)
为移动应用设置推送通知(FCM V1 和 APNs)
本指南介绍如何将 SMSBAT 推送通知集成到您的 iOS 和 Android 应用中。
阶段 1:Firebase 项目和服务账号(FCM V1)
SMSBAT 在 Android 和 iOS 上都通过 FCM V1 发送推送。这意味着您只需向 SMSBAT 上传 一个文件:Firebase 服务账号。APNs 密钥从不上传到 SMSBAT — Firebase 需要它,我们不需要(见阶段 3)。
1. 从 Firebase 获取服务账号
- Firebase 控制台 → 项目设置 → 服务账号标签页。
- 生成新的私钥 → 确认 → 会下载一个
.json文件。
这与 google-services.json 不是同一个文件
google-services.json 和 GoogleService-Info.plist 随应用一起发布。
服务账号是私有的服务器密钥,仅应放在 SMSBAT 中。
切勿将其放入应用代码、代码仓库或聊天消息中。
2. 在 SMSBAT Omni 中创建集成
- 打开集成,并从左侧列表中选择 Firebase。
- 点击创建。
- 填写两个字段:
- 名称 — 任何有助于您在列表中识别该应用的标签
(for example,
Omnichannel Contact Center). - 服务账号 JSON — 点击选择并选择已下载的文件。
- 名称 — 任何有助于您在列表中识别该应用的标签
(for example,
- 点击创建。
这里没有包名或 Bundle ID 字段 — SMSBAT 会从文件本身读取 项目 ID,并显示在表格中。
3. 检查集成是否已就绪
会出现一行,包含名称、项目 ID、状态和创建时间。 正常工作状态为活跃。
行末的按钮:
| 按钮 | 操作 |
|---|---|
| 铅笔(蓝色) | 更改名称或替换服务账号文件 |
| 刷新(绿色) | 重新读取密钥并重新检查对 FCM 的访问权限 |
| 垃圾桶(红色) | 删除集成 |
项目 ID 必须与您应用中的一致
将表格中的项目 ID 与 google-services.json(Android)和
GoogleService-Info.plist(iOS)中的 project_id 进行比较。如果
两者不同,说明应用注册在一个 Firebase 项目中,而 SMSBAT 却发送到
另一个项目。令牌看起来有效,但什么都收不到,也不会
返回任何错误。这是导致“一切都已配置好,却
什么都收不到”的最常见原因。
如果您在 Firebase 中轮换了密钥,请通过铅笔按钮上传新文件, 然后点击绿色按钮。已撤销的密钥会静默失效。
4. 应用密钥
应用向 SMSBAT Push API 发出的请求,使用在 Omni 面板中为您的应用 签发的应用密钥进行认证,并通过请求头传递:
X-Push-App-Key: <application key>
完整的请求参考请参阅 Push API 概览。
阶段 2:Android(Kotlin / Java)
1. 依赖项
在您模块的 build.gradle 中:
dependencies {
implementation 'com.google.firebase:firebase-messaging-ktx:23.4.0'
implementation 'com.google.firebase:firebase-installations-ktx:17.2.0'
}
2. 注册 Firebase Installation ID(FID)
当用户登录或应用启动时,读取 FID 并将其发送给 SMSBAT:
FirebaseInstallations.getInstance().id.addOnCompleteListener { task ->
if (task.isSuccessful) {
val fid = task.result
registerInstallationOnSmsbat(fid, currentUserId)
}
}
fun registerInstallationOnSmsbat(fid: String, userId: String) {
val json = JSONObject().apply {
put("externalUserId", userId)
put("firebaseInstallationId", fid)
put("platform", "android")
put("notificationsEnabled", true)
}
// HTTP POST to https://restapi.smsbat.com/api/push/registerInstallation
// Header: X-Push-App-Key = YOUR_PUSH_APP_KEY
}
3. 处理推送并上报 delivered
在您的 FirebaseMessagingService 中:
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
val data = remoteMessage.data
val notificationGuid = data["guid"]
if (!notificationGuid.isNullOrEmpty()) {
// 1. Confirm delivery so SMSBAT stops the cascade
sendDeliveryStatus(notificationGuid, "delivered")
}
// 2. Draw the local notification yourself (NotificationManager)
showNotification(data["title"], data["body"], data)
}
private fun sendDeliveryStatus(guid: String, status: String) {
// HTTP POST to https://restapi.smsbat.com/api/push/notifications/$guid/status
// Header: X-Push-App-Key = YOUR_PUSH_APP_KEY
// Body: { "status": "delivered", "occurredAt": "2026-08-07T12:00:00Z" }
}
}
为什么要自己绘制横幅
SMSBAT 发送仅数据消息,以便应用能够上报 delivered,而无需
等待用户打开任何内容。系统不会为仅数据
消息显示横幅——必须由应用自己完成。
阶段 3:iOS(Swift / APNs)
在 iOS 上,APNs 密钥上传到 Firebase,而不是 SMSBAT:Firebase 控制台 → Cloud Messaging → APNs 身份验证密钥。随后 FCM V1 会代您向 Apple 投递。
1. 添加 Notification Service Extension
为了确认送达并处理富内容(图片、声音、按钮),请在 Xcode 中添加 Notification Service Extension 目标:
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
let userInfo = bestAttemptContent.userInfo
if let guid = userInfo["guid"] as? String {
// Report delivered to SMSBAT
sendPushStatus(guid: guid, status: "delivered")
}
contentHandler(bestAttemptContent)
}
}
}
2. 处理点击(seen)
在您的 AppDelegate 或 SceneDelegate 中:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let guid = userInfo["guid"] as? String {
// Report seen to SMSBAT
sendPushStatus(guid: guid, status: "seen")
}
completionHandler()
}
结果
集成完成后,应用会即时收到通知,一旦推送送达,级联即会 立即停止,因此付费渠道不会被计费,您还可以获得准确的 送达和已读统计数据。
如果有消息未送达,请查阅 推送故障排除 — 它 涵盖了那些完全不产生错误的故障。