package main import ( "encoding/json" "fmt" "github.com/RobotsAndPencils/buford/certificate" "github.com/RobotsAndPencils/buford/payload" "github.com/RobotsAndPencils/buford/payload/badge" "github.com/RobotsAndPencils/buford/push" "os" ) // set these variables appropriately const ( filename = "" password = "" //host = push.Development host = push.Development deviceToken = "" ) func main() { // load a certificate and use it to connect to the go get APN service: cert, err := certificate.Load(filename, password) exitOnError(err) topic := "32442424" if err == nil { fmt.Printf("err is nil ****\n") fmt.Printf("@@@@@@@@@@@@@@@@@@@????????????????????\n") topic = certificate.TopicFromCert(cert) fmt.Printf("topic: %s\n", topic) } client, err := push.NewClient(cert) exitOnError(err) service := push.NewService(client, host) // construct a payload to send to the device: p := payload.APS{ Alert: payload.Alert{Body: "Hello HTTP/2"}, Badge: badge.New(42), } b, err := json.Marshal(p) fmt.Println("json.Marshal") exitOnError(err) // push the notification: //headers := Headers{ //ID: "uuid", //CollapseID: "game1.score.identifier", //Expiration: time.Unix(12622780800, 0), //LowPriority: true, //Topic: "bundle-id", //} var headers push.Headers fmt.Printf("topic: %s\n", topic) if topic != "" { headers = push.Headers{ Topic: topic, } fmt.Printf("the Topic is *** %s\n", headers.Topic) } id, err := service.Push(deviceToken, &headers, b) //id, err := service.Push(deviceToken, nil, b) fmt.Println("service.Push") exitOnError(err) fmt.Println("apns-id:", id) } func exitOnError(err error) { if err != nil { fmt.Println(err) os.Exit(1) } }
上面是代码,引用的是 https://github.com/bergusman/apns-go
这个库的方法。上面需要用p12的证书,token添加推送手机token。