APNS/2是使用新的HTTP/2推動提供商API的iOS,OSX和Safari上簡單,靈活和快速的Apple推送通知的GO軟件包。
go get -u github.com/sideshow/apns2如果您正在運行測試套件,則還需要安裝作證:
go get -u github.com/stretchr/testify package main
import (
"log"
"fmt"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
)
func main () {
cert , err := certificate . FromP12File ( "../cert.p12" , "" )
if err != nil {
log . Fatal ( "Cert Error:" , err )
}
notification := & apns2. Notification {}
notification . DeviceToken = "11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"
notification . Topic = "com.sideshow.Apns2"
notification . Payload = [] byte ( `{"aps":{"alert":"Hello!"}}` ) // See Payload section below
// If you want to test push notifications for builds running directly from XCode (Development), use
// client := apns2.NewClient(cert).Development()
// For apps published to the app store or installed as an ad-hoc distribution use Production()
client := apns2 . NewClient ( cert ). Production ()
res , err := client . Push ( notification )
if err != nil {
log . Fatal ( "Error:" , err )
}
fmt . Printf ( "%v %v %v n " , res . StatusCode , res . ApnsID , res . Reason )
}您可以選擇使用APNS JWT提供商身份驗證令牌,而不是使用.p12或.pem證書。首先,您需要一個apple的簽名密鑰( .p8文件),密鑰ID和團隊ID。一旦獲得了這些詳細信息,您就可以創建一個新客戶端:
authKey , err := token . AuthKeyFromFile ( "../AuthKey_XXX.p8" )
if err != nil {
log . Fatal ( "token error:" , err )
}
token := & token. Token {
AuthKey : authKey ,
// KeyID from developer account (Certificates, Identifiers & Profiles -> Keys)
KeyID : "ABC123DEFG" ,
// TeamID from developer account (View Account -> Membership)
TeamID : "DEF123GHIJ" ,
}
...
client := apns2 . NewTokenClient ( token )
res , err := client . Push ( notification )至少,通知需要一個deviceToken ,一個主題和有效載荷。
notification := & apns2. Notification {
DeviceToken : "11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7" ,
Topic : "com.sideshow.Apns2" ,
Payload : [] byte ( `{"aps":{"alert":"Hello!"}}` ),
}您還可以設置可選的APNSID ,到期或優先級。
notification . ApnsID = "40636A2C-C093-493E-936A-2A4333C06DEA"
notification . Expiration = time . Now ()
notification . Priority = apns2 . PriorityLow 您可以使用原始字節進行notification.Payload 。如上所述,可以使用有效載荷構建器軟件包,這使得易於構建APNS有效載荷。
// {"aps":{"alert":"hello","badge":1},"key":"val"}
payload := payload . NewPayload (). Alert ( "hello" ). Badge ( 1 ). Custom ( "key" , "val" )
notification . Payload = payload
client . Push ( notification )有關更多信息,請參閱有效載荷文檔。
APNS/2借鑒了Apple的有效響應之間的區別,指示該通知是否已發送,以及不可恢復或意外的錯誤;
Error ,即如果基礎HTTP.Client連接或證書存在問題,則未發送有效載荷,或者未收到有效的響應。Response 。該結構將包含有關推送通知是否成功,其APNS-ID以及適用的更多信息,更多的信息有關它為什麼不成功。檢查是否已成功發送Notification ;
res , err := client . Push ( notification )
if err != nil {
log . Println ( "There was an error" , err )
return
}
if res . Sent () {
log . Println ( "Sent:" , res . ApnsID )
} else {
fmt . Printf ( "Not Sent: %v %v %v n " , res . StatusCode , res . ApnsID , res . Reason )
}為了更好地控制請求取消和超時APN/2支持上下文。如果您想取消父進程,或者需要對單個推動超時的更細膩的控制,則使用上下文可能會有所幫助。有關上下文的更多信息,請參見Google帖子。
ctx , cancel = context . WithTimeout ( context . Background (), 10 * time . Second )
res , err := client . PushWithContext ( ctx , notification )
defer cancel ()另請參閱APNS HTTP 2推動速度上的Wiki頁面。
為了獲得最佳性能,您應該堅持使用apns2.Client實例,而不是每次推動都重新創建它。基礎TLS連接本身可能需要幾秒鐘才能進行連接和談判,因此,如果您設置了apns2.Client並將其拆除每個推動力,那麼這將極大地影響性能。 (Apple建議始終保持連接打開)。
您還應該限制apns2.Client實例的數量。基礎傳輸具有HTTP連接池本身,因此對於大多數用戶來說,單個客戶端實例可能足夠(一個實例可以每秒進行4,000多個推送)。如果您需要更多,那麼每個CPU核心的一個實例是一個很好的起點。
速度受到服務器位置和網絡連接質量的極大影響。如果您只是在本地測試,在代理後面或您的服務器在美國以外的地方,那麼您將無法獲得出色的性能。在AWS中的良好服務器中,您應該能夠獲得體面的吞吐量。
APNS/2具有一個命令行工具,可以使用go get github.com/sideshow/apns2/apns2安裝。用法:
apns2 --help
usage: apns2 --certificate-path=CERTIFICATE-PATH --topic=TOPIC [<flags>]
Listens to STDIN to send notifications and writes APNS response code and reason to STDOUT.
The expected format is: <DeviceToken> <APNS Payload>
Example: aff0c63d9eaa63ad161bafee732d5bc2c31f66d552054718ff19ce314371e5d0 {"aps": {"alert": "hi"}}
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
-c, --certificate-path=CERTIFICATE-PATH
Path to certificate file.
-t, --topic=TOPIC The topic of the remote notification, which is typically the bundle ID for your app
-m, --mode="production" APNS server to send notifications to. `production` or `development`. Defaults to `production`
--version Show application version.
麻省理工學院許可證(MIT)
版權(c)2016亞當·瓊斯
特此免費授予任何獲得此軟件副本和相關文檔文件(“軟件”)的人,以無限制處理該軟件,包括無限制,使用,複製,修改,合併的權利,發布,分發,分佈和/或出售該軟件的副本,並允許提供該軟件的人,但要遵守以下條件:
上述版權通知和此許可通知應包含在軟件的所有副本或大量部分中。
該軟件是“原樣”提供的,沒有任何形式的明示或暗示保證,包括但不限於適銷性,適合特定目的和非侵權的保證。 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE軟體.