我正在使用 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协议(protocol)的下游消息。 Android 设备可以正常接收“已连接”推送通知,但有时会收到“e.Message.Body”,而其他情况则不会。应该添加/删除什么?
Android 将消息发送为
FirebaseMessaging.getInstance().send(new RemoteMessage.Builder("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83d0c6cdc7c6d1dccac7c3e4e0eeade4ecece4efe6e2f3eaf0ade0ecee" rel="noreferrer noopener nofollow">[email protected]</a>")
.setMessageId(Integer.toString(INT_VALUE)).addData("my_token", str).build());
最佳答案
正如已接受的答案 this 中所述问题,XMPP 必须始终保持打开状态。因此,切换到基于 Web 的应用程序(每秒每隔几个请求后就会关闭连接)到托管在本地服务器上的桌面应用程序,解决了这个问题。
此外,它添加 setTtl(86400);
用于消息发送,确保 onMessageSent
调用。
关于android - Sharp.Xmpp FCM 上游消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48843775/