Sharp.Xmpp FCM上游消息

时间:2018-02-17 17:06:46

标签: android firebase sharp.xmpp

我使用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());

1 个答案:

答案 0 :(得分:0)

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

此外,为邮件发送添加setTtl(86400);可确保onMessageSent来电。