Today, I spent several hours doing this push. The previous attempts failed and failed. I have read many tutorials on the Internet. There are several points worth noting.
This blog is clearly written: http://zxs19861202.iteye.com/blog/1532460
Http://www.mobiledevor.com/post/100.html
1. devicetoken is obtained from IOS
-(Void) Application :( uiapplication *) Application didregisterforremotenotifswswithdevicetoken :( nsdata *) ptoken {nslog (@ "regisger success: % @", ptoken); // registration successful, save devicetoken to the Application Server database}
2. Steps
1. log on to the iPhone developer connection portal (http://developer.apple.com/iphone/manage/overview/index.action) and click app IDS
2. Create an Apple id. The wildcard ID cannot be used for the push notification service. For example, Com. itotem. iPhone
3. Click "Configure" next to the Apple id, generate a signature upload according to the "Wizard" step, and then download the generated license.
4. Double-click the. Cer file to import your aps_developer_identity.cer to the keychain.
5. Start the keychain assistant on the Mac, and then selectCertificatesCategory. An extensible option "Apple development push services" is displayed"
6. Expand this option and right-click "Apple development push services"> export "Apple development push services id123 ". Save as a apns-dev-cert.p12 file. (Note thatCertificatesClassification)
7. Extend the "Apple development push services" pair"Private Key"Save as a apns-dev-key.p12 file by doing the same.
8. convert these files to the PEM format through Terminal commands: OpenSSL PKCS12-clcerts-nokeys-out apns-dev-cert.pem-In apns-dev-cert.p12
OpenSSL PKCS12-nocerts-out apns-dev-key.pem-In apns-dev-key.p12
9. Finally, you need to synthesize the key and license file into a apns-dev.pem file that you need to use when connecting to apns:
Cat apns-dev-cert.pem apns-dev-key-noenc.pem> apns-dev.pem
3. php call
$ Devicetoken = 'hangzhou'; // No space $ body = array ("APs" => array ("alert" => 'message', "badge" => 2, "sound" => 'default'); // push method, including content and sound $ CTX = stream_context_create (); // if it is on a Windows server, there is a problem with finding the PEM path. The path is modified to this method: // $ PEM = dirname (_ file __). '/'. 'apns-Dev. pem'; // the Linux server can directly write the PEM path to stream_context_set_option ($ CTX, "SSL", "local_cert", "apns-dev.pem"); $ Pa Ss = "xxxxxx"; stream_context_set_option ($ CTX, 'ssl ', 'passphrase', $ pass); // you need to select two servers for development and testing, select the server with the second sandbox name and use the dev PEM certificate. If it is published, use the product PEM and select the official server $ fp = stream_socket_client ("SSL: // gateway.push.apple.com: 2195 ", $ err, $ errstr, 60, stream_client_connect, $ CTX); $ fp = stream_socket_client (" SSL: // gateway.sandbox.push.apple.com: 2195 ", $ err, $ errstr, 60, stream_client_connect, $ CTX); If (! $ FP) {echo "failed to connect $ err $ errstrn"; return;} print "connection OK \ n"; $ payload = json_encode ($ body ); $ MSG = CHR (0 ). pack ("N", 32 ). pack ("H *", str_replace ('','', $ devicetoken )). pack ("N", strlen ($ payload )). $ payload; echo "sending message :". $ payload. "\ n"; fwrite ($ FP, $ MSG); fclose ($ FP );