Smack库的4.2版本似乎丢弃了FCM发送的消息体。 当我打开调试器时,我可以看到以下传入消息:
<message><data:gcm xmlns:data="google:mobile:data">{"message_type":"nack","from":"xxx","message_id":"aaaa1","error":"BAD_REGISTRATION","error_description":""}</data:gcm></message>
然而,当Smack 4.2解析此消息时,它会丢弃消息中的JSON正文并在我的数据包侦听器中提供以下内容:
<message><gcm xmlns="google:mobile:data"></gcm></message>
这是我的测试类:
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import javax.net.ssl.SSLSocketFactory;
public class CcsClient {
private static final String HOST = "fcm-xmpp.googleapis.com";
private static final int PORT = 5235;
private final XMPPConnection conn;
public CcsClient(String senderId, String serverKey) {
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
ConnectionConfiguration conf = new ConnectionConfiguration(HOST, PORT);
conf.setSASLAuthenticationEnabled(true);
conf.setSocketFactory(SSLSocketFactory.getDefault());
conf.setServiceName(HOST);
conf.setSendPresence(false);
conf.setCompressionEnabled(false);
conf.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
conf.setDebuggerEnabled(true);
this.conn = new XMPPConnection(conf);
conn.addConnectionListener(new AbstractConnectionListener() {
@Override
public void connectionClosed() {
super.connectionClosed();
}
@Override
public void connectionClosedOnError(Exception e) {
super.connectionClosedOnError(e);
}
@Override
public void reconnectingIn(int seconds) {
super.reconnectingIn(seconds);
}
@Override
public void reconnectionFailed(Exception e) {
super.reconnectionFailed(e);
}
@Override
public void reconnectionSuccessful() {
super.reconnectionSuccessful();
}
});
try {
conn.connect();
conn.login(senderId + "@gcm.googleapis.com", serverKey);
System.out.println("connected!");
} catch (XMPPException e) {
e.printStackTrace();
}
}
private static final class GcmPacketExtension extends DefaultPacketExtension {
private final String json;
public GcmPacketExtension(String json) {
super("gcm", "google:mobile:data");
this.json = json;
}
public String getJson() {
return json;
}
@Override
public String toXML() {
return "<gcm xmlns=\"google:mobile:data\">" + StringUtils.escapeForXML(json) + "</gcm>";
}
public Message toPacket() {
Message message = new Message();
message.addExtension(this);
return message;
}
}
public static void main(String[] args) throws InterruptedException {
final CcsClient c = new CcsClient("xxx", "xxx");
c.conn.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
System.out.println("incoming!" + packet.toString());
}
}, new PacketFilter() {
@Override
public boolean accept(Packet packet) {
return true;
}
});
for (int i = 0; i < 1; i++) {
c.conn.sendPacket(new GcmPacketExtension("{\"to\":\"xxx\", \"message_id\":\"aaaa"+i+"\", \"delivery_receipt_requested\":true}").toPacket());
}
Thread.sleep(1000000);
}
}
我做错了什么?
答案 0 :(得分:0)
所以我终于开始工作了。示例客户端位于:https://gist.github.com/judepereira/fd8dc0a5321179b699f5c5e54812770c