APNS push notification process for iPhone applications

Source: Internet
Author: User
Tags pkcs12

IPhoneApplicationAPNS push notificationThe process is the content to be introduced in this article.APNS push notificationThis article introduces the implementation of code. Let's take a look at the details.

Below are all my deployment and configuration processes.

1. register the app in notification and obtain the deviceToken of the test machine from APNS.

 
 
  1. -(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {
  2. [[UIApplication sharedApplication] registerForRemoteNotificationTypes :( UIRemoteNotificationTypeBadge | uiremotenotiftypetypesound)];
  3. // Other codes here.
  4. Return YES;
  5. }
  6.  
  7. -(Void) application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {
  8. NSLog (@ "deviceToken: % @", deviceToken );
  9. }
  10.  
  11. -(Void) application :( UIApplication *) application didFailToRegisterForRemoteNotificationsWithError :( NSError *) error {
  12. NSLog (@ "Error in registration. Error: % @", error );
  13. }
  14.  
  15. -(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo
  16. {
  17. NSLog (@ "received push message: % @", [[userInfoobjectForKey: @ "aps"] objectForKey: @ "alert"]);
  18. If ([[userInfo objectForKey: @ "aps"] objectForKey: @ "alert"]! = NULL ){
  19. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "push notification"
  20. Message: [[userInfo objectForKey: @ "aps"] objectForKey: @ "alert"]
  21. Delegate: self
  22. CancelButtonTitle: @ "close"
  23. OtherButtonTitles: @ "Update Status", nil];
  24. [Alert show];
  25. [Alert release];
  26. }
  27. }

Start the program, register the app to the notification item, and find 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) Go to the Provisioning Portal and download the certificate of Certificates under development.

2) locate the app id to be tested and enable

 
 
  1. Apple Push Notification service: Development Push SSL Certificate 

You must enter the signature certificate in 1) to generate an aps_developer_identity.cer.

3) double-click aps_developer_identity.cer to open the key chain of the system. Locate Apple Development Push Services under My certificates. You need to generate a. p12 file for each of the certificate and its private keys. (The password setting process will appear)

4) convert the above two. p12 files to. pem format:

 
 
  1. openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12  
  2.  
  3.  openssl pkcs12 -nocerts -out key.pem -in key.p12 

5) If you do not need to encrypt the key:

 
 
  1. openssl rsa -in key.pem -out key.unencrypted.pem 

6) Then you can merge two. pem files. This ck. pem is the certificate required by the server.

 
 
  1. cat cert.pem key.unencrypted.pem > ck.pem 

3. The server pushes the notification to ANPS. Two methods are found in cocoachina:

1) php driver. You need to put the ck. pem and php scripts on the server. All php code is:

 
 
  1.  <?php 
  2. $deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b';  
  3. $pass = '123456';   // Passphrase for the private key (ck.pem file)  
  4.  
  5. // Get the parameters from http get or from command line  
  6. $message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';  
  7. $badge = (int)$_GET['badge'] or $badge = (int)$argv[2];  
  8. $sound = $_GET['sound'] or $sound = $argv[3];  
  9.  
  10. // Construct the notification payload  
  11. $body = array();  
  12. $body['aps'] = array('alert' => $message);  
  13. if ($badge)  
  14.   $body['aps']['badge'] = $badge;  
  15. if ($sound)  
  16.   $body['aps']['sound'] = $sound;  
  17.  
  18. /* End of Configurable Items */  
  19. $ctx = stream_context_create();  
  20. stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');    
  21. // assume the private key passphase was removed.  
  22. stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);  
  23.  
  24. // connect to apns  
  25. $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
  26. if (!$fp) {  
  27.     print "Failed to connect $err $errstr\n";  
  28.     return;  
  29. }  
  30. else {  
  31.    print "Connection OK\n<br/>";  
  32. }  
  33.  
  34. // send message  
  35. $payload = json_encode($body);  
  36. $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;  
  37. print "Sending message :" . $payload . "\n";    
  38. fwrite($fp, $msg);  
  39. fclose($fp);  
  40. ?> 

Request once

 
 
  1. http://127.0.0.1/apns/apns.php?message=A%20test%20message%20from%20localhost&badge=2&sound=received5.caf 

It will be pushed to APNS once. My request results are as follows:

Copy code Connection OK

 
 
  1. Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}} 

2) pushMeBaby driver. Import aps_developer_identity.cer to the project and change it to apns. cer.

Start the app to see the push window. PushMeBaby and the specific configuration process are also based on this post. Thank you very much for your dedication.

Summary:IPhoneApplicationAPNS push notificationThe content of the process has been introduced. I hope this article will help you!

Related Article

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.