一個 Google 提供的 免費 Pub/Sub 服務,可支援推播訊息至 iOS、Android、Web、Unity、Flutter
- Maximum message rate to a single device
- 每分鐘 240 則, 每小時 5,000 則
- Upstream message
- 單個專案: 每分鐘 1,500,000 則
- 單台設備: 每分鐘 1,000 則
- Topic message
- 訂閱與退訂: 3,000 QPS
- 使用 Google 帳號登入
- 開新專案
- 新增應用程式 (可選 iOS、Android、Web、Unity、Flutter)
- 跟著指引跑完 SDK 設定和配置即可往下
- 找到專案總覽 > 齒輪 > 專案設定 > 服務帳戶
- 選擇開發用的語言 (這邊是使用 Go) > 產生新的私密金鑰
- 將這份 JSON 載下來並取名成
service-account-key.json
放到 server 根目錄
- Source Code
- 需要先修改的地方
- Client
client/.env
-> REACT_APP_VAPID_KEY (上面取得的 PUBLIC_KEY)client/public/firebase-messaging-sw.js
-> configclient/src/firebaseInit.js
-> config
- Server
server/main.go
-> registrationToken
- Client
- Client:
yarn start
- Server:
go run main.go
- registration token 是由 client 提供,一般在 client 取得後會傳至 server 儲存起來供 server 發送訊息時取用
- FCM 會透過這個 token 找到 client,並傳送我們預期的資料給它
- 此 token 可能會過期,需要確保 server 持有有效的 registration token
- https://firebase.google.com/docs/cloud-messaging/manage-tokens