parent
3bcb42721d
commit
7cefe84329
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
|
||||||
|
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/core/jackson-core/2.2.2/jackson-core-2.2.2.jar"/>
|
||||||
|
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/core/jackson-databind/2.2.2/jackson-databind-2.2.2.jar"/>
|
||||||
|
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/core/jackson-annotations/2.2.2/jackson-annotations-2.2.2.jar"/>
|
||||||
|
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
|
||||||
|
</classpath>
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.hmkcode</groupId>
|
||||||
|
<artifactId>java-post-gcm</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>java-post-gcm</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-core</artifactId>
|
||||||
|
<version>2.2.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.2.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-annotations</artifactId>
|
||||||
|
<version>2.2.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
package com.hmkcode;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.hmkcode.vo.Content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hello world!
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class App
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
System.out.println( "Sending POST to GCM" );
|
||||||
|
|
||||||
|
String apiKey = "AIzaSyB8azikXJKi_NjpWcVNJVO0dGFG1WuNJlg";
|
||||||
|
Content content = createContent();
|
||||||
|
|
||||||
|
POST2GCM.post(apiKey, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Content createContent(){
|
||||||
|
|
||||||
|
Content c = new Content();
|
||||||
|
|
||||||
|
c.addRegId("APA91bFqnQzp0z5IpXWdth1lagGQZw1PTbdBAD13c-UQ0T76BBYVsFrY96MA4SFduBW9RzDguLaad-7l4QWluQcP6zSoX1HSUaAzQYSmI93hMJQUYEdRLpBOpmAGkjckuoVFt6icRjgXKuOpqZEedWUvVsKOqRruXuAe3mDbkimcNAMpc7XMF8M");
|
||||||
|
c.createData("Test Title", "Test Message");
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,78 @@
|
|||||||
|
package com.hmkcode;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.hmkcode.vo.Content;
|
||||||
|
|
||||||
|
public class POST2GCM {
|
||||||
|
|
||||||
|
|
||||||
|
public static void post(String apiKey, Content content){
|
||||||
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
// 1. URL
|
||||||
|
URL url = new URL("https://android.googleapis.com/gcm/send");
|
||||||
|
|
||||||
|
// 2. Open connection
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
|
// 3. Specify POST method
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
|
||||||
|
// 4. Set the headers
|
||||||
|
conn.setRequestProperty("Content-Type", "application/json");
|
||||||
|
conn.setRequestProperty("Authorization", "key="+apiKey);
|
||||||
|
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
|
||||||
|
// 5. Add JSON data into POST request body
|
||||||
|
|
||||||
|
//`5.1 Use Jackson object mapper to convert Contnet object into JSON
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
// 5.2 Get connection output stream
|
||||||
|
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
|
||||||
|
|
||||||
|
// 5.3 Copy Content "JSON" into
|
||||||
|
mapper.writeValue(wr, content);
|
||||||
|
|
||||||
|
// 5.4 Send the request
|
||||||
|
wr.flush();
|
||||||
|
|
||||||
|
// 5.5 close
|
||||||
|
wr.close();
|
||||||
|
|
||||||
|
// 6. Get the response
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
System.out.println("\nSending 'POST' request to URL : " + url);
|
||||||
|
System.out.println("Response Code : " + responseCode);
|
||||||
|
|
||||||
|
BufferedReader in = new BufferedReader(
|
||||||
|
new InputStreamReader(conn.getInputStream()));
|
||||||
|
String inputLine;
|
||||||
|
StringBuffer response = new StringBuffer();
|
||||||
|
|
||||||
|
while ((inputLine = in.readLine()) != null) {
|
||||||
|
response.append(inputLine);
|
||||||
|
}
|
||||||
|
in.close();
|
||||||
|
|
||||||
|
// 7. Print result
|
||||||
|
System.out.println(response.toString());
|
||||||
|
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
package com.hmkcode.vo;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class Content implements Serializable {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private List<String> registration_ids;
|
||||||
|
private Map<String,String> data;
|
||||||
|
|
||||||
|
|
||||||
|
public void addRegId(String regId){
|
||||||
|
if(registration_ids == null)
|
||||||
|
registration_ids = new LinkedList<String>();
|
||||||
|
registration_ids.add(regId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createData(String title, String message){
|
||||||
|
if(data == null)
|
||||||
|
data = new HashMap<String,String>();
|
||||||
|
|
||||||
|
data.put("title", title);
|
||||||
|
data.put("message", message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<String> getRegistration_ids() {
|
||||||
|
return registration_ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistration_ids(List<String> registration_ids) {
|
||||||
|
this.registration_ids = registration_ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setData(Map<String, String> data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
package com.hmkcode;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest
|
||||||
|
extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the test case
|
||||||
|
*
|
||||||
|
* @param testName name of the test case
|
||||||
|
*/
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
super( testName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the suite of tests being tested
|
||||||
|
*/
|
||||||
|
public static Test suite()
|
||||||
|
{
|
||||||
|
return new TestSuite( AppTest.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rigourous Test :-)
|
||||||
|
*/
|
||||||
|
public void testApp()
|
||||||
|
{
|
||||||
|
assertTrue( true );
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue