我正在尝试使用一些基本信息响应设备,一旦它注册到我的服务器,但我的服务器的响应似乎是空的。在我指定command = extras.getString(“command”)的行下面,命令为null。此外,当我在该行中断并检查调试器中的附加内容时,bundle根本不包含任何附加内容。
public class GcmIntentService extends IntentService {
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
if (extras != null && !extras.isEmpty()) {
// This line returns null
String command = extras.getString("command");
服务器的相关代码:
...
jMessage message = new jMessage();
Map<String, String> responseData = new HashMap<String, String>();
//these are the values I want to read on device, but they are missing
responseData.put("command", "REGISTRATION_SUCCESSFUL");
responseData.put("userID", userID.toString());
message.setmTo(reg_id);
message.setmMessageId(nextMessageId());
message.setmData(responseData);
sendDownstreamMessage(message);
...
public void sendDownstreamMessage(jMessage m) throws NotConnectedException{
String json = createJsonMessageFromMessage(m);
Packet request = new GcmPacketExtension(json).toPacket();
connection.sendPacket(request);
}
public static String createJsonMessage(String to, String messageId,
Map<String, String> payload, String collapseKey, Long timeToLive,
Boolean delayWhileIdle) {
Map<String, Object> message = new HashMap<String, Object>();
message.put("to", to);
if (collapseKey != null) {
message.put("collapse_key", collapseKey);
}
if (timeToLive != null) {
message.put("time_to_live", timeToLive);
}
if (delayWhileIdle != null && delayWhileIdle) {
message.put("delay_while_idle", true);
}
message.put("message_id", messageId);
message.put("data", payload);
return JSONValue.toJSONString(message);
}
public static String createJsonMessageFromMessage(jMessage m){
return createJsonMessage(m.getmRecipients(), m.getmMessageId(), m.getmData(), m.getmCollapeKey(),
m.getmTimeToLive(), m.getmDelayWhileIdle());
}
我的服务器根据smack调试控制台发送的消息。请注意,数据仍然显示在那里:(我添加换行符)
<message id='Yi0UJ-4'>
<gcm xmlns="google:mobile:data">
{"data":
{"userID":"187188880",
"command":"REGISTRATION_SUCCESSFUL"},
"message_id":"m-b7b64e2f-ab89-4e1d-9764-b562f2133ee9",
"to":"(GCM reg id, edited out)"}
</gcm>
</message>
我的服务器在设备上的响应中可用的密钥。请注意,现在缺少userID和命令字段:
I/EXTRA KEY SET﹕ KEY: message_type
I/EXTRA KEY SET﹕ KEY: event
I/EXTRA KEY SET﹕ KEY: google.message_id
I/EXTRA KEY SET﹕ KEY: android.support.content.wakelockid
我开始怀疑服务器是否实际上是在响应中发送放大器代码,而不仅仅是显示错误。我没有从谷歌那里得到一个小窍门所以我认为它很好,但也许GCM服务器可以处理它,但谷歌Play库不能?如果是这种情况,为什么Smack库有时会添加放大器代码而不是其他代码。例如,在响应中,gcm标记中有引号。