Implementation of simple push notification (push Notification) on iOS

Source: Internet
Author: User
Tags pack php server php script ssl certificate

implementation of simple push notification (push Notification) on iOS

According to this very good tutorial (http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12), combine your own practice, write down a little note, For reference only:)

Because of the length, I list a simple catalogue, as follows

1 understanding the mechanism of Apple push notifications

2 Create App ID

3) Create a CSR file

4) Create provisioning profile file

5 Device Token in Xcode project

6 Create. Pem files

7 Write PHP server code, send notification 1, understand the Apple push notification mechanism

From the above flowchart, you can see an app that can receive push notifications, which requires 3 things:

App ID (uniquely identifies an app program) provisioning profile (app release requires it, so push notifications can only be tested on the real machine) Device Token (device identification, which is special in the push notification function)

And the server that can push notifications is 2 things:

SSL Certificate Private Key

(because I am not clear about the information encryption, so this does not explain)


It is noteworthy that APNs (Applepush Notification Service) Server completes the function of sending device token and notification content, and the 2 actions are passive, that is, the first action was initiated by the app, The second is initiated by the server that pushed the notification.

So much for me to understand. Below I follow the reference article to carry on the experiment.

2. Create App ID

Click on the "New App ID" button, as follows


The contents of the description can be arbitrary, Bundle Identifier (app ID Suffix) must be Bundle Identifier when the app project is created, as follows


Click "Submit", click "App IDs" in the left-hand navigation, and find the new app ID just created, as follows


Click "Configure" after the following

Check the "Enable for Apple Push Notification Service" and click on the Red "Configure" button, where only a certificate is obtained for development. A dialog box pops up, as follows

After clicking on "Continue", we will upload a CSR file, as follows


The following uses a keychain access (keychainaccess) application to create the CSR file (. certsigningrequest file) that is required above

3. Create CSR Files

Keychain Access is located in the/Applications/Utilities directory, and opens it as follows

Then the pop-up window is as follows.


UserEmail address can be written, Common name is the same, pay attention to check "Save to Disks", and then click "Continue". Quickly generate the required files to find it.


Go back to the page below and upload the Helloremotenotification.certsigningrequest file that you just created with keychainaccess.

Soon the required certificate is OK, as follows

Click on "Continue" and click "Done".

Found above status is enabled, and more "Download" button, click on it, download a file named "Aps_development.cer". Double-click to open it,

Locate the private key named "Helloremotenotification" in the "Keys" column above (note private key instead of public key), right-click it, and select Export Helloremotenotification "...", which will export a. p12 file (you need to enter a password), as follows (there are currently 3 files)


The following starts with the. p12 file you just created, creating the profile provision file

4. Create provisioningprofile files

In the image above, click on the "New profile" button, as follows

Fill in "Profile Name", tick "certificate", "APP ID" to select the correct, previously created ID, that is, pushnotification, and finally the association needs to test the true machine device. Click "Submit", as follows

Can see a provisioning profile file, click the "Download" button to download it, then we produced a total of 4 files, as follows

Double-click the "pushnotification.mobileprovision" file, or drag it into the Xcode.

In Xcode, locate the code signing item, as shown above, to drag a debug configuration powertrain into the provisioning profile corresponding to the iphone Developer.

5, Xcode project to obtain device Token

In the Application:didfinishlaunchingwithoptions: method, register to use remote notifications.

Add 2 methods, Application:didregisterforremotenotificationswithdevicetoken: and application: Didfailtoregisterforremotenotificationswitherror: Used to get device token and print errors. Run the Helloremotenotification project we built, if the above steps are correct, should print out device Token, as follows

There may also be an error as follows

6, create. Pem files

Convert an existing. cer file into a. pem file to convert an existing. p12 file into a. pem file (password required) Finally, merge the top 2. pem files into 1. Pem files (requires a new password)


APS_DEVELOPMENT.CER->HELLOREMOTENOTIFICATION.PEM (renamed to Helloremotenotificationcert.pem below)

Helloremotenotification.p12-> Helloremotenotificationkey.pem

Helloremotenotification.pem +helloremotenotificationkey.pem merged into CK2.PEM

7, write PHP server code, send notifications

<?php//Put your device token here (without spaces): $deviceToken = ' <xcode device of console output ';

Put your private key ' s passphrase here: $passphrase = ' < last entered password > ';

Put your alert: $message = ' I-i-push notification! ';
$ctx = Stream_context_create ();
Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', ' Ck2.pem ');

Stream_context_set_option ($ctx, ' SSL ', ' passphrase ', $passphrase); Open a connection to the APNs server $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $err, $ Errstr, stream_client_connect|.

Stream_client_persistent, $ctx); if (! $fp) Exit ("Failed to connect: $err $errstr".

PHP_EOL); Echo ' Connected to APNs '.

Php_eol;

Create the payload Body $body [' aps '] = Array (' Alert ' => $message, ' sound ' => ' default ');

Encode the payload as JSON $payload = Json_encode ($body); Build the binary notification $msg = Chr (0). Pack (' n ', 32). Pack (' h* ', $deviceToken). Pack (' n ', strlen ($payload)).

$payload;

Send it to the server $result = fwrite ($fp, $msg, strlen ($msg)); if (! $result) echo ' message is not delivered '.
Php_eol; else ECHO ' message successfully delivered '.

Php_eol; Close the connection to the server fclose ($FP);
Note: Modify the following two lines of code

$deviceToken = ' <xcode console output device token> ';

Put your private key ' s passphrase here:
$passphrase = ' < last entered password > ';
......
......
Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', ' Ck2.pem ');

Run the PHP script above, as follows

You can see the push notifications received on the ipad, like the following, which indicates successful practice.

Resources:

1, http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

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.