diff --git a/java-post-gcm/.classpath b/java-post-gcm/.classpath new file mode 100644 index 0000000..e6f2836 --- /dev/null +++ b/java-post-gcm/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/java-post-gcm/pom.xml b/java-post-gcm/pom.xml new file mode 100644 index 0000000..f03142f --- /dev/null +++ b/java-post-gcm/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + + com.hmkcode + java-post-gcm + 1.0-SNAPSHOT + jar + + java-post-gcm + http://maven.apache.org + + + UTF-8 + + + + + com.fasterxml.jackson.core + jackson-core + 2.2.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.2.2 + + + com.fasterxml.jackson.core + jackson-annotations + 2.2.2 + + + junit + junit + 3.8.1 + test + + + diff --git a/java-post-gcm/src/main/java/com/hmkcode/App.java b/java-post-gcm/src/main/java/com/hmkcode/App.java new file mode 100644 index 0000000..8a2704d --- /dev/null +++ b/java-post-gcm/src/main/java/com/hmkcode/App.java @@ -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; + } +} diff --git a/java-post-gcm/src/main/java/com/hmkcode/POST2GCM.java b/java-post-gcm/src/main/java/com/hmkcode/POST2GCM.java new file mode 100644 index 0000000..dfec71e --- /dev/null +++ b/java-post-gcm/src/main/java/com/hmkcode/POST2GCM.java @@ -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(); + } + } +} + diff --git a/java-post-gcm/src/main/java/com/hmkcode/vo/Content.java b/java-post-gcm/src/main/java/com/hmkcode/vo/Content.java new file mode 100644 index 0000000..90d7c0e --- /dev/null +++ b/java-post-gcm/src/main/java/com/hmkcode/vo/Content.java @@ -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 registration_ids; + private Map data; + + + public void addRegId(String regId){ + if(registration_ids == null) + registration_ids = new LinkedList(); + registration_ids.add(regId); + } + + public void createData(String title, String message){ + if(data == null) + data = new HashMap(); + + data.put("title", title); + data.put("message", message); + } + + + public List getRegistration_ids() { + return registration_ids; + } + + public void setRegistration_ids(List registration_ids) { + this.registration_ids = registration_ids; + } + + public Map getData() { + return data; + } + + public void setData(Map data) { + this.data = data; + } +} diff --git a/java-post-gcm/src/test/java/com/hmkcode/AppTest.java b/java-post-gcm/src/test/java/com/hmkcode/AppTest.java new file mode 100644 index 0000000..2723e91 --- /dev/null +++ b/java-post-gcm/src/test/java/com/hmkcode/AppTest.java @@ -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 ); + } +}