假设:
步骤1:生成证书PEM文件。 本文将帮助您生成PEM https://www.raywenderlich.com/123862/push-notifications-tutorial
步骤2:为APN安装Go库
去获取github.com/anachronistic/apns
步骤3:在main.go中编写以下函数。 将证书PEM文件放置在config文件夹中。 您可以根据需要更改推送URL。
func SendPushToClient(pushText string,pushToken string){ fmt.Println(“ SendPushToClient”) 有效负载:= apns.NewPayload() 有效负载。警报= pushText
pn:= apns.NewPushNotification() pn.DeviceToken = pushToken pn.AddPayload(有效载荷) 客户端:= apns.NewClient(“ gateway.sandbox.push.apple.com:2195”,“ ./config/pushcert.pem”,“ ./config/pushcert.pem”) 响应:= client.Send(pn)
警报,_:= pn.PayloadString() fmt.Println(“ Alert:”,alert) fmt.Println(“ Success:”,分别为成功) fmt.Println(“错误:”,分别为错误) }
步骤4:从主函数调用“ SendPushToClient”函数。 在以下代码中替换客户端令牌
func main(){ SendPushToClient(“您好,来自APN”,“ ”) }
步骤5:编译并运行
去跑主
完整源代码main.go
包主
导入( “ fmt” “ github.com/anachronistic/apns” )
func SendPushToClient(pushText string,pushToken string){ fmt.Println(“ SendPushToClient”) fmt.Println(“ pushText:”,pushText) fmt.Println(“ pushToken:”,pushToken) 有效负载:= apns.NewPayload() 有效负载。警报= pushText
文件:https://godoc.org/github.com/Coccodrillo/apns