Class CloudPush
java.lang.Object
com.codename1.backend.push.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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe production endpoint of the push service. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetAppId()send(PushNotification message, PushTarget... targets) Sends one message to one or more devices.send(PushNotification message, List<PushTarget> targets) Sends one message to one or more devices.toString()The application only.withEndpoint(String url) A copy that sends to another URL: a stub in a test, or a gateway that forwards to the service.
-
Field Details
-
DEFAULT_ENDPOINT
-
-
Constructor Details
-
CloudPush
A sender for one push application.- Parameters:
appId- the application UUID from the Console's push overviewserverApiKey- a server API key with thepushscope- Throws:
IllegalArgumentException- if either is null or blank
-
-
Method Details
-
withEndpoint
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
- Returns:
- the URL messages are sent to
-
getAppId
- Returns:
- the application UUID messages are sent for
-
send
Sends one message to one or more devices.- Parameters:
message- the messagetargets- 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 madeIOException- if the request failed without an answer: the service couldn't be reached, the connection broke, or it timed out
-
send
Sends one message to one or more devices.- Parameters:
message- the messagetargets- the devices, each aPushTarget- 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 aPushTarget; checked before any request is madeIOException- if the request failed without an answer: the service couldn't be reached, the connection broke, or it timed out
-
toString
-