fbpx

如何透過 PHP 發送 Apple Notification Push


依照前一篇文章「iOS APNS 訊息推播 - Apple Push Notification Service 介紹」的介紹,大部分的工作 APNS 都幫我們做好了,我們只要透過憑證連上 APNS 並且依據以下規格進行溝通,訊息就能夠發送。

Simple Notification Format
Enhanced Notification Format
在 PHP 的實作上透過 stream_context_create() 函式建立 SSL 連線,再利用 pack() 函式實作上述的規格來傳送 Bytes 資料 (Payload),以下是 PHP 最精簡的發送訊息程式碼,執行後可以順利與 APNS 溝通與發送訊息。各位可以依據需求修改程式來發送 Enhanced 或 Simple 訊息,或者切換 Sandbox 或 Production 環境。程式範例如下:

參考資料