gcmd.java (4490B)
1 /** 2 * Copyright 2015 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // package pushd_android; 18 19 import org.apache.commons.io.IOUtils; 20 import org.json.JSONArray; 21 import org.json.JSONObject; 22 import org.json.JSONException; 23 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.OutputStream; 27 import java.net.HttpURLConnection; 28 import java.net.URL; 29 30 // NOTE: 31 // This class emulates a server for the purposes of this sample, 32 // but it's not meant to serve as an example for a production app server. 33 // This class should also not be included in the client (Android) applicaiton 34 // since it includes the server's API key. For information on GCM server 35 // implementaion see: https://developers.google.com/cloud-messaging/server 36 public class pushd_android { 37 38 public static final String API_KEY = "API_KEY"; 39 40 public static void main(String[] args) { 41 if (args.length < 1 || args.length > 2 || args[0] == null) { 42 System.err.println("usage: ./gradlew run -Pmsg=\"MESSAGE\" [-Pto=\"DEVICE_TOKEN\"]"); 43 System.err.println(""); 44 System.err.println("Specify a test message to broadcast via GCM. If a device's GCM registration token is\n" + 45 "specified, the message will only be sent to that device. Otherwise, the message \n" + 46 "will be sent to all devices subscribed to the \"global\" topic."); 47 System.err.println(""); 48 System.err.println("Example (Broadcast):\n" + 49 "On Windows: .\\gradlew.bat run -Pmsg=\"<Your_Message>\"\n" + 50 "On Linux/Mac: ./gradlew run -Pmsg=\"<Your_Message>\""); 51 System.err.println(""); 52 System.err.println("Example (Unicast):\n" + 53 "On Windows: .\\gradlew.bat run -Pmsg=\"<Your_Message>\" -Pto=\"<Your_Token>\"\n" + 54 "On Linux/Mac: ./gradlew run -Pmsg=\"<Your_Message>\" -Pto=\"<Your_Token>\""); 55 System.exit(1); 56 } 57 try { 58 // Prepare JSON containing the GCM message content. What to send and where to send. 59 JSONObject jGcmData = new JSONObject(); 60 JSONObject jData = new JSONObject(); 61 jData.put("message", args[0].trim()); 62 // Where to send GCM message. 63 if (args.length > 1 && args[1] != null) { 64 jGcmData.put("to", args[1].trim()); 65 } else { 66 jGcmData.put("to", "/topics/global"); 67 } 68 // What to send in GCM message. 69 jGcmData.put("data", jData); 70 71 // Create connection to send GCM Message request. 72 URL url = new URL("https://android.googleapis.com/gcm/send"); 73 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 74 conn.setRequestProperty("Authorization", "key=" + API_KEY); 75 conn.setRequestProperty("Content-Type", "application/json"); 76 conn.setRequestMethod("POST"); 77 conn.setDoOutput(true); 78 79 // Send GCM message content. 80 OutputStream outputStream = conn.getOutputStream(); 81 outputStream.write(jGcmData.toString().getBytes()); 82 83 // Read GCM response. 84 InputStream inputStream = conn.getInputStream(); 85 String resp = IOUtils.toString(inputStream); 86 System.out.println(resp); 87 System.out.println("Check your device/emulator for notification or logcat for " + 88 "confirmation of the receipt of the GCM message."); 89 } catch (IOException e) { 90 System.out.println("Unable to send GCM message."); 91 System.out.println("Please ensure that API_KEY has been replaced by the server " + 92 "API key, and that the device's registration token is correct (if specified)."); 93 e.printStackTrace(); 94 } catch (JSONException e) { 95 System.out.println("caught JSONException"); 96 } 97 } 98 99 }