From e9489eda528108dded713794cc6fba6924661735 Mon Sep 17 00:00:00 2001 From: kberos Date: Wed, 3 Feb 2016 11:55:42 +0000 Subject: [PATCH] Added a constructor that passes the endpoint. Otherwise if default constructor is used, it sets the Default GCM_SEND_ENDPOINT --- .../com/google/android/gcm/server/Sender.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/client-libraries/java/rest-client/src/com/google/android/gcm/server/Sender.java b/client-libraries/java/rest-client/src/com/google/android/gcm/server/Sender.java index 25b1f3b..1bcf8ba 100644 --- a/client-libraries/java/rest-client/src/com/google/android/gcm/server/Sender.java +++ b/client-libraries/java/rest-client/src/com/google/android/gcm/server/Sender.java @@ -92,6 +92,7 @@ public class Sender { Logger.getLogger(Sender.class.getName()); private final String key; + private final String endpoint; /** * Default constructor. @@ -100,6 +101,18 @@ public class Sender { */ public Sender(String key) { this.key = nonNull(key); + this.endpoint = GCM_SEND_ENDPOINT; + } + + /** + * Additional constructor that passes the endpoint as well. Can be used for testing with a mock. + * + * @param key API key obtained through the Google API Console. + * @param endpoint An endpoint that can accept notification requests. Normally the GCM Endpoint. + */ + public Sender(String key, String endpoint) { + this.key = nonNull(key); + this.endpoint = nonNull(endpoint); } /** @@ -417,7 +430,7 @@ private String makeGcmHttpRequest(Map jsonRequest) throws Invali HttpURLConnection conn; int status; try { - conn = post(GCM_SEND_ENDPOINT, "application/json", requestBody); + conn = post(endpoint, "application/json", requestBody); status = conn.getResponseCode(); } catch (IOException e) { logger.log(Level.FINE, "IOException posting to GCM", e); @@ -618,7 +631,7 @@ protected static StringBuilder newBody(String name, String value) { protected static void addParameter(StringBuilder body, String name, String value) { nonNull(body).append('&') - .append(nonNull(name)).append('=').append(nonNull(value)); + .append(nonNull(name)).append('=').append(nonNull(value)); } /**