2

我正在使用Sharp.Xmpp通过 FCM 从 Android 设备接收上游消息。它连接良好,并且“有时”我可以在我的应用服务器上接收从 Android 发送的消息,但比率为 50%,而 Android 设备提供了成功的onMessageSent事件。服务器端代码为:

try {
     using (var cl = new XmppClient("fcm-xmpp.googleapis.com", "Username", "Password", 5236, Sharp.Xmpp.Core.TLSMode.TLSSocket)) {

     // Setup any event handlers before connecting.
     cl.Message += OnNewMessage;

     // Connect and authenticate with the server.
     cl.Connect();

     SendPushNotification("Connected");

     cl.Close(); }
}
catch (InvalidOperationException ex) { SendPushNotification("Error Invalid: " + ex); }
catch (IOException ex) { SendPushNotification("Error IO: " + ex); }
catch (XmppException ex) { SendPushNotification("Error XMPP: " + ex); }
catch (NullReferenceException ex) { SendPushNotification("Error Null: " + ex); }

收到新消息事件是

private void OnNewMessage(object sender, Sharp.Xmpp.Im.MessageEventArgs e) {
    SendPushNotification("Message from " + e.Jid + " " + e.Message.Body);
    div_View_Message.InnerHtml = "Message xyz " + e.Message.Body.ToString();
    //throw new NotImplementedException();
}

并且SendPushNotification(String str)是带有 HTTP 协议的下游消息。Android 设备会收到“已连接”的推送通知,但有时会收到“e.Message.Body”而不是其他人。应该添加/删除什么?

Android 将消息发送为

FirebaseMessaging.getInstance().send(new RemoteMessage.Builder("SENDER_ID@gcm.googleapis.com")
            .setMessageId(Integer.toString(INT_VALUE)).addData("my_token", str).build());
4

1 回答 1

2

正如该问题的已接受答案中所述,XMPP 必须始终保持打开状态。因此,切换到基于 Web 的应用程序(每秒每隔几个请求后关闭连接)到托管在本地服务器上的桌面应用程序解决了这个问题。

此外,它添加setTtl(86400);用于消息发送确保onMessageSent呼叫。

于 2018-03-03T20:00:07.077 回答