The implementation case of APNS push notification process code in the iPhone application is described in this article. It mainly describes how to implement APNS push notifications. For details, refer to the detailed code in this article.
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: % @", [[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];
}
}
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 it to Push the Notification service: development Push SSL Certificate under Development. 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 = '000000'; // 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: // gateway.sandbox.push.apple.com: 000000', $ err, $ errstr, 60, STREAM_CLIENT_CONNECT, $ ctx );
If (! $ Fp ){
Print "Failed to connect $ err $ errstrn ";
Return;
}
Else {
Print "Connection OKn <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: // 127.0.0.1/apns. 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 "}}
(2) pushMeBaby driver. Import aps_developer_identity.cer to the project and change it to apns. cer.
Summary: The Implementation case of the APNS push notification process code in the iPhone application is described. I hope this article will help you!
Author: Feng jingbao