Class Sender
java.lang.Object
org.apache.fineract.infrastructure.gcm.domain.Sender
Helper class to send messages to the GCM service using an API Key.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intInitial delay before first retry, without jitter.protected static final org.slf4j.Loggerprotected static final intMaximum delay before a retry. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static voidaddParameter(StringBuilder body, String name, String value) Adds a new parameter to the HTTP POST body.protected HttpURLConnectiongetConnection(String url) Gets anHttpURLConnectiongiven an URL.protected static StringgetString(InputStream stream) Convenience method to convert an InputStream to a String.protected static StringBuilderCreates aStringBuilderto be used as the body of an HTTP POST.newKeyValues(String key, String value) Creates a map with just one key-value pair.protected HttpURLConnectionMake an HTTP post to a given URL.protected HttpURLConnectionMakes an HTTP POST request to a given endpoint.Sends a message to one device, retrying in case of unavailability.Sends a message to many devices, retrying in case of unavailability.sendNoRetry(Message message, String to) Sends a message without retrying in case of service unavailability.sendNoRetry(Message message, List<String> registrationIds) Sends a message without retrying in case of service unavailability.final voidsetConnectTimeout(int connectTimeout) Set the underlying URLConnection's connect timeout (in milliseconds).final voidsetReadTimeout(int readTimeout) Set the underlying URLConnection's read timeout (in milliseconds).
-
Field Details
-
BACKOFF_INITIAL_DELAY
protected static final int BACKOFF_INITIAL_DELAYInitial delay before first retry, without jitter.- See Also:
-
MAX_BACKOFF_DELAY
protected static final int MAX_BACKOFF_DELAYMaximum delay before a retry.- See Also:
-
LOG
protected static final org.slf4j.Logger LOG
-
-
Constructor Details
-
Sender
Full options constructor.- Parameters:
key- FCM Server Key obtained through the Firebase Web Console.endpoint- Endpoint to use when sending the message.
-
-
Method Details
-
getEndpoint
-
setConnectTimeout
public final void setConnectTimeout(int connectTimeout) Set the underlying URLConnection's connect timeout (in milliseconds). A timeout value of 0 specifies an infinite timeout.Default is the system's default timeout.
- See Also:
-
setReadTimeout
public final void setReadTimeout(int readTimeout) Set the underlying URLConnection's read timeout (in milliseconds). A timeout value of 0 specifies an infinite timeout.Default is the system's default timeout.
- See Also:
-
send
Sends a message to one device, retrying in case of unavailability.Note: this method uses exponential back-off to retry in case of service unavailability and hence could block the calling thread for many seconds.
- Parameters:
message- message to be sent, including the device's registration id.to- registration token, notification key, or topic where the message will be sent.retries- number of retries in case of service unavailability errors.- Returns:
- result of the request (see its javadoc for more details).
- Throws:
IllegalArgumentException- if to is null.InvalidRequestException- if GCM didn't returned a 200 or 5xx status.IOException- if message could not be sent.
-
sendNoRetry
Sends a message without retrying in case of service unavailability. Seesend(Message, String, int)for more info.- Returns:
- result of the post, or null if the GCM service was unavailable or any network exception caused the request to fail, or if the response contains more than one result.
- Throws:
InvalidRequestException- if GCM didn't returned a 200 status.IllegalArgumentException- if to is null.IOException
-
send
Sends a message to many devices, retrying in case of unavailability.Note: this method uses exponential back-off to retry in case of service unavailability and hence could block the calling thread for many seconds.
- Parameters:
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.- Returns:
- combined result of all requests made.
- Throws:
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.
-
sendNoRetry
public MulticastResult sendNoRetry(Message message, List<String> registrationIds) throws IOException Sends a message without retrying in case of service unavailability. Seesend(Message, List, int)for more info.- Returns:
- multicast results if the message was sent successfully, null if it failed but could be retried.
- Throws:
IllegalArgumentException- if registrationIds is null or empty.InvalidRequestException- if GCM didn't returned a 200 status.IOException- if there was a JSON parsing error
-
post
Make an HTTP post to a given URL.- Returns:
- HTTP response.
- Throws:
IOException
-
post
Makes an HTTP POST request to a given endpoint.Note: the returned connected should not be disconnected, otherwise it would kill persistent connections made using Keep-Alive.
- Parameters:
url- endpoint to post the request.contentType- type of request.body- body of the request.- Returns:
- the underlying connection.
- Throws:
IOException- propagated from underlying methods.
-
newKeyValues
Creates a map with just one key-value pair. -
newBody
Creates aStringBuilderto be used as the body of an HTTP POST.- Parameters:
name- initial parameter for the POST.value- initial value for that parameter.- Returns:
- StringBuilder to be used an HTTP POST body.
-
addParameter
Adds a new parameter to the HTTP POST body.- Parameters:
body- HTTP POST body.name- parameter's name.value- parameter's value.
-
getConnection
Gets anHttpURLConnectiongiven an URL.- Throws:
IOException
-
getString
Convenience method to convert an InputStream to a String.If the stream ends in a newline character, it will be stripped.
If the stream is null, returns an empty string.
- Throws:
IOException
-