import ( "crypto/tls" "flag" "fmt" "golang.org/x/net/http2" "net" "net/http" "runtime" "strings" "time" certificate "github.com/sideshow/apns2/certificate" "log" ) // Apple HTTP/2 Development & Production urls const ( HostDevelopment = "https://api.development.push.apple.com" HostProduction = "https://api.push.apple.com" ) func main() { push_ios() } func push_ios() { cert, err := certificate.FromPemFile("/Users/xzh/www/test/www/pem/dev.pem", "") if err != nil { log.Fatal("Cert Error:", err) return } httpClient := http.Client{ Transport: &http2.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: false, Certificates: []tls.Certificate{cert}, }, DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) { dialer := &net.Dialer{ Timeout: 20 * time.Second, KeepAlive: 60 * time.Second, } return tls.DialWithDialer(dialer, network, addr, cfg) }, }} device_token := "413eed6b6253f16fdc010217caa845cadab7abde6360ee959051dc3b6645a06b" url := "https://api.development.push.apple.com/3/device/" + device_token param := strings.NewReader(fmt.Sprintf("{\"aps\":{\"alert\":\"收到了回我下。\",\"sound\":\"default\"}}")) req, _ := http.NewRequest("POST", url, param) req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("apns-topic", "com.zhapp.fhzxg.BPushProject") resp, err := httpClient.Do(req) if err != nil { println(err.Error()) return } println(resp.Body, resp.StatusCode) }
原文链接:go苹果原生http2,apns推送接口调用核心代码,转载请注明来源!