Java servlet mobile app access interface (4) Push and servletapp

Source: Internet
Author: User

Java servlet mobile app access interface (4) Push and servletapp

 

1. download and run the server DEMO.

After logging on to umeng, the browser directly enter the following URL, directly go to the document development and DEMO download page: http://dev.umeng.com/push/ios/integration (below)

1. Download the DEMO

 

2. After performing the preceding steps, import the downloaded java demo to Ecplise and run

Here, I am using the java console to run the DEMO case. The above right-click Demo. java to run. By default, the single mobile push method of android is executed, which can be modified here,

Because I tested IOS push, here I will change the running method in the main method to a single mobile push method in IOS. Below

Below are some of the parameters in the above call Method

After the modification, you can right-click the official DEMO and run DEMO. JAVA.

Returned results:

Remember that the IP address of the APP created by umeng must be changed to the IP address of the server; otherwise, an IP error will be returned.

 

 

2. Integrate the push function into the Servlet

I encountered many problems when integrating this DEMO. First, a self-encapsulated HTTP request method will return a 500 error during execution. So why did I ask their customers yesterday, but I didn't go to work on weekends, so you can leave it alone, and then use the HTTP request package integrated in the official DEMO to solve this problem.

1. Add the official HTTP request package to the Servlet application.

Copy. jar in the above circle to the lib folder under the Apatch-Tomcat path corresponding to the project,

Next, introduce the following classes to the corresponding Servlet page.

Import org. apache. commons. codec. digest. DigestUtils;
Import org. apache. http. HttpResponse;
Import org. apache. http. client. HttpClient;
Import org. apache. http. client. methods. HttpPost;
Import org. apache. http. entity. StringEntity;
Import org. apache. http. impl. client. DefaultHttpClient;

 

The following code is executed:

// UmentPushIosPrameter = new JSONObject (); try {// corresponding APP keyUmentPushIosPrameter. put ("appkey", "568f2ccbe0f55a3e050017? "); // The following is a string that converts time to time format. Here, the timestamp must use 10 UmentPushIosPrameter. put ("timestamp", System. currentTimeMillis ()/1000); // push type: single device UmentPushIosPrameter. put ("type", "unicast"); // test the TOKEN UmentPushIosPrameter. put ("device_tokens", "success"); UmentPushIosPrameter. put ("production_mode", "false"); // production_mode JSONObject payloadFirstPrameter = new JSONObje Ct (); // custom key value pair payloadFirstPrameter. put ("name", "Asad launch site"); JSONObject apsFirstPrameter = new JSONObject (); // push content String postContent = "Alex's legal framework"; apsFirstPrameter. put ("alert", postContent); apsFirstPrameter. put ("badge", "1"); apsFirstPrameter. put ("sound", "default"); payloadFirstPrameter. put ("aps", apsFirstPrameter); // Add it to the root node UmentPushIosPrameter. put ("payload", payloadFirstPrameter); // Add it to the root node Strin G GetEncryptStr = "POST" + "http://msg.umeng.com/api/send" + UmentPushIosPrameter. toString () + "pa4nfw4hz3mprilryuo2yjdwjlipkv? "; // Corresponding App Master Secret // encrypted String Ok1 = DigestUtils. md5Hex (GetEncryptStr); String unicastParameter = UmentPushIosPrameter. toString (); StringEntity se = new StringEntity (unicastParameter. toString (), "UTF-8"); String url = "http://msg.umeng.com/api/send" + "? Sign = "+ Ok1; HttpPost post = new HttpPost (url); post. setHeader ("User-Agent", USER_AGENT); post. setEntity (se); // Send the post request and get the response HttpResponse response1 = client.exe cute (post); int status = response1.getStatusLine (). getStatusCode (); System. out. println ("Response Code:" + status); BufferedReader rd = new BufferedReader (new InputStreamReader (response1.getEntity (). getConten T (); StringBuffer result = new StringBuffer (); String line = ""; while (line = rd. readLine ())! = Null) {result. append (line);} System. out. println (result. toString (); if (status = 200) {System. out. println ("Notification sent successfully. ");} else {System. out. println ("Failed to send the notification! ") ;}} Catch (JSONException e) {// TODO Auto-generated catch blocke. printStackTrace ();}

Of course, there are many push methods, including Android, which can be viewed directly in the official documentation and added with corresponding parameters.

Here I want to talk about a problem I encountered when splicing an MD5 encrypted string. The official DEMO is to splice the App Master Secret: This splicing parameter. getBytes ("utf8") is converted to Byte type, but here I use the same method and return an encrypted token error. Then I will not convert this parameter to Byte type, it is passed.

 
String GetEncryptStr = "POST" + "http://msg.umeng.com/api/send" + UmentPushIosPrameter. toString () + "pa4nfw4hz3mprilryuo2yjdwjlipkv? "; // Corresponding App Master Secret
 

  

 

 

 

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.