golang實現ios推送


生成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)
}

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM