IOS push implementation and server test + considerations

Source: Internet
Author: User
Tags openssl rsa pkcs12

I. Prepare materials

1. Create a push certificate in the app Certificate

(Download and install... remember to keep p12 (Cer. p12 and key. p12) + CER + mobileprovision for future update packages. Do not reconfigure the new p12 on the server after cancellation)

2. Generate the license file 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:

Copy code

  1. OpenSSL PKCS12-clcerts-nokeys-out cert. pem-in CERT. p12

Copy code

  1. OpenSSL PKCS12-nocerts-out key. pem-in key. p12

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

Copy code

  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.

Copy code

  1. Cat cert. pem key. unencrypted. pem> CK. pem

Ii. Code implementation:

1. IOS code:

1. Proxy

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//... // ***** Message push ***** [[uiapplication sharedapplication] registerforremotenotificationtypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
//.....
return YES;}

#pragma mark -#pragma mark push delegate- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{NSString *deviceTokenString=[NSString stringWithFormat:@"%@",deviceToken];deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@"<" withString:@""];deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@">" withString:@""];deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];}- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
//[UIApplication sharedApplication].applicationIconBadgeNumber=0;}

2. Test the PHP code on the server:

<? PHP $ devicetoken = 'device No ..... '; $ pass = '...... '; // ck. PEM password // define the message and sound $ message =$ _ Get ['message'] or $ message = $ argv [1] or $ message = 'message by kllmctrl '; $ badge = (INT) $ _ Get ['badge'] or $ badge = 3; // quantity $ sound = $ _ Get ['sound'] or $ sound = 'default'; // sound // dialog box to be displayed on the client $ body = array (); $ body ['aps '] = array ('alert' => $ message); if ($ badge) $ body ['aps '] ['badge'] = $ badge; if ($ sound) $ body ['aps '] ['sound'] = $ Sound; // file path $ CTX = stream_context_create (); stream_context_set_option ($ CTX, 'ssl ', 'local _ cert ','. /ck. pem'); // ck. path of PEM // configuration path stream_context_set_option ($ CTX, 'ssl ', 'passphrase', $ pass); // link Apple $ fp = stream_socket_client ('ssl: // gateway.sandbox.push.apple.com: 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 the 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);?>

Iii. Test

Http: // localhost: 8888/push. php

Note: The certificate in the iPhone description file must be valid.

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.