This is a creation in Article, where the information may have evolved or changed.
The original is here: http://bravenewmethod.wordpress.com/2011/02/25/apple-push-notifications-with-go-language/
Two days ago happened to see APNS no Go implementation, still pondering how to achieve a try, this next I worry again. The article itself is not very good, the code is somewhat useful. This article is purely to leave a material for the work behind. If you use it, you can ignore it.
———— – Translate split line ———— –
Go language development apple push notification
I started to learn and become familiar with the Go language, and I did some common common sense, such as sending Apple push notifications (Apple push notifications). This is my personal performance test for some development environments. To date, there have been:
- Push notifications with node. JS (push notifications with node. js)
- Push notifications using Erlang (push notifications with Erlang)
First Step Preparation
Get and compile Go. The example here is installed on Ubuntu 10.04 LTS x64, based on the introduction to go Getting Started guide.
- An introduction to the. Pem file for Apple push and get application sandbox private key is here.
- Of course, you have to get a 32-bit push token from your IOS app (push token).
The second part of the code
Here is the complete code, copied and saved as file apn.go.
Note Modify the certificate file (CERT.PEM and KEY-NOENC.PEM) for your own certificate file. Also, replace the push token with your own push token, which in this case is written in hexadecimal string for clarity.
Package Mainimport ("Crypto/tls" "FMT" "NET" "JSON" "OS" "Time" "bytes" "Encoding/hex" "encoding/binary ") func main () {//Load Certificate and config file cert, err: = TLS. Loadx509keypair ("Cert.pem", "Key-noenc.pem") if err! = Nil {fmt. Printf ("Error:%s\n", err.) String ()) OS. Exit (1)} conf: = &tls. Config {certificates: []tls. Certificate{cert},}//Connects to APNS and encrypts the socket conn with the TLS client, err: = Net. Dial ("TCP", "", "gateway.sandbox.push.apple.com:2195") if err! = Nil {fmt. Printf ("TCP Error:%s\n", err. String ()) OS. Exit (1)} Tlsconn: = TLS. Client (conn, conf)//force handshake to verify that the identity handshake is handled, otherwise it will try Err = Tlsconn at the first read and write. Handshake () if err! = Nil {fmt. Printf ("TLS error:%s\n", err. String ()) OS. Exit (1)}//debug information state: = Tlsconn. ConnectionState () fmt. PRINTF ("Conn State%v\n", state)//constructs binary from JSON structure payload Payload: = Make (map[string]interface{}) payload["APS"] = map[string]string{"alert": "Hello Push"} bpayload, err: = Json. Marshal (payload)//Converts a hexadecimal device token into a binary byte array btoken, _: = Hex. Decodestring ("6B4628DE9317C80EDD1C791640B58FDFC46D21D0D2D1351687239C44D8E30AB1")//Construction PDU Buffer: = bytes. Newbuffer ([]byte{})//command binary. Write (buffer, binary. Bigendian, Uint8 (1))//transmission id,optional binary. Write (buffer, binary. Bigendian, UInt32 (1))//expiry time, 1 hours binary. Write (buffer, binary. Bigendian, UInt32 (time. Seconds () + 60*60))//push device token binary. Write (buffer, binary. Bigendian, UInt16 (Len (btoken))) binary. Write (buffer, binary. Bigendian, Btoken)//push payload binary. Write (buffer, binary. Bigendian, UInt16 (Len (bpayload))) binary. Write (buffer, binary. Bigendian, bpayload) PDU: = buffer. Bytes ()//write to PDU _, err = Tlsconn. Write (PDU) if err! = Nil {fmt. Printf ("Write Error:%s\n", err. String ()) OS. Exit (1)}//wait 5 seconds for the PDU to return error tlsconn from the socket. Setreadtimeout (5*1e9) readb: = [6]byte{} n, err: = Tlsconn. Read (readb[:]) if n > 0 {fmt. Printf ("Received:%s\n", Hex.Encodetostring (Readb[:n])} tlsconn. Close ()}
Step three compile and run
Simple
$6g apn.go$ 6l apn.6$./6.outconn State {True 47}$
If all is fine, the program will exit in a few seconds and you will see a push notification on your iPhone.