GCM messege Sender

November 27, 2015 , 0 Comments

We create the method to used messege send one mobile to another mobile its just simple use this method.

post method to GCM :
public void post2GCM() throws Exception {
URL url = new URL("https://android.googleapis.com/gcm/send");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + apiKey); // here your Application id
conn.setDoOutput(true);
// JSON message format accepted by GCM

// set key which provided by GCM use this key
String input = "{\"registration_ids\" : [\"APA91bGhiA5tEGfqov_c5Vpeafh-8S484SXExnLWRyaVJrhcPFlpe7HohhZ4ZCU1ysMB1d-I3CKQL-l63zAUycrjGf8amLNqbHu_AcLv2X1rymm342hHtfg5QpoVVXYiNpCp6Obgc09L\"],\"data\" : {\"message\": \"hai welcome\"},}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());

// To send notification
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());

os.flush();
}

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Popular Posts