简体   繁体   中英

chat notification when app is background using firebase

I am new to firebase. I am implementing one to one chat application using it, I am able to chat with user when he is online. Now I want to notify the user for receiving a message when his app is in background.

I am RND so many links, but I can't find any related article or answer.

Please help me.

when an IOS app is killed it's not present in the memory and you can't keep a service that listen to firebase running in the background you must have some backend code listen to changes in a database node and push a notification to phone when a new message is received.

luckily the new service of firebase "cloud functions" could do the same for you, here is an example of how to use it with android I think it's the same for IOS

update:

you may look at the server javascript code to get a hint about how it works, the cloud function will listen to node changes and notify user if needed so you don't need extra code in IOS, the flow will be something like this:

db/
  |-notifications
  |-messages

when you send message to someone you push the message to it's normal node and also to the notification node

db/
  |-notifications
  |\
  | |-[message id]
  |  \-from: [sender id]
  |  \-to: [receiver id]
  |  \-message: [message body]
  |
  |-messages
   \
    |-[sender id]
     \
      |-[receiver id]
       \
        |-[message id]
         \-from: [sender id]
         \-to: [receiver id]
         \-message: [message body]

and the cloud function listens to that notification node and perform it's action of sending the notification to phone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM