標籤:
產生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 mainimport ("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)}
golang實現ios推送