生成pem文件
打开Keychain Access 导出推送证书和私钥
推送证书 cert.p12
私钥 key.p12
导出.pem文件
转换推送证书
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
转换私钥
openssl pkcs12 -nocerts -out key.pem -in key.p12 #输入2次密码,后面golang代码中密码部分相同
合并推送证书和私钥
cat cert.pem key.pem > push_ck.pem
测试生成的pem
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert cert.pem -key key.pem
输出大体如下说明成功
使用golang的推送库 ap
package main import ( "log" apns "github.com/sideshow/apns2" "github.com/sideshow/apns2/certificate" ) func main() { cert, pemErr := certificate.FromPemFile("push_ck.pem", "密码") if pemErr != nil { log.Println("Cert Error:", pemErr) } notification := &apns.Notification{} notification.DeviceToken = "6970fc6ecdda0fa32f48e920b4657149f394eb2c3f03b7517f11f450a8ba2b41" notification.Topic = "com.yghc.property" notification.Payload = []byte(`{ "aps" : { "alert" : "Hello!" } } `) client := apns.NewClient(cert).Production() //开发环境 res, err := client.Development().Push(notification) if err != nil { log.Println("Error:", err) return } log.Println("APNs ID:", res.ApnsID) }