Link: http://blog.csdn.net/kongxx/article/details/8160493
Today, we are looking at the Server API of Urban airship. on the official website of Urban airship, we recommend a third-party open-source library named Ghost. To see how to use the server-side API of Urban airship, I decided to write a small program.
Today's test involves sending messages from urban airship to Android devices. Therefore, we assume that an Android device or simulator has installed a native app that can receive messages, for detailed steps, refer to my previous blog (http://blog.csdn.net/kongxx/article/details/8155916 ).
Next we will start today's test
1. Create a project first. MAVEN is used to create the project.
mvn archetype:generate -DgroupId=urbanairship.server -DartifactId=urbanairship-server -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2. Modify the Pom. xml file as follows:
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://maven.apache.org/POM/4.0.0"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>urbanairship.server</groupId><artifactId>urbanairship-server</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>urbanairship-server</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.1</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.8.1</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></build></project>
3. Create a test class. Replace the username, password, and apid.
package urbanairship.server;import java.io.UnsupportedEncodingException;import java.util.logging.Level;import java.util.logging.Logger;import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.StatusLine;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicHeader;import org.apache.http.util.EntityUtils;public class Test {private static Logger logger = Logger.getLogger(Test.class.getName());private static final String username = "This should be Application Key";private static final String password = "This should be Application Master Secret";private static final String apid = "This should be your APID of your android device";public static void main(String[] args) throws Exception {StringBuffer sb = new StringBuffer();sb.append("{\"android\": {\"alert\": \"hello world\"}, \"apids\": [\""+apid+"\"]}");String path = "https://go.urbanairship.com/api/push/";DefaultHttpClient httpClient = new DefaultHttpClient();httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));HttpPost httpPost = new HttpPost(path);httpPost.setHeader("Accept", "application/json");httpPost.setEntity(new JsonEntity(sb.toString()));logger.log(Level.INFO, "executing request: " + httpPost.getRequestLine());HttpResponse response = httpClient.execute(httpPost);StatusLine status = response.getStatusLine();int statusCode = status.getStatusCode();logger.log(Level.INFO, ""+statusCode);HttpEntity responseEntity = response.getEntity();logger.log(Level.INFO, EntityUtils.toString(responseEntity));}static private class JsonEntity extends StringEntity {public JsonEntity(String jsonString) throws UnsupportedEncodingException {super(jsonString, "UTF-8");}@Overridepublic Header getContentType() {Header h = new BasicHeader("Content-Type", "application/json");return h;}}}
4. Test: first run the simulator, then run the android native app that can receive the message, and finally run the test class above. Wait a moment and you will see the message on the device or simulator.