Remote push for iOS development

Source: Internet
Author: User
Tags pkcs12

Speaking of remote push, should use also quite a lot of, today based on the sea of cloud push service, do a push small demo, to understand how the remote push in iOS is how a matter, first you have to have Apple developer account, good salted egg is almost, the main content to go up.

First, the preparatory stage

1. A name called Certsigningrequest file is required before the Apple Developer online application for a push certificate. This file is generated on a Mac system and is used to request a push certificate file on the Apple website, following the steps to generate the file using a keychain:

(1). Open Keychain under Mac-"Keychain Access-" certificate assistant-"request a certificate from a certification authority, as shown in:

(2). After the previous step, the following dialog box appears, the mailbox and name are filled in, and then you select Save to disk, you can generate a certificatesigningrequest.certsigningrequest file locally. Such as:

2. To apply for a push certificate on the Apple Developer's website, proceed as follows:

(1), after landing with the developer account, enter member Center, as shown in:

(2) After entering Member Center, click on the following link:

(3), register a support push AppID, in the identifiers below the App ids option click Add button, add a new appids, into:

(4), fill in the name of your AppID

(5), bundle ID that binds to your app

(6), select push notifications, so that the AppID support message push:

 

3, use the above AppID to apply for a push certificate

(1), click on the certificate, select the development mode, and then radio push service

(2), select the AppID we just created, as follows:

(3), upload the file we just created locally with a keychain:

(4), then you can download and use our push certificate

4. Create a provisioning profiles file with our AppID as follows:

5. The process for generating a PEM file for PHP using the above certificate is as follows:

(1). Open the CSR file we generated above by keychain, right-click to export the P12 file, such as:

(2). Set Access password:

(3), switch to the terminal under the P12 folder we have just exported, the command line to generate a PEM file, the command line as follows, the merger will let you enter the password, password is the password we set above:

1 OpenSSL Pkcs12-clcerts-nokeys- out A.pem- in aps_test.p122 OpenSSL Pkcs12-nocerts- out B.pem- in aps_test.p123 cat A.pem B.pem > End.pem

6. Upload the generated End.pem file to the cloud push service on the SAE and we can access it via the file ID on the SAE:

    

Second, the server-side code writing:

The PHP code used by the SAE Oberyun push is as follows:

1<?PHP2 3 $cert _id=the serial number of your certificate;4 $device _token=your Devicetoken;5 $message= "Hello World";6 7 //This is a simple example of a more complex message format Please see the official Apple documentation8 $body=Array(9' APS ' =Array(' alert ' =$message)Ten ); One $apns=NewSaeapns (); A //push message to SAE push server - $result=$apns->push ($cert _id,$body,$device _token ); -  the if($result&&Is_array($result) ){ -     Echo' Send Success '; -     Var_dump($result ); -}Else { +     Echo' Send failed '; -     Var_dump($apns->errno (),$apns-errmsg ()); + } A  at //View summary information sent by the message, including the number of messages that were successfully sent on the current day, and the number of messages that failed to send for various reasons.  - $result=$apns->getinfo ($cert _id); - if($result===false) { -     Echo"GetInfo failed"; -     Var_dump($apns->errno (),$apns-errmsg ()); -}Else { in     Echo"GetInfo Success"; -     Var_dump($result); to } +  -?>

Third, the client code is as follows:

1. Register for push in Appdelegate:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions {     // Override point for customization after application launch.     // registering remote notifications    [Application registerusernotificationsettings:[uiusernotificationsettings Settingsfortypes: Uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound Categories:nil]];    [Application registerforremotenotifications];     return YES;}

  

2. Get the callback method for the device token

1 // get device tokens after registering for remote push 2 3 -(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) devicetoken4{5      6     NSLog (@ "%@", Devicetoken); 7 }

3. How to register the error callback:

-(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: ( Nserror *) error{    NSLog (@ "%@", [error localizeddescription]);}

4. Our push certificate and profile in the code signing in build settings

Here, you can send a notification to our device by calling the PHP file on the server.

Remote push for iOS development

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.