Let me check and will update you
That was in reference to sending push notifications, in which case it isnāt. My answer to the original problem was to simply cache the messages on the server.
Although I might have misread the question, I was thinking it was about receiving messages after disconnect rather than sending. In either case you want to cache messages, on the client if thereās no connection on send, on the server if thereās no connection on receive.
However, depending on how you interpret āautomatically sent in backgroundā, the answer of using local storage and re-sending on itās own may not work. If by āin the backgroundā the question means even if the app has been switched away from, that wonāt work. If they mean that the app is left open, then thatās fine.
What I meant by background is " I should receive the messages or push notification when a new messages arrives when the app is gone background "
Right so this is the confusion. The original question is about sending the message right? Not receiving. While sending, if thereās a failure it makes sense to simply tell the user the message couldnāt be sent and that they should try again.
From there the message goes to your server. At that point, instead of simply sending it on to the user, you probably want to store it. Then send it out.
When a user opens your app, you could then fetch the last 20 or so messages and display them. Then any messages sent while the app is open would go through sockets. Regardless of network connection, if you donāt do this all your conversations will be lost every time someone closed the app.
Push notifications are separate. You can definitely send them to notify people that a message was sent, but that alone isnāt going to fully solve the message caching
So, I am inserting all the messages sent in db with a flag indicating sent, not send. So when network is detected I have to fetch all the messages from db with flag not network, right? and Iterate over and send to server?
Iām not proposing that, Iām suggesting you simply store the messages, most likely with a timestamp of when they were sent.
When the message is received on your server:
- Store it
- Send it along with sockets
Then, have a new endpoint on your server that retrieves the last ~20 messages (or however many you want).
When the app is opened, hit that endpoint and show the last ~20 messages or so, in order of timestamp. You will continue to receive new messages at this point via sockets since the app will be open. At some point you could add pagination to this endpoint so that if the user scrolls up you could retrieve the next 20 messages, then the next 20 and so on.
This should get you consistent working messaging that will work across multiple devices and will show the users any messages received while the app was closed. Once you have that, then you can create any additional features you might want, like push notifications or caching some messages locally or auto re-sending messages when a network connection is detected.
I donāt know exactly what you are building or your exact requirements, so I cantā tell you for sure, Iām simply suggesting something like that for a general chat app.