java中的XMPP服务器不接收来自GCM服务器的所有消息

时间:2017-04-15 19:03:52

标签: java android google-cloud-messaging xmpp smack

我使用Smack库实现了XMPP服务器,我的服务器从Google Cloud Messaging服务器(现在是Firebase)获取消息,但问题是当我从android发送一条消息到gcm服务器时,我的XMPP服务器收到只有第一条消息,第二条消息被截获,(我只能看到有消息的通知

<message id="gQaM0-6"><gcm xmlns="google:mobile:data">{"message_type":"ack","message_id":"0","to":"eVtypIWW7Q8:APA91bH5oU0AC3zyuCAWVYkMzoGQeIiGe71c2BL4lE5uFHRfB3iPXtD-qIJDmJZ3ySsPDi0VhkKl0Cz3XZG7rWa1Ca7pX9yQqzWSMXBiGK4SEO4Q-Owfr45E_VBJMrXqsSziuJhek"}</gcm></message>

但我没有这方面的数据  我在方法中得到的第一条消息void processPacket(Packet packet) 这是XMPP服务器的完整代码:

public class XMPPServer implements PacketListener {

    private static XMPPServer sInstance = null;
    private XMPPConnection connection;
    private ConnectionConfiguration config;
    private String mApiKey = null;
    private String mProjectId = null;
    private boolean mDebuggable = false;
    private String fcmServerUsername = null;

    public static XMPPServer getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException("You have to prepare the client first");
        }
        return sInstance;
    }

    public static XMPPServer prepareClient(String projectId, String apiKey, boolean debuggable) {
        synchronized (XMPPServer.class) {
            if (sInstance == null) {
                sInstance = new XMPPServer(projectId, apiKey, debuggable);
            }
        }
        return sInstance;
    }

    private XMPPServer(String projectId, String apiKey, boolean debuggable) {
        this();
        mApiKey = apiKey;
        mProjectId = projectId;
        mDebuggable = debuggable;
        fcmServerUsername = mProjectId + "@" + Util.FCM_SERVER_CONNECTION;
    }

    private XMPPServer() {
        // Add GcmPacketExtension
        ProviderManager.getInstance().addExtensionProvider(Util.FCM_ELEMENT_NAME, Util.FCM_NAMESPACE,
                new PacketExtensionProvider() {

                    @Override
                    public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
                        String json = parser.nextText();
                        GcmPacketExtension packet = new GcmPacketExtension(json);
                        return packet;
                    }
                });
    }

    /**
     * Connects to FCM Cloud Connection Server using the supplied credentials
     */
    public void connect() throws XMPPException {
        config = new ConnectionConfiguration(Util.FCM_SERVER, Util.FCM_PORT);
        config.setSecurityMode(SecurityMode.enabled);
        config.setReconnectionAllowed(true);
        config.setSocketFactory(SSLSocketFactory.getDefault());
        // Launch a window with info about packets sent and received
        config.setDebuggerEnabled(mDebuggable);

        connection = new XMPPConnection(config);
        connection.connect();

        connection.addConnectionListener(new ConnectionListener() {
                //a few overrided methods
        });
        // Handle incoming packets (the class implements the PacketListener)
        connection.addPacketListener(this, new PacketTypeFilter(Message.class));

        // Second message without data I get in this method (1)
        connection.addPacketWriterInterceptor(new PacketInterceptor() {
            @Override
            public void interceptPacket(Packet packet) {
                System.out.println("INTERCEPT PACKAGE: " + packet.toXML());
            }
        }, new PacketTypeFilter(Message.class));
        connection.login(fcmServerUsername, mApiKey);
    }
    /**
     * Normal message with my data I get in this method (2)
     */
    @SuppressWarnings("unchecked")
    @Override
    public void processPacket(Packet packet) {
        Message incomingMessage = (Message) packet;
        GcmPacketExtension gcmPacket = (GcmPacketExtension) incomingMessage.getExtension(Util.FCM_NAMESPACE);
        String json = gcmPacket.getJson();
        System.out.println("Message : " + json);
     }

几乎有整个代码,我用(1)和(2)标记的最重要的部分,(使用搜索快速查找) 为什么我只能收到包含数据的第一条消息? 为什么第二条消息转到PacketInterceptor(mark(1))?

1 个答案:

答案 0 :(得分:0)

如果您正在使用Firebase Cloud Messaging(FCM),请检查您的应用服务器是否已连接到以下端点:

// Production
fcm-xmpp.googleapis.com:5235

// Testing
fcm-xmpp.googleapis.com:5236

除此之外,您可能还需要检查Downstream messages,其中提到一旦建立XMPP连接,CCS和您的服务器使用普通的XMPP <message>节来发送JSON编码的消息来来回回。 <message>的正文必须是:

<gcm xmlns:google:mobile:data>
    JSON payload
</gcm>

另外,请注意常规FCM消息的JSON有效负载中的例外情况。访问给定的链接以获取更多信息。

这些相关的SO帖子也可能有所帮助: