iOS APNS 訊息推播 – Apple Push Notification Service 介紹
2014-05-16 16:19
453 查看
[b]原文网址:http://blog.toright.com/posts/2806/ios-apns-%E8%A8%8A%E6%81%AF%E6%8E%A8%E6%92%A5-apple-push-notification-service-%E4%BB%8B%E7%B4%B9
[/b]
[b]APNS 介紹
[/b]
APNS 全名為 Apple Push Notification Service,也就是大家常說的 Push Notification 或者是 Server Push 技術,簡單說就是主動由 Server 發送訊息到 Client 的方法,而通常這樣的技術都會實作穿越防火牆的能力。在 iOS 所提供的 APNS 的架構中,包含了以下幾種角色:
Device – 就是你的行動上網裝置 (MID)
APNS – Apple 提供連線服務的伺服器 (Gateway)
Provider – 需要自行實作的訊息發送者
接收 Notification 的單位稱為 Device,發送 Notification 的單位稱為 Provider。Provider 與 Device 這兩個單位皆需要與 APNS 進行溝通,必須認證連線後才能正確工作。Device 就是我們的行動裝置 (iPhone/iPad),當有網路的時候 iOS 就動裝置會自動連上 APNS,並且登入自己的裝置,這時 Device 就準備好可以接收訊息了,如下圖:
![](http://blog.toright.com/wp-content/uploads/2012/10/service_device_ct.jpg)
而負責發送訊息的 Provider 就需要自行透過憑證建立連線來發送訊息,連線時使用 SSL TLS 機制進行認證與加密,下圖為 Apple 官方提供的驗證架構圖:
![](http://blog.toright.com/wp-content/uploads/2012/10/service_provider_ct.jpg)
由於 Provider 在發送 Notification 的時候需要用到 SSL Push Certificate 與 Device Token,因此我們需要在 App 啟動的時候自動讓 Provider 知道行動裝置的 Device Token,這個動作我們稱為 Registration,如下圖步驟(4)。傳送 Device Token 到 Provider 的方法可以自行實作,通常透過簡單的 WebService 就可以完成,這裡就不詳述了。
![](http://blog.toright.com/wp-content/uploads/2012/10/registration_sequence.jpg)
申請 AppID 之後,我們可以在 Apple Developer 中的 Provisioning Portal 功能中下載 Push Service 需要的連線憑證 SSL Push Certificate,如下圖:
![](http://blog.toright.com/wp-content/uploads/2012/10/apns-640x368.png)
憑證有兩種形態,分別為 Development 與 Producrion,Development Ceritificate 使用的時機我們稱為 Sandbox,主要用在 App 未上架前的開發測試週期中(或是 Debug 階段),然而一般我們使用 Xcode 佈署 App 到行動裝置中進行 Debug,就是要透過 Development Ceritificate 來發送 Notification。若是 App 上架之後 Provider
就要改用 Production Ceritificate 來發送訊息了。
[b]Provider APNS SSL Connection[/b]
前面有提到 Provider 在發送訊息時需要對 APNS 進行 SSL 連線,連線完成後便可以向 Service 發送訊息,這封訊息我們稱為 Payload(後面會介紹)。在 APNS 的架構中 Service Gateway 扮演了 Message Queue (訊息佇列) 的角色。我僅需要將要發送的訊息送到 Gateway 後,透過已經連上 Gateway 的行動裝置就可以自動接收訊息。在 APNS 的架構中,定義了兩種訊息格式,分別是
Simple Notification 與 Enhanced Notification。
Simple Notification 是非常沒有保障的,當 Provider 發送 Notification 的時候,假如 Device 這時並沒有正在跟 APNS 連線,那麼這一封信息就不會被傳送到使用者的裝置上。Enhanced Notification 相對於 Simple Notification 就比較有保障一些,發送時可以定義「存活時間」,如果在有效時間內只要 Device 有連上 APNS,那麼這一封訊息 (Payload)
就會被送出,流程可以參考下圖:
![](http://blog.toright.com/wp-content/uploads/2012/10/token_trust.jpg)
此外一提,APNS 針對單一個裝置只允許保留一封訊息(被最後一個訊息覆蓋),所以如果你想要利用 Enhanced Notification 灌爆 APNS 的話還是死了這條心吧。
當 Provider 透過憑證連上 APNS Gateway 之後,只要照著以下的格式傳送資料到 Gateway,APNS 就會幫你發送訊息了,兩種訊息格式定義如下:
Simple Notification Format
![](http://blog.toright.com/wp-content/uploads/2012/10/aps_provider_binary.jpg)
Enhanced Notification Format
![](http://blog.toright.com/wp-content/uploads/2012/10/aps_binary_provider_2-640x90.jpg)
如果你想要一次發送訊息給很多裝置,只需要在建立 SSL 連線以後連續傳送上述的資料即可,上面介紹的 Notification Format 最後的 Block 稱為 Payload,接下來我們來看看 Payload 的格式。
The Notification Payload Block
當 Provider 要發送訊息時,必須傳送一個特別格式的資料到 APNS,這個資料我們稱為 Payload。Payload 內文使用 JSON 格式進行定義,基本功能為 alert, badge 與 sound,alert 提供文字訊息顯示;badge 提供 App 圖示右上角的顯示數字;sound 則提供收到訊息要播放的聲音(可在 App Bundle 自行定義)。除了這些功能,Payload 還允許我們自己定義要傳送的資料,Payload 範例如下:
JavaScript
{
"aps": {
"alert": "Hello World.",
"badge": 9,
"sound": "default"
},
"custom_key": "custom_value"
}
當 Device 的 iOS 收到上面的訊息之後,就會在系統中顯示訊息視窗,並且同時撥放提示音(預設是:等燈登),然後就可以在桌面 App 的圖示上看見數字 9 的標記了。
討論
Windows Phone 有
MPNS、Android 有
C2DS,我認為行動裝置的作業系統,提供推播機制絕對是必要的功能,否則每一個 App 都實作連線,會大量耗損電源。而且在 iOS 不允許 App 背景執行的限制下,Push Notification 功能更是重要。實作能夠通用於各種作業系統的 Provider 也是不錯的設計,而市面上也已經有許多現成的 Backend Server 實作了跨平台的 Push Service,在開發上也可以省下不少功夫。
[b]參考資料[/b]
Apple Push Notification Service
Provider Communication with Apple Push Notification Service
WebApp 開發教學 (1) – HTML5 WebView 行動應用
淺談
REST 軟體架構風格 (Part.II) – 如何設計 RESTful Web Service?
淺談
REST 軟體架構風格 (Part.I) – 從了解 REST 到設計 RESTful!
如何在
Safari 5 建立 iPhone/iPad (i系列行動產品) 網頁瀏覽環境
Linux
Samba Service 設定筆記(網路芳鄰/檔案共享)
PHP Rich Server Platform (Released!!)
SOA 與 Web Service
Mac Book & iPod Touch (A apple a day, keep the money away)
Apache
AXIS 2 – SOAP Web Service 實作教學 (即時天氣預報)
Restlet 實作教學 – Representational
State Transfer Web Service
[/b]
[b]APNS 介紹
[/b]
APNS 全名為 Apple Push Notification Service,也就是大家常說的 Push Notification 或者是 Server Push 技術,簡單說就是主動由 Server 發送訊息到 Client 的方法,而通常這樣的技術都會實作穿越防火牆的能力。在 iOS 所提供的 APNS 的架構中,包含了以下幾種角色:
Device – 就是你的行動上網裝置 (MID)
APNS – Apple 提供連線服務的伺服器 (Gateway)
Provider – 需要自行實作的訊息發送者
接收 Notification 的單位稱為 Device,發送 Notification 的單位稱為 Provider。Provider 與 Device 這兩個單位皆需要與 APNS 進行溝通,必須認證連線後才能正確工作。Device 就是我們的行動裝置 (iPhone/iPad),當有網路的時候 iOS 就動裝置會自動連上 APNS,並且登入自己的裝置,這時 Device 就準備好可以接收訊息了,如下圖:
![](http://blog.toright.com/wp-content/uploads/2012/10/service_device_ct.jpg)
而負責發送訊息的 Provider 就需要自行透過憑證建立連線來發送訊息,連線時使用 SSL TLS 機制進行認證與加密,下圖為 Apple 官方提供的驗證架構圖:
![](http://blog.toright.com/wp-content/uploads/2012/10/service_provider_ct.jpg)
由於 Provider 在發送 Notification 的時候需要用到 SSL Push Certificate 與 Device Token,因此我們需要在 App 啟動的時候自動讓 Provider 知道行動裝置的 Device Token,這個動作我們稱為 Registration,如下圖步驟(4)。傳送 Device Token 到 Provider 的方法可以自行實作,通常透過簡單的 WebService 就可以完成,這裡就不詳述了。
![](http://blog.toright.com/wp-content/uploads/2012/10/registration_sequence.jpg)
申請 AppID 之後,我們可以在 Apple Developer 中的 Provisioning Portal 功能中下載 Push Service 需要的連線憑證 SSL Push Certificate,如下圖:
![](http://blog.toright.com/wp-content/uploads/2012/10/apns-640x368.png)
憑證有兩種形態,分別為 Development 與 Producrion,Development Ceritificate 使用的時機我們稱為 Sandbox,主要用在 App 未上架前的開發測試週期中(或是 Debug 階段),然而一般我們使用 Xcode 佈署 App 到行動裝置中進行 Debug,就是要透過 Development Ceritificate 來發送 Notification。若是 App 上架之後 Provider
就要改用 Production Ceritificate 來發送訊息了。
[b]Provider APNS SSL Connection[/b]
前面有提到 Provider 在發送訊息時需要對 APNS 進行 SSL 連線,連線完成後便可以向 Service 發送訊息,這封訊息我們稱為 Payload(後面會介紹)。在 APNS 的架構中 Service Gateway 扮演了 Message Queue (訊息佇列) 的角色。我僅需要將要發送的訊息送到 Gateway 後,透過已經連上 Gateway 的行動裝置就可以自動接收訊息。在 APNS 的架構中,定義了兩種訊息格式,分別是
Simple Notification 與 Enhanced Notification。
Simple Notification 是非常沒有保障的,當 Provider 發送 Notification 的時候,假如 Device 這時並沒有正在跟 APNS 連線,那麼這一封信息就不會被傳送到使用者的裝置上。Enhanced Notification 相對於 Simple Notification 就比較有保障一些,發送時可以定義「存活時間」,如果在有效時間內只要 Device 有連上 APNS,那麼這一封訊息 (Payload)
就會被送出,流程可以參考下圖:
![](http://blog.toright.com/wp-content/uploads/2012/10/token_trust.jpg)
此外一提,APNS 針對單一個裝置只允許保留一封訊息(被最後一個訊息覆蓋),所以如果你想要利用 Enhanced Notification 灌爆 APNS 的話還是死了這條心吧。
當 Provider 透過憑證連上 APNS Gateway 之後,只要照著以下的格式傳送資料到 Gateway,APNS 就會幫你發送訊息了,兩種訊息格式定義如下:
Simple Notification Format
![](http://blog.toright.com/wp-content/uploads/2012/10/aps_provider_binary.jpg)
Enhanced Notification Format
![](http://blog.toright.com/wp-content/uploads/2012/10/aps_binary_provider_2-640x90.jpg)
如果你想要一次發送訊息給很多裝置,只需要在建立 SSL 連線以後連續傳送上述的資料即可,上面介紹的 Notification Format 最後的 Block 稱為 Payload,接下來我們來看看 Payload 的格式。
The Notification Payload Block
當 Provider 要發送訊息時,必須傳送一個特別格式的資料到 APNS,這個資料我們稱為 Payload。Payload 內文使用 JSON 格式進行定義,基本功能為 alert, badge 與 sound,alert 提供文字訊息顯示;badge 提供 App 圖示右上角的顯示數字;sound 則提供收到訊息要播放的聲音(可在 App Bundle 自行定義)。除了這些功能,Payload 還允許我們自己定義要傳送的資料,Payload 範例如下:
JavaScript
{
"aps": {
"alert": "Hello World.",
"badge": 9,
"sound": "default"
},
"custom_key": "custom_value"
}
1 2 3 4 5 6 7 8 | { "aps": { "alert": "Hello World.", "badge": 9, "sound": "default" }, "custom_key": "custom_value" } |
討論
Windows Phone 有
MPNS、Android 有
C2DS,我認為行動裝置的作業系統,提供推播機制絕對是必要的功能,否則每一個 App 都實作連線,會大量耗損電源。而且在 iOS 不允許 App 背景執行的限制下,Push Notification 功能更是重要。實作能夠通用於各種作業系統的 Provider 也是不錯的設計,而市面上也已經有許多現成的 Backend Server 實作了跨平台的 Push Service,在開發上也可以省下不少功夫。
[b]參考資料[/b]
Apple Push Notification Service
Provider Communication with Apple Push Notification Service
推薦文章
AndroidWebApp 開發教學 (1) – HTML5 WebView 行動應用
淺談
REST 軟體架構風格 (Part.II) – 如何設計 RESTful Web Service?
淺談
REST 軟體架構風格 (Part.I) – 從了解 REST 到設計 RESTful!
如何在
Safari 5 建立 iPhone/iPad (i系列行動產品) 網頁瀏覽環境
Linux
Samba Service 設定筆記(網路芳鄰/檔案共享)
PHP Rich Server Platform (Released!!)
SOA 與 Web Service
Mac Book & iPod Touch (A apple a day, keep the money away)
Apache
AXIS 2 – SOAP Web Service 實作教學 (即時天氣預報)
Restlet 實作教學 – Representational
State Transfer Web Service
相关文章推荐
- IOS 推送服务--Apple Push Notification Service
- iOS-在SAE使用Apple Push Notification Service实现消息推送
- IOS iphone 推送服务--Apple Push Notification Service
- iPhone Apple Push Notification Service (APNS)(全教程)
- Apple Push Notification Service (APNS)原理与实现方案
- iPhone Apple Push Notification Service (APNS)(全教程)
- iOS 8 Apple Push Notification Service
- iOS - APNs(Apple Push Notification Services)推送通知
- Apple Push Notification Service(APNS)开发指引
- APNS(Apple Push Notification Service)远程推送原理解析
- [转]宝文!Apple Push Notification Service (APNS)原理与实现方案
- APNs(英文全称:Apple Push Notification service)Demo与实践
- Apple Push Notification Service (APNS)原理与实现方案
- iPhone Apple Push Notification Service (APNS)
- iPhone Apple Push Notification Service (APNS)
- iPhone Apple Push Notification Service (APNS)(全教程)
- Apple Push Notification Service(APNS)相关
- iPhone Apple Push Notification Service (APNS)(全教程)
- java libs for apns:apple push notification service
- APNS - Apple Push Notification Service