Invoke Service in Backround

I have created a chat application in Ionic3 using I am stuck in a situation where the user sends a message when no network is available. Like in whatsapp it should automatically sent in background when the network is available. Is there any way to do it in Ionic3?

I have installed the plugin ionic cordova plugin add cordova-plugin-background-mode . Could you please help me how to do it in Ionic3?

Thanks and Regards
Anand Raj

Any help please…!

Firestore does that automatically. Check other database services, see what makes sense for you.

I am using Please help me how to use that plugin also. I need to get a notification when a new chat arrives when the app runs in background

Maybe isnt the right tool. I think you need to do some research.

@AaronSterling Due to security reasons I cannot use firebase.

You have to cache your messages on your server. Then when you detect the users disconnects start saving any messages for them on the server. When they reconnect, send all their messages to them on connection.

Thanks for your reply. Is it possible to send push notification when new chat arrives and the app is in background?

Sure, but that gets right back to Aarons point of isn’t the right tool for that. I’d guess no matter what you want to cache messages on the server. Whether you also do push notifications is up to you.

Let me check and will update you. Can you help me to solve my next issue also?
send messages automatically when the network comes?

CAche in the localstorage and when u detect network, flush it

1 Like

@Tommertom is right.That’s exactly it. Just cache your messages using Storage, and on network reconnect (use the network native api), send all the cached messages the same way you send your other messages. The notification(s) can then be sent with whatever server you’re using.

@rlouie I dont get it, why did you say is maybe the wrong tool? Nothing wrong with doing the above right? Maybe I’m missing something… :slight_smile:

Thanks for your reply. I can store the messages in local storage but could you please explain how to send the messages as soon as the network is detected?

How r u sending them now?

send(msg) {
        if(msg != ''){
            this.socket.emit('message', msg);
        this.chat_input = '';

I meant how to automatically send messages in bulk on network detection ?

Detect network using the Network plugin. It has event handlers. Check the doc

Then bulk send.

Make sure you flag that you are bulk flushing as the flusher may be triggered multiple times (going from no network to 3g to wifi within seconds)

A bit more explanation on the second point will be appreciated. Dont know to perform bulk send. Can you explain with a code?

The example code for network detection is shown through the link

Sending bulk depends on how u plan to store the messages in the local store

What r your thoughts on that?

My coding time cost $$. Answering q not

I have stored all the messages sent in local storage with flag for sending with , without or network. I want to sent all the messages with flag no network in bulk when network is connected.

ok. that sounds like a plan. what is holding you back?

you have the code to detect network
you know how to store locally
you know how to send
just iterate through the messages and send!