Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class CloudPush

  1. Object
  2. CloudPush

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.

Fields

public static final String DEFAULT_ENDPOINT = "https://cloud.codenameone.com/api/v3/push/messages"The production endpoint of the push service.

Constructors

public CloudPush(String appId, String serverApiKey)A sender for one push application.

Methods

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.
public String getEndpoint()
public String getAppId()
public PushResult send(PushNotification message, PushTarget... targets) throws IOExceptionSends one message to one or more devices.
public PushResult send(PushNotification message, List<PushTarget> targets) throws IOExceptionSends one message to one or more devices.
public String toString()The application only.

Inherited methods

Field details

DEFAULT_ENDPOINT

public static final String DEFAULT_ENDPOINT = "https://cloud.codenameone.com/api/v3/push/messages"
The production endpoint of the push service.

Constructor details

CloudPush

public CloudPush(String appId, String serverApiKey)
A sender for one push application.

Parameters

appId String
the application UUID from the Console’s push overview
serverApiKey String
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 String
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 PushNotification
the message
targets PushTarget...
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 PushNotification
the message
targets List<PushTarget>
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.