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