Add a new flag for gcm devices since the Chrome extension uses ac2dm

This commit is contained in:
felipeal
2012-08-17 18:54:06 +00:00
parent 138db46635
commit b92b6ab3f3
5 changed files with 38 additions and 15 deletions

View File

@@ -134,7 +134,8 @@ public class DeviceRegistrar {
// TODO: Allow device name to be configured // TODO: Allow device name to be configured
params.add(new BasicNameValuePair("deviceName", isTablet(context) ? "Tablet" : "Phone")); params.add(new BasicNameValuePair("deviceName", isTablet(context) ? "Tablet" : "Phone"));
params.add(new BasicNameValuePair("deviceType", "gcm")); params.add(new BasicNameValuePair("deviceType", "ac2dm"));
params.add(new BasicNameValuePair("gcm", "true"));
AppEngineClient client = new AppEngineClient(context, accountName); AppEngineClient client = new AppEngineClient(context, accountName);
return client.makeRequest(urlPath, params); return client.makeRequest(urlPath, params);

View File

@@ -248,7 +248,7 @@ public class C2DMessaging {
} }
} }
public Result send(Message message, String regId) { public Result sendGcmMessage(Message message, String regId) {
String key = serverConfig.getGcmApiKey(); String key = serverConfig.getGcmApiKey();
Sender sender = new Sender(key); Sender sender = new Sender(key);
try { try {

View File

@@ -19,8 +19,6 @@ package com.google.android.chrometophone.server;
import com.google.appengine.api.datastore.Key; import com.google.appengine.api.datastore.Key;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@@ -44,10 +42,6 @@ public class DeviceInfo {
public static final String TYPE_AC2DM = "ac2dm"; public static final String TYPE_AC2DM = "ac2dm";
public static final String TYPE_CHROME = "chrome"; public static final String TYPE_CHROME = "chrome";
public static final String TYPE_GCM = "gcm";
public static final List<String> SUPPORTED_TYPES = Collections
.unmodifiableList(Arrays.asList(TYPE_AC2DM, TYPE_CHROME, TYPE_GCM));
/** /**
* User-email # device-id * User-email # device-id
@@ -91,6 +85,13 @@ public class DeviceInfo {
@Persistent @Persistent
private Boolean debug; private Boolean debug;
/**
* Devices that migrated to GCM will have it set to true; older devices
* will have it either null or false.
*/
@Persistent
private Boolean gcm;
public DeviceInfo(Key key, String deviceRegistrationID) { public DeviceInfo(Key key, String deviceRegistrationID) {
this.key = key; this.key = key;
this.deviceRegistrationID = deviceRegistrationID; this.deviceRegistrationID = deviceRegistrationID;
@@ -127,6 +128,18 @@ public class DeviceInfo {
this.debug = debug; this.debug = debug;
} }
public Boolean getGcm() {
return gcm;
}
public void setGcm(Boolean gcm) {
this.gcm = gcm;
}
public boolean isC2DM() {
return gcm == null || !gcm;
}
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }

View File

@@ -68,6 +68,7 @@ public class RegisterServlet extends HttpServlet {
dijson.put("key", di.getKey().toString()); dijson.put("key", di.getKey().toString());
dijson.put("name", di.getName()); dijson.put("name", di.getName());
dijson.put("type", di.getType()); dijson.put("type", di.getType());
dijson.put("gcm", di.getGcm());
dijson.put("regid", di.getDeviceRegistrationID()); dijson.put("regid", di.getDeviceRegistrationID());
dijson.put("ts", di.getRegistrationTimestamp()); dijson.put("ts", di.getRegistrationTimestamp());
devices.put(dijson); devices.put(dijson);
@@ -112,6 +113,9 @@ public class RegisterServlet extends HttpServlet {
deviceType = "ac2dm"; deviceType = "ac2dm";
} }
String gcm = reqInfo.getParameter("gcm");
boolean isGcm = gcm != null && gcm.equalsIgnoreCase("true");
// Because the deviceRegistrationId isn't static, we use a static // Because the deviceRegistrationId isn't static, we use a static
// identifier for the device. (Can be null in older clients) // identifier for the device. (Can be null in older clients)
String deviceId = reqInfo.getParameter("deviceId"); String deviceId = reqInfo.getParameter("deviceId");
@@ -165,11 +169,12 @@ public class RegisterServlet extends HttpServlet {
} }
device.setName(deviceName); // update display name device.setName(deviceName); // update display name
device.setGcm(isGcm);
// TODO: only need to write if something changed, for chrome nothing // TODO: only need to write if something changed, for chrome nothing
// changes, we just create a new channel // changes, we just create a new channel
pm.makePersistent(device); pm.makePersistent(device);
log.log(Level.INFO, "Registered device " + reqInfo.userName + " " + log.log(Level.INFO, "Registered device " + reqInfo.userName + " " +
deviceType); deviceType + "(gcm: " + isGcm + ")");
if (device.getType().equals(DeviceInfo.TYPE_CHROME)) { if (device.getType().equals(DeviceInfo.TYPE_CHROME)) {
String channelId = String channelId =

View File

@@ -76,6 +76,7 @@ public class SendServlet extends HttpServlet {
deviceNames, deviceType); deviceNames, deviceType);
if (id.startsWith(ERROR_STATUS)) { if (id.startsWith(ERROR_STATUS)) {
log.warning("Error sending url to device of type " + deviceType + ": " + id);
resp.setStatus(500); resp.setStatus(500);
} }
resp.getWriter().println(id); resp.getWriter().println(id);
@@ -125,10 +126,11 @@ public class SendServlet extends HttpServlet {
deviceInfo, reqDebug, deviceInfo.getType()); deviceInfo, reqDebug, deviceInfo.getType());
} }
if (res == null) { if (res instanceof Boolean) {
log.info("Link sent to phone! collapse_key:" + collapseKey); ok = (Boolean) res;
ok = true; log.info("Link sent to phone: " + ok + "! collapse_key:" + collapseKey);
} else { } else {
log.fine("Non-boolean send result: " + res);
// C2DM error // C2DM error
if (res instanceof IOException) { if (res instanceof IOException) {
IOException ex = (IOException) res; IOException ex = (IOException) res;
@@ -147,10 +149,10 @@ public class SendServlet extends HttpServlet {
} }
// GCM result. // GCM result.
if (res instanceof Result) { if (res instanceof Result) {
log.info("GCM send result: " + res);
Result result = (Result) res; Result result = (Result) res;
String regId = deviceInfo.getDeviceRegistrationID(); String regId = deviceInfo.getDeviceRegistrationID();
if (result.getMessageId() != null) { if (result.getMessageId() != null) {
log.info("Link sent to phone! collapse_key:" + collapseKey);
ok = true; ok = true;
String canonicalRegId = result.getCanonicalRegistrationId(); String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) { if (canonicalRegId != null) {
@@ -213,7 +215,8 @@ public class SendServlet extends HttpServlet {
String regId = deviceInfo.getDeviceRegistrationID(); String regId = deviceInfo.getDeviceRegistrationID();
String debug = (deviceInfo.getDebug()) || reqDebug ? "1" : null; String debug = (deviceInfo.getDebug()) || reqDebug ? "1" : null;
Object res; Object res;
if (deviceType.equals(DeviceInfo.TYPE_AC2DM)) { if (deviceInfo.isC2DM()) {
log.fine("Sending C2DM message");
res = push.sendNoRetry(regId, res = push.sendNoRetry(regId,
collapseKey, collapseKey,
"url", url, "url", url,
@@ -221,6 +224,7 @@ public class SendServlet extends HttpServlet {
"sel", sel, "sel", sel,
"debug", debug); "debug", debug);
} else { } else {
log.fine("Sending GCM message");
Builder builder = new Message.Builder() Builder builder = new Message.Builder()
.collapseKey(collapseKey) .collapseKey(collapseKey)
.addData("url", url) .addData("url", url)
@@ -230,7 +234,7 @@ public class SendServlet extends HttpServlet {
builder.addData("debug", debug); builder.addData("debug", debug);
} }
Message message = builder.build(); Message message = builder.build();
res = push.send(message, regId); res = push.sendGcmMessage(message, regId);
} }
return res; return res;
} }