bakuposter.gcm.server.POST2GCMessage.java Source code

Java tutorial

Introduction

Here is the source code for bakuposter.gcm.server.POST2GCMessage.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bakuposter.gcm.server;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 *
 * @author Rashad Javadov
 */
public class POST2GCMessage {

    public static void post(String apiKey, Content content) {

        try {
            URL url = new URL("https://android.googleapis.com/gcm/send");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Authorization", "key=" + apiKey);
            conn.setDoOutput(true);
            ObjectMapper mapper = new ObjectMapper();
            System.out.println(content.getRegistration_ids().get(0));
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            mapper.writeValue(wr, content);
            wr.flush();
            wr.close();
            int responseCode = conn.getResponseCode();
            System.out.println("responseCode = " + responseCode);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}