繁体   English   中英

Java中的XMPP服务器不会从GCM服务器接收所有消息

[英]XMPP server in java does not receive all messages from GCM server

我已经使用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(标记(1))?

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

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

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

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

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

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

这些相关的SO帖子也可能会帮助您:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM