Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
Add priority parameter to server.
Browse files Browse the repository at this point in the history
  • Loading branch information
kroikie committed Sep 8, 2015
1 parent 545e15b commit 4416a77
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public final class Constants {
*/
public static final String PARAM_TIME_TO_LIVE = "time_to_live";

/**
* Prefix to HTTP parameter used to set the message priority.
*/
public static final String PARAM_PRIORITY = "priority";

/**
* Too many messages sent by the sender. Retry after a while.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* <strong>Message with optional attributes and payload data:</strong>
* <pre><code>
* Message message = new Message.Builder()
* .priority("normal")
* .collapseKey(collapseKey)
* .timeToLive(3)
* .delayWhileIdle(true)
Expand All @@ -64,6 +65,7 @@ public final class Message implements Serializable {
private final Map<String, String> data;
private final Boolean dryRun;
private final String restrictedPackageName;
private final String priority;

public static final class Builder {

Expand All @@ -75,6 +77,7 @@ public static final class Builder {
private Integer timeToLive;
private Boolean dryRun;
private String restrictedPackageName;
private String priority;

public Builder() {
this.data = new LinkedHashMap<String, String>();
Expand Down Expand Up @@ -128,6 +131,14 @@ public Builder restrictedPackageName(String value) {
return this;
}

/**
* Sets the priority property.
*/
public Builder priority(String value) {
priority = value;
return this;
}

public Message build() {
return new Message(this);
}
Expand All @@ -141,6 +152,7 @@ private Message(Builder builder) {
timeToLive = builder.timeToLive;
dryRun = builder.dryRun;
restrictedPackageName = builder.restrictedPackageName;
priority = builder.priority;
}

/**
Expand Down Expand Up @@ -178,6 +190,13 @@ public String getRestrictedPackageName() {
return restrictedPackageName;
}

/**
* Gets the message priority value.
*/
public String getPriority() {
return priority;
}

/**
* Gets the payload data, which is immutable.
*/
Expand All @@ -188,6 +207,9 @@ public Map<String, String> getData() {
@Override
public String toString() {
StringBuilder builder = new StringBuilder("Message(");
if (priority != null) {
builder.append("priority=").append(priority).append(", ");
}
if (collapseKey != null) {
builder.append("collapseKey=").append(collapseKey).append(", ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static com.google.android.gcm.server.Constants.PARAM_DELAY_WHILE_IDLE;
import static com.google.android.gcm.server.Constants.PARAM_DRY_RUN;
import static com.google.android.gcm.server.Constants.PARAM_PAYLOAD_PREFIX;
import static com.google.android.gcm.server.Constants.PARAM_PRIORITY;
import static com.google.android.gcm.server.Constants.PARAM_REGISTRATION_ID;
import static com.google.android.gcm.server.Constants.PARAM_RESTRICTED_PACKAGE_NAME;
import static com.google.android.gcm.server.Constants.PARAM_TIME_TO_LIVE;
Expand Down Expand Up @@ -152,6 +153,10 @@ public Result send(Message message, String registrationId, int retries)
public Result sendNoRetry(Message message, String registrationId)
throws IOException {
StringBuilder body = newBody(PARAM_REGISTRATION_ID, registrationId);
String priority = message.getPriority();
if (priority != null) {
addParameter(body, PARAM_PRIORITY, priority);
}
Boolean delayWhileIdle = message.isDelayWhileIdle();
if (delayWhileIdle != null) {
addParameter(body, PARAM_DELAY_WHILE_IDLE, delayWhileIdle ? "1" : "0");
Expand Down Expand Up @@ -393,6 +398,7 @@ public MulticastResult sendNoRetry(Message message,
throw new IllegalArgumentException("registrationIds cannot be empty");
}
Map<Object, Object> jsonRequest = new HashMap<Object, Object>();
setJsonField(jsonRequest, PARAM_PRIORITY, message.getPriority());
setJsonField(jsonRequest, PARAM_TIME_TO_LIVE, message.getTimeToLive());
setJsonField(jsonRequest, PARAM_COLLAPSE_KEY, message.getCollapseKey());
setJsonField(jsonRequest, PARAM_RESTRICTED_PACKAGE_NAME, message.getRestrictedPackageName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void testRequiredParameters() {
@Test
public void testOptionalParameters() {
Message message = new Message.Builder()
.priority("high")
.collapseKey("108")
.delayWhileIdle(true)
.timeToLive(42)
Expand All @@ -55,6 +56,7 @@ public void testOptionalParameters() {
.addData("k1", "v1")
.addData("k2", "v2")
.build();
assertEquals("high", message.getPriority());
assertEquals("108", message.getCollapseKey());
assertTrue(message.isDelayWhileIdle());
assertEquals(42, message.getTimeToLive().intValue());
Expand All @@ -65,6 +67,7 @@ public void testOptionalParameters() {
assertEquals("v1", data.get("k1"));
assertEquals("v2", data.get("k2"));
String toString = message.toString();
assertTrue(toString.contains("priority=high"));
assertTrue(toString.contains("collapseKey=108"));
assertTrue(toString.contains("timeToLive=42"));
assertTrue(toString.contains("delayWhileIdle=true"));
Expand Down

0 comments on commit 4416a77

Please sign in to comment.