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.
- -(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes :( UIRemoteNotificationTypeBadge | uiremotenotiftypetypesound)];
- // Other codes here.
- Return YES;
- }
-
- -(Void) application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {
- NSLog (@ "deviceToken: % @", deviceToken );
- }
-
- -(Void) application :( UIApplication *) application didFailToRegisterForRemoteNotificationsWithError :( NSError *) error {
- NSLog (@ "Error in registration. Error: % @", error );
- }
-
- -(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo
- {
- NSLog (@ "received push message: % @", [[userInfoobjectForKey: @ "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];
- }
- }
Start the program, register the app to the notification item, and find the printed deviceToken in the console:
- 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
- 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:
- openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
-
- openssl pkcs12 -nocerts -out key.pem -in key.p12
5) If you do not need to encrypt the key:
- 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.
- 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:
- <?php
- $deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b';
- $pass = '123456'; // 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 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);
-
- // connect to apns
- $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $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);
- ?>
Request once
- 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
- 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!