Getting duplicate message in Chat

Hi, Im working on Chat functionality in iOS. Chat support is available in Web, android and iOS.

Im experiencing strange issue. Sometimes when chat is send from iOS it gets duplicated. Duplicated messages can be seen in web as well as in android device when we use it.

In iOS we are fetching the messages and storing it locally and display it. When we try to send the message and if the sendbird connection is not set or ended, we try to establish the connection and after that we send all the messages which are not send to server.
When the app is left idle for sometime or in background for sometime and after that if we try to send a message or attachment, duplicate message issue comes. The same message is send multiple times. This issue is not coming all the time, it’s inconsistant. If we keep on sending the messages, it gets duplicated for sometime.

Can someone help me out here?