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亚当·琼斯
特此免费授予任何获得此软件副本和相关文档文件(“软件”)的人,以无限制处理该软件,包括无限制,使用,复制,修改,合并的权利,发布,分发,分布和/或出售该软件的副本,并允许提供该软件的人,但要遵守以下条件:
上述版权通知和此许可通知应包含在软件的所有副本或大量部分中。
该软件是“原样”提供的,没有任何形式的明示或暗示保证,包括但不限于适销性,适合特定目的和非侵权的保证。在任何情况下,作者或版权持有人均不应对任何索赔,损害赔偿或其他责任责任,无论是在合同,侵权或其他方面的诉讼中,与软件或与软件或使用或其他交易有关的诉讼或其他责任软件。