APNS Service Push Notifications

Source: Internet
Author: User
Tags openssl rsa pkcs12

The author of this article is Cocoachina member "Marshluca", post address http://www.cocoachina.com/bbs/read.php?tid-20723.html

1. Register the app notification inside and get the devicetoken of the test machine from the APNs.
1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {2[[UIApplication Sharedapplication] registerforremotenotificationtypes: (Uiremotenotificationtypebadge |Uiremotenotificationtypesound)]; 3         //Other codes here. 4     returnYES;5 }6  7- (void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {8NSLog (@"Devicetoken:%@", Devicetoken);9 }Ten   One- (void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) Error { ANSLog (@"Error in registration. Error:%@", error); - } -   the- (void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) UserInfo - { -      -NSLog (@"receive push message:%@", [[UserInfo Objectforkey:@"APS"] Objectforkey:@"Alert"]); +     if([[UserInfo Objectforkey:@"APS"] Objectforkey:@"Alert"]!=NULL) { -uialertview* alert = [[Uialertview alloc] Initwithtitle:@"Push Notifications" +Message:[[userinfo Objectforkey:@"APS"] Objectforkey:@"Alert"] A                                                        Delegate: Self atCancelbuttontitle:@"Close" -Otherbuttontitles:@"Update Status", nil]; - [alert show]; - [alert release]; -     } -}


Start the program, and after registering the app with the notification item, locate the printed devicetoken in the console:
  1. Devicetoken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b>


2. Generate the license required by the app on the server
1) Enter the provisioning Portal and download the certificates certificate under development.
2) Locate the app ID that you want to test, and then enable it under development Apple push Notification service:development push SSL Certificate. You need to enter the signing certificate in 1 to generate a aps_developer_identity.cer.
3) Double-click Aps_developer_identity.cer to open the system's key chain. Find Apple Development Push Services under my certificates. You need to export a. p12 file for each of the private keys under certificate and it. (The setup password process will appear)
4) The above 2. p12 files need to be converted to. Pem format:
  1. OpenSSL pkcs12-clcerts-nokeys-out cert.pem-in CERT.P12

  1. OpenSSL pkcs12-nocerts-out key.pem-in KEY.P12

5) If you need to encrypt the key:
  1. OpenSSL rsa-in key.pem-out Key.unencrypted.pem

6) You can then merge two. pem files, the CK.PEM is the certificate required by the server.
  1. Cat Cert.pem Key.unencrypted.pem > Ck.pem


3. Service-side push notification to ANPs. Two methods have been found in Cocoachina:
1) PHP driver. Ck.pem and PHP scripts need to be placed on the server. The full PHP code is:
  1. 1 $deviceToken= ' 6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b ';2 $pass= ' 123456 ';//Passphrase for the private key (Ck.pem file)3  4 //Get the parameters from HTTP GET or from command line5 $message=$_get[' Message '] or$message=$argv[1] or$message= ' A test message from WorldCup ';6 $badge= (int)$_get[' badge '] or$badge= (int)$argv[2];7 $sound=$_get[' Sound '] or$sound=$argv[3];8  9 //Construct the notification payloadTen $body=Array(); One $body[' APS '] =Array(' alert ' =$message); A if($badge) -   $body[' APS '] [' badge '] =$badge; - if($sound) the   $body[' APS '] [' sound '] =$sound; -   - /*End of configurable 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); A   at //Connect to APNs - $fp=stream_socket_client(' ssl://gateway.sandbox.push.apple.com:2195 ',$err,$errstr, Stream_client_connect,$ctx); - if(!$fp) { -     Print"Failed to connect$err $errstr\ n "; -     return; - } in Else { -    Print"Connection ok\n to"; + } -   the //Send Message * $payload= Json_encode ($body); $ $msg=CHR(0).Pack("n", 32).Pack(' h* ',Str_replace(‘ ‘, ‘‘,$deviceToken)) .Pack("N",strlen($payload)) .$payload;Panax Notoginseng Print"Sending message:".$payload. "\ n";  - fwrite($fp,$msg); the fclose($fp); +?>


Request One http://127.0.0.1/apns/apns.php?message=A%20test%20message%20from%20localhost&badge=2&sound= RECEIVED5.CAF will be pushed to the APNs. The results of my request are as follows:
  1. Connection OK
  2. Sending message: {"APS": {"alert": "A test message from localhost", "badge": 2, "sound": "Received5.caf"}}


2) Pushmebaby Drive. Import the Aps_developer_identity.cer into project and rename it to Apns.cer.

APNS Service Push Notifications

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: info-contact@alibabacloud.com 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.