java.lang.Object
org.apache.fineract.infrastructure.gcm.domain.Sender

public class Sender extends Object
Helper class to send messages to the GCM service using an API Key.
  • Field Details

    • BACKOFF_INITIAL_DELAY

      protected static final int BACKOFF_INITIAL_DELAY
      Initial delay before first retry, without jitter.
      See Also:
    • MAX_BACKOFF_DELAY

      protected static final int MAX_BACKOFF_DELAY
      Maximum delay before a retry.
      See Also:
    • LOG

      protected static final org.slf4j.Logger LOG
  • Constructor Details

    • Sender

      public Sender(String key, String endpoint)
      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

      public String 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

      public Result send(Message message, String to, int retries) throws IOException
      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

      public Result sendNoRetry(Message message, String to) throws IOException
      Sends a message without retrying in case of service unavailability. See send(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

      public MulticastResult send(Message message, List<String> regIds, int retries) throws IOException
      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. See send(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

      protected HttpURLConnection post(String url, String body) throws IOException
      Make an HTTP post to a given URL.
      Returns:
      HTTP response.
      Throws:
      IOException
    • post

      protected HttpURLConnection post(String url, String contentType, String body) throws IOException
      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

      protected static final Map<String,String> newKeyValues(String key, String value)
      Creates a map with just one key-value pair.
    • newBody

      protected static StringBuilder newBody(String name, String value)
      Creates a StringBuilder to 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

      protected static void addParameter(StringBuilder body, String name, String value)
      Adds a new parameter to the HTTP POST body.
      Parameters:
      body - HTTP POST body.
      name - parameter's name.
      value - parameter's value.
    • getConnection

      protected HttpURLConnection getConnection(String url) throws IOException
      Gets an HttpURLConnection given an URL.
      Throws:
      IOException
    • getString

      protected static String getString(InputStream stream) throws IOException
      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