Apple push notication Service (apns) configuration strategy

Source: Internet
Author: User
Tags pkcs12
Apns (Apple push notication service) is supported after IOS 3.0. The following describes how to configure the apns service. Apns consists of two parts: client and server: Client: 1. Create an app ID. If you are already a registered Apple user (at least a developer ). Log on to Apple Developer Program portal 2 and create an app ID. Click Configure. Select support apns and select your development certificate (CSR) according to the steps ). Download the CER file and double-click Install (Apple development IOS push notication services: XXXXXXXXX) 3. Login In the keychain on Mac already contains the desired certificate and key. Press Control to export the certificate and key. Save it as the CERT. p12 and key. p12 files (you need to enter a password during the export process. Make sure you keep the password.) 4. Open the terminal. CD to the folder that protects cert. p12 key. p12. Set cert. p12 he key. p12 is converted to the PEM file, and OpenSSL PKCS12-clcerts-nodes-out cert is merged. PEM-In cert. p12openssl PKCS12-nodes-out key. PEM-in key. p12cat cert. PEM key. PEM> yourapp. pem5 produces the privosion file corresponding to the app ID. And add xcode. Use this certificate to sign your application (note that the app ID must be matched) 6. The apns client mainly involves the following API 6.1 to register the app in notification. -(bool) Application :( uiapplication *) Application metadata :( nsdictionary *) launchoptions {[uiapplication sharedapplication] metadata :( uiremotenotificationtypebadge | uiremotenotiftypetypesound)]; // other codes here. return yes;} 6.2 obtain the devicetoken of the test machine from apns. -(void) appli Cation :( uiapplication *) Application didregisterforremotenotificationswithdevicetoken :( nsdata *) devicetoken {nslog (@ "devicetoken: % @", devicetoken);}-(void) Application :( uiapplication *) application didfailtoregisterforremotenotificationswitherror :( nserror *) error {nslog (@ "error in registration. error: % @ ", error);} 3 when a push is received, process-(void) Application :( uiapplication *) Application didreceiverem Otenotification :( nsdictionary *) userinfo {nslog (@ "received push message: % @", [[userinfo objectforkey: @ "APs"] objectforkey: @ "alert"]); if ([[userinfo objectforkey: @ "APs"] objectforkey: @ "alert"]! = NULL) {uialertview * Alert = [[uialertview alloc] initwithtitle: @ "push notification" message: [[userinfo objectforkey: @ "APs"] objectforkey: @ "alert"] delegate: Self cancelbuttontitle: @ "close" otherbuttontitles: @ "Update Status", nil]; [alert show]; [alert release];} the client is basically complete. You need to keep the PEM file, the p12 File Password, and the obtained devicetke. The server needs to use the server: 1) PHP driver. You need to put the CK. PEM and PHP scripts on the server. All PHP code is: <? PHP $ devicetoken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b '; // you can replace $ pass = '2016' with your devicetoken '; // passphrase for the private key (CK. PEM file) // get the parameters from http get or from command line $ message =$ _ Get ['message'] or $ message = $ argv [1] or $ message = 'a Test message from worldcup '; $ badge = (INT) $ _ Get ['badge'] or $ badge = (INT) $ argv [2]; $ sound = $ _ Get ['sound'] or $ sound = $ argv [3]; // construct the notification payload $ body = array (); $ body ['aps '] = array ('alert' => $ message); if ($ badge) $ body ['aps '] ['badge'] = $ badge; if ($ sound) $ body ['aps '] ['sound'] = $ sound;/* end of resumable items */$ CTX = stream_context_create (); stream_context_set_option ($ CTX, 'ssl ', 'local _ cert', 'ck. pem'); // assume the private key passphase was removed. Stream_context_set_option ($ CTX, 'ssl ', 'passphrase', $ pass); // connect to apns $ fp = stream_socket_client ('ssl: // 100', $ err, $ errstr, 60, stream_client_connect, $ CTX); If (! $ FP) {print "failed to connect $ err $ errstr \ n"; return;} else {print "connection OK \ n <br/> ";} // send message $ payload = json_encode ($ body); $ MSG = CHR (0 ). pack ("N", 32 ). pack ('H * ', str_replace ('','', $ devicetoken )). pack ("N", strlen ($ payload )). $ payload; print "sending message :". $ payload. "\ n"; fwrite ($ FP, $ MSG); fclose ($ FP);?> Please request http: // php? Message = A % 20 test % 20 message % 20 from % 20 localhost & badge = 2 & Sound = received5.caf will be pushed to apns once. My request results are as follows: Connection OK sending message: {"APs": {"alert": "A Test message from localhost", "badge": 2, "sound ": "received5.caf"} deploy PHP and access it in IE. After testing, you can get the PUSH message on the real machine. It has passed the test on iphone4.3.


Although apns was successfully configured yesterday, its principles are not very clear. Today, I flipped through Erica Sadun's cookbook and found a specific chapter about it, which gives a clear explanation of its ins and outs. Take notes.

"Pushing notifications through apns requires three conditions: SSL certificate. The device ID and the notification to be sent must have custom valid content ." The SSL certificate can be IOsprovisioning
Step by step in the app IDs menu of the portal . "A token is associated with a device. The device token is created as a by-product of registration. After receiving the registration request, IOS immediately contacts apns and waits for it to return a device token. Apns builds a device token and returns it to IOS. Applications Use delegate callback-(Void) Application :( uiapplication *) appdidregisterforremotenoti Ficationswithdevicetoken Nsdata *) devicetoken
Get device token ". "Only applications get a device token and send it to the server can receive push messages. The application must send the device token to a device that is not actually notified by the push "."The valid content of the notification must include an APS dictionary that defines the attributes of the sound, sign, and warning to be sent to the user. The Custom Data Dictionary is limited to 256b. The notification content can be converted to JSON format ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.