public class Sender extends Object
Modifier and Type | Field and Description |
---|---|
protected static int |
BACKOFF_INITIAL_DELAY
Initial delay before first retry, without jitter.
|
protected Logger |
logger |
protected static int |
MAX_BACKOFF_DELAY
Maximum delay before a retry.
|
protected Random |
random |
protected static String |
UTF8 |
Constructor and Description |
---|
Sender(String key)
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
protected static void |
addParameter(StringBuilder body,
String name,
String value)
Adds a new parameter to the HTTP POST body.
|
protected HttpURLConnection |
getConnection(String url)
Gets an
HttpURLConnection given an URL. |
protected static String |
getString(InputStream stream)
Convenience method to convert an InputStream to a String.
|
protected static StringBuilder |
newBody(String name,
String value)
Creates a
StringBuilder to be used as the body of an HTTP POST. |
protected static Map<String,String> |
newKeyValues(String key,
String value)
Creates a map with just one key-value pair.
|
protected HttpURLConnection |
post(String url,
String body)
Make an HTTP post to a given URL.
|
protected HttpURLConnection |
post(String url,
String contentType,
String body) |
MulticastResult |
send(Message message,
List<String> regIds,
int retries)
Sends a message to many devices, retrying in case of unavailability.
|
Result |
send(Message message,
String registrationId,
int retries)
Sends a message to one device, retrying in case of unavailability.
|
MulticastResult |
sendNoRetry(Message message,
List<String> registrationIds)
Sends a message without retrying in case of service unavailability.
|
Result |
sendNoRetry(Message message,
String registrationId)
Sends a message without retrying in case of service unavailability.
|
protected static final String UTF8
protected static final int BACKOFF_INITIAL_DELAY
protected static final int MAX_BACKOFF_DELAY
protected final Random random
protected final Logger logger
public Sender(String key)
key
- API key obtained through the Google API Console.public Result send(Message message, String registrationId, int retries) throws IOException
Note: this method uses exponential back-off to retry in case of service unavailability and hence could block the calling thread for many seconds.
message
- message to be sent, including the device's registration id.registrationId
- device where the message will be sent.retries
- number of retries in case of service unavailability errors.IllegalArgumentException
- if registrationId is null.InvalidRequestException
- if GCM didn't returned a 200 or 503 status.IOException
- if message could not be sent.public Result sendNoRetry(Message message, String registrationId) throws IOException
send(Message, String, int)
for more info.InvalidRequestException
- if GCM didn't returned a 200 or 503 status.IllegalArgumentException
- if registrationId is null.IOException
public MulticastResult send(Message message, List<String> regIds, int retries) throws IOException
Note: this method uses exponential back-off to retry in case of service unavailability and hence could block the calling thread for many seconds.
message
- message to be sent.regIds
- registration id of the devices that will receive
the message.retries
- number of retries in case of service unavailability errors.IllegalArgumentException
- if registrationIds is null or
empty.InvalidRequestException
- if GCM didn't returned a 200 or 503 status.IOException
- if message could not be sent.public MulticastResult sendNoRetry(Message message, List<String> registrationIds) throws IOException
send(Message, List, int)
for more info.IllegalArgumentException
- if registrationIds is null or
empty.InvalidRequestException
- if GCM didn't returned a 200 status.IOException
- if message could not be sent or received.protected HttpURLConnection post(String url, String body) throws IOException
IOException
protected HttpURLConnection post(String url, String contentType, String body) throws IOException
IOException
protected static final Map<String,String> newKeyValues(String key, String value)
protected static StringBuilder newBody(String name, String value)
StringBuilder
to be used as the body of an HTTP POST.name
- initial parameter for the POST.value
- initial value for that parameter.protected static void addParameter(StringBuilder body, String name, String value)
body
- HTTP POST bodyname
- parameter's namevalue
- parameter's valueprotected HttpURLConnection getConnection(String url) throws IOException
HttpURLConnection
given an URL.IOException
protected static String getString(InputStream stream) throws IOException
If the stream ends in a newline character, it will be stripped.
IOException
Copyright © 2014. All Rights Reserved.