ตั้งค่าแอปมือถือสำหรับการแจ้งเตือนแบบพุช (FCM V1 และ APNs)
ตั้งค่าแอปมือถือสำหรับการแจ้งเตือนแบบพุช (FCM V1 และ APNs)
คู่มือนี้ครอบคลุมการเชื่อมต่อการแจ้งเตือนแบบพุชของ SMSBAT เข้ากับแอป iOS และ Android ของคุณ
ขั้นตอนที่ 1. โปรเจกต์ Firebase และ service account (FCM V1)
SMSBAT ส่งพุชผ่าน FCM V1 ทั้งบน Android และ iOS ซึ่งหมายความว่าคุณอัปโหลด ไฟล์เดียวเท่านั้นให้ SMSBAT: Firebase service account คีย์ APNs ไม่เคยถูก อัปโหลดไปยัง SMSBAT — Firebase ต้องใช้ แต่เราไม่ต้องใช้ (ดูขั้นตอนที่ 3)
1. รับ service account จาก Firebase
- Firebase Console → Project Settings → แท็บ Service accounts
- Generate new private key → ยืนยัน → ไฟล์
.jsonจะถูกดาวน์โหลด
ไฟล์นี้ไม่ใช่ไฟล์เดียวกับ google-services.json
google-services.json และ GoogleService-Info.plist จัดส่ง ภายในแอป
service account เป็นคีย์ส่วนตัวของเซิร์ฟเวอร์และต้องอยู่ ใน SMSBAT เท่านั้น
ห้ามใส่ในโค้ดแอป รีโพซิทอรี หรือข้อความแชทเด็ดขาด
2. สร้างการเชื่อมต่อใน SMSBAT Omni
- เปิด การเชื่อมต่อ และเลือก Firebase จากรายการทางด้านซ้าย
- คลิก สร้าง
- กรอกสองฟิลด์:
- ชื่อ — ป้ายกำกับใดก็ได้ที่ช่วยให้คุณจำแอปในรายการได้
(for example,
Omnichannel Contact Center). - Service account JSON — คลิก เลือก แล้วเลือกไฟล์ที่ดาวน์โหลดมา
- ชื่อ — ป้ายกำกับใดก็ได้ที่ช่วยให้คุณจำแอปในรายการได้
(for example,
- คลิก สร้าง
ที่นี่ไม่มีฟิลด์สำหรับชื่อแพ็กเกจหรือ bundle ID — SMSBAT อ่าน Project ID จากตัวไฟล์เองและแสดงในตาราง
3. ตรวจสอบว่าการเชื่อมต่อทำงาน
แถวจะปรากฏพร้อม ชื่อ, Project ID, สถานะ และ สร้างเมื่อ สถานะที่ทำงานปกติคือ ใช้งานอยู่
ปุ่มที่ท้ายแถว:
| ปุ่ม | การดำเนินการ |
|---|---|
| ดินสอ (สีน้ำเงิน) | เปลี่ยนชื่อหรือเปลี่ยนไฟล์ service account |
| รีเฟรช (สีเขียว) | อ่านคีย์ใหม่และตรวจสอบการเข้าถึง FCM อีกครั้ง |
| ถังขยะ (สีแดง) | ลบการเชื่อมต่อ |
Project ID ต้องตรงกับในแอปของคุณ
เปรียบเทียบ Project ID ในตารางกับ project_id ใน
google-services.json (Android) และ GoogleService-Info.plist (iOS) หาก
ต่างกัน แสดงว่าแอปลงทะเบียนในโปรเจกต์ Firebase หนึ่งขณะที่ SMSBAT ส่งไปยัง
อีกโปรเจกต์หนึ่ง โทเคนจะดูถูกต้อง ไม่มีอะไรมาถึง และจะไม่มีข้อผิดพลาด
ส่งคืน นี่เป็นสาเหตุที่พบบ่อยที่สุดของ “ตั้งค่าทุกอย่างแล้วแต่
ไม่มีอะไรมาถึง”
หากคุณหมุนเวียนคีย์ใน Firebase ให้อัปโหลดไฟล์ใหม่ผ่านปุ่มดินสอ แล้วกดปุ่มสีเขียว คีย์ที่ถูกเพิกถอนจะหยุดทำงานโดยไม่แจ้งเตือน
4. Application key
คำขอจากแอปไปยัง SMSBAT Push API ยืนยันตัวตนด้วย application key ที่ออกให้สำหรับแอปของคุณในแผงควบคุม Omni โดยส่งเป็นเฮดเดอร์:
X-Push-App-Key: <application key>
ดูเอกสารอ้างอิงคำขอฉบับเต็มในภาพรวม Push API
ขั้นตอนที่ 2. Android (Kotlin / Java)
1. Dependencies
ใน 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 ส่งข้อความแบบ data-only เพื่อให้แอปรายงาน delivered ได้ก่อนที่
ผู้ใช้จะเปิดสิ่งใด ระบบไม่แสดงแบนเนอร์สำหรับ data-only
ข้อความ — แอปต้องดำเนินการเอง
ขั้นตอนที่ 3. iOS (Swift / APNs)
บน iOS คีย์ APNs จะถูกอัปโหลดไปยัง Firebase ไม่ใช่ SMSBAT: Firebase Console → Cloud Messaging → APNs Authentication Key จากนั้น FCM V1 จะส่งถึง Apple แทนคุณ
1. เพิ่ม Notification Service Extension
เพื่อยืนยันการส่งและจัดการเนื้อหามัลติมีเดีย (รูปภาพ เสียง ปุ่ม) ให้เพิ่ม เป้าหมาย Notification Service Extension ใน Xcode:
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()
}
ผลลัพธ์
เมื่อเชื่อมต่อเสร็จสิ้น แอปจะได้รับการแจ้งเตือนทันที cascade จะหยุดทันทีที่ พุชส่งถึง ช่องทางแบบชำระเงินจึงไม่ถูกเรียกเก็บเงิน และคุณจะได้สถิติ การส่งและการอ่านที่แม่นยำ
หากมีบางอย่างไม่มาถึง ให้ศึกษา การแก้ไขปัญหาพุช — ซึ่ง ครอบคลุมความล้มเหลวที่ไม่มีข้อผิดพลาดใดๆ เลย