Class CloudPush

java.lang.Object
com.codename1.backend.push.CloudPush

public final class CloudPush extends Object

Sends push notifications through the Codename One push service.

CloudPush push = new CloudPush(appId, serverApiKey);
PushResult result = push.send(message, PushTarget.fromDeviceKey(deviceKey));
if (!result.isAccepted()) {
    log("push refused: " + result.getError());
}

appId is the application UUID the Console shows for the push application, and serverApiKey a server API key created with the push scope. The key is a credential: keep it in configuration, not in source, and never ship it in the app.

Each notification is one HTTPS request, made with Web on the calling thread. Every answer the service gives comes back as a PushResult, refusals included; only a request that got no answer at all throws. Nothing here logs, so the key and the device tokens reach no log through this class.

Instances are immutable and can be shared between threads.

  • Field Details

    • DEFAULT_ENDPOINT

      public static final String DEFAULT_ENDPOINT
      The production endpoint of the push service.
      See Also:
  • Constructor Details

    • CloudPush

      public CloudPush(String appId, String serverApiKey)
      A sender for one push application.
      Parameters:
      appId - the application UUID from the Console's push overview
      serverApiKey - a server API key with the push scope
      Throws:
      IllegalArgumentException - if either is null or blank
  • Method Details

    • withEndpoint

      public CloudPush withEndpoint(String url)
      A copy that sends to another URL: a stub in a test, or a gateway that forwards to the service.
      Parameters:
      url - the full URL of the messages endpoint
      Returns:
      a sender with the same application and key
      Throws:
      IllegalArgumentException - if the URL is null or blank
    • getEndpoint

      public String getEndpoint()
      Returns:
      the URL messages are sent to
    • getAppId

      public String getAppId()
      Returns:
      the application UUID messages are sent for
    • send

      public PushResult send(PushNotification message, PushTarget... targets) throws IOException
      Sends one message to one or more devices.
      Parameters:
      message - the message
      targets - the devices
      Returns:
      the service's answer, accepted or not
      Throws:
      IllegalArgumentException - if the message is null or has no body, data or surface command, or if there are no targets; checked before any request is made
      IOException - if the request failed without an answer: the service couldn't be reached, the connection broke, or it timed out
    • send

      public PushResult send(PushNotification message, List<PushTarget> targets) throws IOException
      Sends one message to one or more devices.
      Parameters:
      message - the message
      targets - the devices, each a PushTarget
      Returns:
      the service's answer, accepted or not
      Throws:
      IllegalArgumentException - if the message is null or has no body, data or surface command, or if the list is empty or holds something other than a PushTarget; checked before any request is made
      IOException - if the request failed without an answer: the service couldn't be reached, the connection broke, or it timed out
    • toString

      public String toString()
      The application only. The key is a credential, and a gateway URL can carry one in its query string, so neither goes anywhere a log might.
      Overrides:
      toString in class Object