API Survey #5
Firebase Cloud Messaging
@author Cara Wang <caraw@cnyes.com>
@since 2017/02/21
1
前情提要一下
我是 Cara Wang, 任職於 鉅亨網 cnYES
cnYES 的 API Team 每個月會分享一個 Public API 的使用方式
實作一個小玩具
分享 API 的使用方式
與覺得該 API 設計好的地方以及可能可以更好的地方
2
What is firebase cloud messaging?
整合好的推播機制: Web / iOS / Android
流程:
3
推播流程
4
What is firebase cloud messaging?
整合好的推播機制: Web / iOS / Android
以前叫做 GCM (Google Cloud Messaging)
Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the
reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn
more. If you are integrating messaging in a new app, start with FCM. GCM users
are strongly recommended to upgrade to FCM, in order to benefit from new FCM
features today and in the future.
“
5
API
6
"notification"
"to"
API: PUSH Notification
POST https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key={Your Auth}
{
: {
"title": "推播標題",
"body": "推播內文",
"icon": "推播小圖",
"click_action": "推播動作"
},
"data": {自訂內容},
: "iid token"
}
01.
02.
03.
04.05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
7
WebNotification:瀏覽器支援度
8
APIResponse-Header
9
APIResponse-JsonBody
10
FCMDownstream
11
FCMUpstream
12
IID_TOKEN TOPIC_NAME
API: Topic Registration
註冊特定議題
例如: 使用者對不同新聞類別感興趣
POST https://iid.googleapis.com/iid/v1/ /rel/topics/
Content-Length: 0
Authorization:key={Your Auth}
IID_TOKEN: instance id
TOPIC_NAME: 自訂的議題名稱
01.
02.
03.
13
FCMUpstream
14
"/topics/{議題名稱}"
API: PUSH Notification
推播給特定議題
POST https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key={Your Auth}
{
"notification": {
"title": "推播標題",
"body": "推播內文",
"icon": "推播小圖",
"click_action": "推播動作"
},
"to":
01.
02.
03.
04.05.
06.
07.
08.
09.
10.
11.
12.
15
API: Topic Registration
取得單一 IID 的資訊
GET https://iid.googleapis.com/iid/info/IID_TOKEN
Content-Type:application/json
Authorization:key={Your Auth}
Query String 帶入 details=true 才可以拿到這個 iid 註冊的議題有哪些
01.
02.
03.
16
GetIIDinfo-Header
17
GetIIDinfo-Body
18
RESTful API ?
POST https://fcm.googleapis.com/fcm/send
HTTP Method: 用 POST 建立 資料
Resource Name: send 是動詞, 不符合 RESTful 希望 Resource Name 要
是名詞的期望
Response Payload: 成功時的格式一致, 但失敗時是 HTML 格式, 不符合
Interface / Uniform Contract
19
HTTPStatusCode
20
RESTful API ?
POST https://fcm.googleapis.com/fcm/send
HTTP Method: 用 POST 建立 資料
Resource Name: send 是動詞, 不符合 RESTful 希望 Resource Name 要
是名詞的期望
Response Payload: 成功時的格式一致, 但失敗時是 HTML 格式, 不符合
Interface / Uniform Contract
HTTP Status Code: 200 是表示 FCM 收到資料, 並非成功傳給使用者
21
HTTPStatusCode
22
RESTful API ?
POST https://iid.googleapis.com/iid/v1/ IID_TOKEN /rel/topics/ TOPIC_NAME
GET https://iid.googleapis.com/iid/info/IID_TOKEN
HTTP Method: 用 POST 建立 資料, 用 GET 取得資料
Resource Name: 資源都是名詞, 且有分層關係, 但是 POST 有版本而 GET
沒有
Response Payload: 成功時的格式不一致
23
POSTAPI沒有ResponseBody
24
GETAPI則有ResponseBody
25
RESTful API ?
POST https://iid.googleapis.com/iid/v1/ IID_TOKEN /rel/topics/ TOPIC_NAME
GET https://iid.googleapis.com/iid/info/IID_TOKEN
HTTP Method: 用 POST 建立 資料, 用 GET 取得資料
Resource Name: 資源都是名詞, 且有分層關係, 但是 POST 有版本而 GET
沒有
Response Payload: 成功時的格式不一致, 失敗時有時是 HTML 有時是
Json, 格式也不一致
26
POSTAPI沒有帶入content-length的Header錯誤
27
GETAPI給錯誤的IID
28
GETAPI沒有帶入Auth在Header
29
RESTful API ?
POST https://iid.googleapis.com/iid/v1/ IID_TOKEN /rel/topics/ TOPIC_NAME
GET https://iid.googleapis.com/iid/info/IID_TOKEN
HTTP Method: 用 POST 建立 資料, 用 GET 取得資料
Resource Name: 資源都是名詞, 且有分層關係, 但是 POST 有版本而 GET
沒有
Response Payload: 成功時的格式不一致, 但失敗時是 HTML 格式是一致
的
HTTP Status Code:
30
HTTPStatusCode
31
Pros and Cons
1. Pros
1. 有一致的 HTTP Status Code 定義
2. 發生錯誤時的 API Response 若是 Json 格式, 使用字串代號而不是數字
代碼: ex: "error":"InvalidToken"
32
Pros and Cons
1. Cons
1. 沒有一致的 Response 格式, 造成串接上要依據不同 API 做個別的處理
2. 甚至有 HTML 回傳格式, 無法做解析
3. 使用 IID_TOKEN 在 Path 上, 造成 API Path 非常的長
33
Q & A
34
2015/09 ~ 2016/12
35
2016/03
36
37
38
2016/05
39
40
41
2016/08
42
2016/12
43
如果你對我們有興趣,歡迎來找彩蛋
44
45

Api survey #5 - firebase cloud messaging

  • 1.
    API Survey #5 FirebaseCloud Messaging @author Cara Wang <caraw@cnyes.com> @since 2017/02/21 1
  • 2.
    前情提要一下 我是 Cara Wang,任職於 鉅亨網 cnYES cnYES 的 API Team 每個月會分享一個 Public API 的使用方式 實作一個小玩具 分享 API 的使用方式 與覺得該 API 設計好的地方以及可能可以更好的地方 2
  • 3.
    What is firebasecloud messaging? 整合好的推播機制: Web / iOS / Android 流程: 3
  • 4.
  • 5.
    What is firebasecloud messaging? 整合好的推播機制: Web / iOS / Android 以前叫做 GCM (Google Cloud Messaging) Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future. “ 5
  • 6.
  • 7.
    "notification" "to" API: PUSH Notification POSThttps://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key={Your Auth} { : { "title": "推播標題", "body": "推播內文", "icon": "推播小圖", "click_action": "推播動作" }, "data": {自訂內容}, : "iid token" } 01. 02. 03. 04.05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 7
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    IID_TOKEN TOPIC_NAME API: TopicRegistration 註冊特定議題 例如: 使用者對不同新聞類別感興趣 POST https://iid.googleapis.com/iid/v1/ /rel/topics/ Content-Length: 0 Authorization:key={Your Auth} IID_TOKEN: instance id TOPIC_NAME: 自訂的議題名稱 01. 02. 03. 13
  • 14.
  • 15.
    "/topics/{議題名稱}" API: PUSH Notification 推播給特定議題 POSThttps://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key={Your Auth} { "notification": { "title": "推播標題", "body": "推播內文", "icon": "推播小圖", "click_action": "推播動作" }, "to": 01. 02. 03. 04.05. 06. 07. 08. 09. 10. 11. 12. 15
  • 16.
    API: Topic Registration 取得單一IID 的資訊 GET https://iid.googleapis.com/iid/info/IID_TOKEN Content-Type:application/json Authorization:key={Your Auth} Query String 帶入 details=true 才可以拿到這個 iid 註冊的議題有哪些 01. 02. 03. 16
  • 17.
  • 18.
  • 19.
    RESTful API ? POSThttps://fcm.googleapis.com/fcm/send HTTP Method: 用 POST 建立 資料 Resource Name: send 是動詞, 不符合 RESTful 希望 Resource Name 要 是名詞的期望 Response Payload: 成功時的格式一致, 但失敗時是 HTML 格式, 不符合 Interface / Uniform Contract 19
  • 20.
  • 21.
    RESTful API ? POSThttps://fcm.googleapis.com/fcm/send HTTP Method: 用 POST 建立 資料 Resource Name: send 是動詞, 不符合 RESTful 希望 Resource Name 要 是名詞的期望 Response Payload: 成功時的格式一致, 但失敗時是 HTML 格式, 不符合 Interface / Uniform Contract HTTP Status Code: 200 是表示 FCM 收到資料, 並非成功傳給使用者 21
  • 22.
  • 23.
    RESTful API ? POSThttps://iid.googleapis.com/iid/v1/ IID_TOKEN /rel/topics/ TOPIC_NAME GET https://iid.googleapis.com/iid/info/IID_TOKEN HTTP Method: 用 POST 建立 資料, 用 GET 取得資料 Resource Name: 資源都是名詞, 且有分層關係, 但是 POST 有版本而 GET 沒有 Response Payload: 成功時的格式不一致 23
  • 24.
  • 25.
  • 26.
    RESTful API ? POSThttps://iid.googleapis.com/iid/v1/ IID_TOKEN /rel/topics/ TOPIC_NAME GET https://iid.googleapis.com/iid/info/IID_TOKEN HTTP Method: 用 POST 建立 資料, 用 GET 取得資料 Resource Name: 資源都是名詞, 且有分層關係, 但是 POST 有版本而 GET 沒有 Response Payload: 成功時的格式不一致, 失敗時有時是 HTML 有時是 Json, 格式也不一致 26
  • 27.
  • 28.
  • 29.
  • 30.
    RESTful API ? POSThttps://iid.googleapis.com/iid/v1/ IID_TOKEN /rel/topics/ TOPIC_NAME GET https://iid.googleapis.com/iid/info/IID_TOKEN HTTP Method: 用 POST 建立 資料, 用 GET 取得資料 Resource Name: 資源都是名詞, 且有分層關係, 但是 POST 有版本而 GET 沒有 Response Payload: 成功時的格式不一致, 但失敗時是 HTML 格式是一致 的 HTTP Status Code: 30
  • 31.
  • 32.
    Pros and Cons 1.Pros 1. 有一致的 HTTP Status Code 定義 2. 發生錯誤時的 API Response 若是 Json 格式, 使用字串代號而不是數字 代碼: ex: "error":"InvalidToken" 32
  • 33.
    Pros and Cons 1.Cons 1. 沒有一致的 Response 格式, 造成串接上要依據不同 API 做個別的處理 2. 甚至有 HTML 回傳格式, 無法做解析 3. 使用 IID_TOKEN 在 Path 上, 造成 API Path 非常的長 33
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.