使用Go送出iOS設備的推播訊息(Push Notification)

環境準備

go get github.com/anachronistic/apns

完整程式

package main
import (
       "fmt"
       "github.com/anachronistic/apns"
)

func main() {
       SendPushToClient("Hello from APN","<CLIENT TOKEN>")
}
func SendPushToClient(pushText string,pushToken string)  {
       fmt.Println("SendPushToClient")
       fmt.Println("pushText: ", pushText)
       fmt.Println("pushToken: ", pushToken)
       payload := apns.NewPayload()
       payload.Alert = pushText
       pn := apns.NewPushNotification()
       pn.DeviceToken = pushToken
       pn.AddPayload(payload)
       client := apns.NewClient("gateway.sandbox.push.apple.com:2195", "./config/pushcert.pem", "./config/pushcert.pem")
       resp := client.Send(pn)
       alert, _ := pn.PayloadString()
       fmt.Println("Alert:", alert)
       fmt.Println("Success:", resp.Success)
       fmt.Println("Error:", resp.Error)
}

參考文件