I'm using Sharp.Xmpp to receive upstream messages from Android Device via FCM. It connects fine and 'at times' I can receive the messages sent from Android on my app server but the ratio is 50% whereas Android device gives a successful onMessageSent event.
The server side code is:
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); }
New Message Received event is
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();
}
and the SendPushNotification(String str) is Downstream messages with HTTP protocol. Android Device receives a push notification of 'connected' fine but the 'e.Message.Body' is received at times and not others. What should be added/removed?
Android Sends the messages as
FirebaseMessaging.getInstance().send(new RemoteMessage.Builder("[email protected]")
.setMessageId(Integer.toString(INT_VALUE)).addData("my_token", str).build());
As mentioned in the accepted answer to this question, XMPP must stay open at all times. Hence, switching for Web based application, that closes the connection after every few requests per second, to a Desktop application, hosted on a local server, solved the issue.
Also, it adding
setTtl(86400);for message sending ensuresonMessageSentcall.