mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
Add a new flag for gcm devices since the Chrome extension uses ac2dm
This commit is contained in:
@@ -134,7 +134,8 @@ public class DeviceRegistrar {
|
||||
// TODO: Allow device name to be configured
|
||||
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);
|
||||
return client.makeRequest(urlPath, params);
|
||||
|
||||
@@ -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();
|
||||
Sender sender = new Sender(key);
|
||||
try {
|
||||
|
||||
@@ -19,8 +19,6 @@ package com.google.android.chrometophone.server;
|
||||
import com.google.appengine.api.datastore.Key;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@@ -44,10 +42,6 @@ public class DeviceInfo {
|
||||
|
||||
public static final String TYPE_AC2DM = "ac2dm";
|
||||
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
|
||||
@@ -91,6 +85,13 @@ public class DeviceInfo {
|
||||
@Persistent
|
||||
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) {
|
||||
this.key = key;
|
||||
this.deviceRegistrationID = deviceRegistrationID;
|
||||
@@ -127,6 +128,18 @@ public class DeviceInfo {
|
||||
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) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@@ -68,6 +68,7 @@ public class RegisterServlet extends HttpServlet {
|
||||
dijson.put("key", di.getKey().toString());
|
||||
dijson.put("name", di.getName());
|
||||
dijson.put("type", di.getType());
|
||||
dijson.put("gcm", di.getGcm());
|
||||
dijson.put("regid", di.getDeviceRegistrationID());
|
||||
dijson.put("ts", di.getRegistrationTimestamp());
|
||||
devices.put(dijson);
|
||||
@@ -112,6 +113,9 @@ public class RegisterServlet extends HttpServlet {
|
||||
deviceType = "ac2dm";
|
||||
}
|
||||
|
||||
String gcm = reqInfo.getParameter("gcm");
|
||||
boolean isGcm = gcm != null && gcm.equalsIgnoreCase("true");
|
||||
|
||||
// Because the deviceRegistrationId isn't static, we use a static
|
||||
// identifier for the device. (Can be null in older clients)
|
||||
String deviceId = reqInfo.getParameter("deviceId");
|
||||
@@ -165,11 +169,12 @@ public class RegisterServlet extends HttpServlet {
|
||||
}
|
||||
|
||||
device.setName(deviceName); // update display name
|
||||
device.setGcm(isGcm);
|
||||
// TODO: only need to write if something changed, for chrome nothing
|
||||
// changes, we just create a new channel
|
||||
pm.makePersistent(device);
|
||||
log.log(Level.INFO, "Registered device " + reqInfo.userName + " " +
|
||||
deviceType);
|
||||
deviceType + "(gcm: " + isGcm + ")");
|
||||
|
||||
if (device.getType().equals(DeviceInfo.TYPE_CHROME)) {
|
||||
String channelId =
|
||||
|
||||
@@ -76,6 +76,7 @@ public class SendServlet extends HttpServlet {
|
||||
deviceNames, deviceType);
|
||||
|
||||
if (id.startsWith(ERROR_STATUS)) {
|
||||
log.warning("Error sending url to device of type " + deviceType + ": " + id);
|
||||
resp.setStatus(500);
|
||||
}
|
||||
resp.getWriter().println(id);
|
||||
@@ -125,10 +126,11 @@ public class SendServlet extends HttpServlet {
|
||||
deviceInfo, reqDebug, deviceInfo.getType());
|
||||
}
|
||||
|
||||
if (res == null) {
|
||||
log.info("Link sent to phone! collapse_key:" + collapseKey);
|
||||
ok = true;
|
||||
if (res instanceof Boolean) {
|
||||
ok = (Boolean) res;
|
||||
log.info("Link sent to phone: " + ok + "! collapse_key:" + collapseKey);
|
||||
} else {
|
||||
log.fine("Non-boolean send result: " + res);
|
||||
// C2DM error
|
||||
if (res instanceof IOException) {
|
||||
IOException ex = (IOException) res;
|
||||
@@ -147,10 +149,10 @@ public class SendServlet extends HttpServlet {
|
||||
}
|
||||
// GCM result.
|
||||
if (res instanceof Result) {
|
||||
log.info("GCM send result: " + res);
|
||||
Result result = (Result) res;
|
||||
String regId = deviceInfo.getDeviceRegistrationID();
|
||||
if (result.getMessageId() != null) {
|
||||
log.info("Link sent to phone! collapse_key:" + collapseKey);
|
||||
ok = true;
|
||||
String canonicalRegId = result.getCanonicalRegistrationId();
|
||||
if (canonicalRegId != null) {
|
||||
@@ -213,7 +215,8 @@ public class SendServlet extends HttpServlet {
|
||||
String regId = deviceInfo.getDeviceRegistrationID();
|
||||
String debug = (deviceInfo.getDebug()) || reqDebug ? "1" : null;
|
||||
Object res;
|
||||
if (deviceType.equals(DeviceInfo.TYPE_AC2DM)) {
|
||||
if (deviceInfo.isC2DM()) {
|
||||
log.fine("Sending C2DM message");
|
||||
res = push.sendNoRetry(regId,
|
||||
collapseKey,
|
||||
"url", url,
|
||||
@@ -221,6 +224,7 @@ public class SendServlet extends HttpServlet {
|
||||
"sel", sel,
|
||||
"debug", debug);
|
||||
} else {
|
||||
log.fine("Sending GCM message");
|
||||
Builder builder = new Message.Builder()
|
||||
.collapseKey(collapseKey)
|
||||
.addData("url", url)
|
||||
@@ -230,7 +234,7 @@ public class SendServlet extends HttpServlet {
|
||||
builder.addData("debug", debug);
|
||||
}
|
||||
Message message = builder.build();
|
||||
res = push.send(message, regId);
|
||||
res = push.sendGcmMessage(message, regId);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user