iOS development: Push notification brief and development practice

Source: Internet
Author: User
Tags openssl openssl rsa pkcs12 ssl certificate

Turn from: http://luoyl.info/blog/2012/02/apple_push_notification_guide/



I. About PUSH notifications

Push notifications, also called remote notifications, are features that were introduced after iOS 3.0. is a way to tell a user that a new message is being sent from an external server to the application when the program is not started or is not running in the foreground. Generally speaking, when you want to display a message or download data, the notification is sent by the remote server (the provider of the program) and then pushed to the device via the Apple push Notification service (Apple Push, APNs).

A new message that is pushed may be a message, a schedule that is about to expire, or a new data on a remote server. When displayed on the system, you can display a warning message or display a number on the program icon, and you can also play a warning tone. Once the user notices that the program has new information, time, or data, they can run the program and access the new content. You can also choose to ignore the notification, and the program will not be activated.

IPhone, ipad and ipod touch at the same time only one app runs in the foreground. When most programs run in the background, they can respond to content that is of interest to some users (timing, data, etc.). Push notifications allow the program to notify the user when these events occur.

As a provider, you must obtain an SSL certificate through the iOS Developer program portal as a push notification for the application development and deployment. Each certificate limit is used for a program, using the program's bundle ID as an identity. Certificates are used for two purposes: one for sandbox (for development and testing) and the other for publishing products. These two operating environments have the respective IP addresses specified and require a different certificate. Provisioning profiles must also be obtained for each of the two different environments.

APNs offers two basic services: message push and feedback service.

Message push: The push notification is sent to the APNS as binary data using a streaming TCP socket. Message push has two interfaces for development and test sandbox, publishing products, each with its own address and port. Regardless of which interface you use, you need to use SSL certificates to establish a secure channel through TLS or SSL. The provider compiles the notification information and sends it to APNS through this channel.
Note: sandbox:gateway.sandbox.push.apple.com:219
Product Interface: gateway.push.apple.com:2195

Feedback Service: You can get a record of sending failures for a program. Providers should use feedback services to periodically check which devices have not been notified, to send notifications to these devices repeatedly, and to reduce the burden on push servers.
Note: sandbox:feedback.push.apple.com:2196
Product interface: feedback.sandbox.push.apple.com:2196 Two. The working mechanism of Apple push notification

Here is a full push flowchart
From the above figure, we can see.  The first is the application registration message push. iOS and APNs server are devicetoken.  The application accepts Devicetoken.  The application sends Devicetoken to the push service-side program (Provider).  The server-side program sends messages to the APNS service. The APNs service sends messages to the iphone application.

Both the iphone client and APNs, or provider and APNs, need to connect through the certificate:

Figure,

1. Provider refers to a push server for an iphone software that is the server we are going to develop.

2. APNS is an acronym for the Apple Push Notification Service (AAPL), which is the MAC server.

The above diagram can be divided into three stages:

The first stage: Push server (provider) to send the message, the purpose of the iphone logo packaging, issued to APNs;

Phase two: APNs the iphone in its own list of registered push services, looking for a corresponding logo, and sending the message to the iphone;

Phase III: The iphone sends messages to the appropriate application and pops the push notification as set. Three. Development of certificates and the configuration of push certificates

1. Login to iOS provisioning using the developer account, select or create a new app Id, take "info.luoyl.iostest" as an example

2. After the creation, enter the app ID list, you can see that the new app ID is not activated by default, click on the Configure link, enter the Push function activation page:

3. On the "Enable for Apple Push Notification Service" option, tick the "Configure" button at the row point:

4. A window will pop up at this point "continue"

5. Eject the Certificate Upload page, the certificate chooses beforehand completes "certificatesigningrequest.certsigningrequest", then the point "Generate" button;

6. Next there will be "Your APNs SSL certificate has been generated." Hint, point "continue":

7. Download the newly generated certificate "Aps_development.cer" to the computer:

8. Thus, AppID's development Push SSL certificate has become "enabled":

9. Make a developer test certificate, AppID designated as "Info.luoyl.iostest", download and double-click to install to the computer:

10. Double-click the "aps_development.cer" Download in step 7 to install to Keychain access:

11. Select the push services certificate, the right key to export the certificate for Personal Information exchange (. p12) format file, here I named "APS_DEVELOPMENT.P12", point store will pop up a password Settings window, you can leave blank:

12. Implement the following command at the terminal to convert the personal Information exchange (. p12) format file that you just exported to the push certificate of the push server: OpenSSL pkcs12-clcerts-nokeys-out cert.pem-in aps_ DEVELOPMENT.P12 OpenSSL pkcs12-nocerts-out key.pem-in aps_development.p12 OpenSSL rsa-in key.pem-out key.unencr Ypted.pem cat Cert.pem Key.unencrypted.pem > Iostest_push_dev.pems

The above command in the execution of 4 is required to enter the password, of which 1 and 2 direct return, 3 must set a key such as "push", in 4 input 3 set the key "push";
The "Iostest_push_dev.pem" that is generated after the command is executed is the push certificate that we push the server to use;

After the above steps have been configured, the required conditions for the development of the push function have been completed, and then a new iOS application will be created to experience the interface needed to implement the push function in iOS applications.

Four. Develop iOS application with push function

To enable the application to support the push function, our project configuration should be noted: Bundle Identifier, Code signing specified development certificate binding AppID to and push certificate binding AppID consistent (see figure below); If the development certificate in the project was created before the AppID activation push function, you must regenerate one. A development certificate that supports push will have an extra license string named "Aps-environment" than the old certificate, and if you continue to use the old certificate, an error "Cannot find the" "aps-environment" right string for the application when the program initiates an attempt to register the push function; The test requires a real machine, which is not supported by the simulator.


In terms of code, push registration, monitoring, and processing are all concentrated in the Appdelegate class:

1. (BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
Two functions are mainly implemented in the method body:
First, the completion of the PUSH function registration request, that is, when the program started to eject whether the use of push function;
Second, the implementation of the program start is triggered by the push message window, where you can handle the push content;-  (BOOL) Application: (uiapplication *) application  Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions    {        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]  bounds]] autorelease];        // Override point for customization after  application launch.        self.viewcontroller = [[[ viewcontroller alloc] init] autorelease];        self.window.rootViewController = self.viewController;        [self.window setbackgroundcolor:[uicolor colorwithpatternimage:[ uiimage imagenamed:@ "Background.png"]];        [self.window makeKeyAndVisible];        /**  Register push notification function, */       [[UIApplication sharedApplication]  Registerforremotenotificationtypes: (Uiremotenotificationtypealert | uiremotenotificationtypebadge  | uiremotenotificationtypesound)];                //judgment program is not done by push service         if  (launchoptions)  {            nsdictionary* pushnotificationkey = [launchoptions objectforkey: Uiapplicationlaunchoptionsremotenotificationkey];            if  (pushnotificationkey)  {               uialertview *alert = [[ uialertview alloc]initwithtitle:@ "Push notification"                                            &nbs

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.