1. Client Programming
The push notification technology can run on Mac OS X and iOS systems. This chapter mainly introduces the programming of iOS clients. The push notification programming is relatively simple. The key to programming is to obtain the token, this is returned from APNS and then submitted to the content provider. Let's take a look at some preparations before development.
Configure the Xcode Project
You need to configure the iOS push application in the Xcode project. These configurations are mainly used to set the code signature ID. The configuration profile (Provisioning Profiles) is the prerequisite for the code signature ID ).
With the configuration profile, you can set the code signature identifier. You need to download the profile to your local computer. You need to select this configuration profile for the code signature identifier. Select TAGETS → MyNotes → Code Signing Identity and select your own Code signature ID.
After setting, you can start encoding.
Code Implementation
The Code Implementation of push notifications consists of two steps: the first step is to register and receive notifications, and the second step is to receive registration results. These tasks are implemented in the application delegate object AppDelegate.
Let's first look at the registration code:
- (BOOL)application:(UIApplication **||=
The registration process is long. It returns the callback method code from Apple through APNS after registration:
- ()application:(UIApplication**%*tokeStr = [NSString stringWithFormat: ([tokeStr length] == * = [NSCharacterSet characterSetWithCharactersInString:= [tokeStr stringByTrimmingCharactersInSet:= [tokeStr stringByReplacingOccurrencesOfString: withString:*strURL = @”http:*url =*request = forKey:- ()application:(UIApplication**%
2. Push Server programming
The content provider receives and saves the device token. When new content needs to be pushed, they will start a service program to push their content one by one. In the specific push process, it is not directly sent by the content provider to the user's device. Instead, the Service Program establishes a trusted connection with APNS and then pushes the data to APNS, then, APNS uses a secure channel to push data to user devices.
To write the PUSH Service Program of the content provider, We need to program SSL authentication and construct APNS data packets. The data packets are divided into three main parts: Command and deviceToken) and Payload ). The load cannot exceed 256 bytes, which is in JSON format. For example:
As a service program launched, it can be implemented in many computer languages. From the perspective of ease of management, PHP, Java,. NET, and even Note. js can be used. This book focuses on writing push service programs in PHP and Java.
Push Service using PHP
PHP is a very good server-side script. It has not been eroded by JavaEE and. NET for so many years, which indicates that it has extraordinary advantages. PHP programming is simple for many people. It is also easy to implement the PUSH Service in this chapter. The following code implements the pushed PHP code:
<? = Array ("aps" => array ("alert" => 'Happy New Year. from php', "badge" =>, "sound" => '= "apns-= stream_socket_client (" ssl :(! = Chr (). pack ("n",). pack ("H *?>
Open the terminal window and execute the following command:
$ Openssl pkcs12-in
Enter Import Password:
MAC verified OK
Enter the password after you press Enter. This password is set when "certificate. p12" is exported.
PHP can be run after the PHP code is compiled. There are two ways to run PHP. One is to put this file under the Apache HTTP Server Directory and ensure that PHP is installed under Apache, run it in the browser and enter http: // localhost/phpPNs/Pusher in the browser. php. This URL is the php file on my Apache HTTP server.
The other method is much simpler. We don't need to install the Apache HTTP Server. We only need to install the PHP interpreter. We can run the following command on the terminal:
$ Php-f Pusher. php
Connection
Send message
In this way, the notification can be pushed. If everything is pushed successfully, the user's device can receive the notification.
Use
The above describes the PUSH Service program implemented by PHP, and the basic process of writing in other languages is the same. This section describes the Java PUSH Service Program. Because of the specific process and PHP, here we no longer write their own Java implementation code, but to use someone else has encapsulated javapns (http://code.google.com/p/javapns/) class library, it encapsulates socket Implementation Details and makes development easier.
The following code implements the pushed Java code:
=/Certificate. p12 "," 51work6.com ",
The code above also depends on the following class libraries: bcprov-jdk15-146.jar, JavaPNS_2.2.jar, and log4j-1.2.15.jar. The bcprov-jdk15-146.jar and JavaPNS_2.2.jar can be downloaded in the http://code.google.com/p/javapns. Log4j-1.2.15.jar to http://logging.apache.org/log4j/1.2/download.html download.
We will not introduce the running of this Java program. It is a Java Application that can run in a jre environment. We can make it into a Java Web program so that we can send notifications in the browser.