1

I'm developing a flutter communication app for the company I work in, but I came across two issues.

Here's what I need to do: 1) Send notifications to user groups or to a specific user, and save these notifications in a database or json file. This list will appear on the main screen of my app as "latest news."

The problem is, how do I capture the text of my notification when the app is in the background?

2) Create a kind of "talk to us" where employees can ask questions and get answers from the board.

In this case I believe the ideal would be to use the realtime database to store these messages, but how do I notify the user that he has a new response? I imagine I could use cloud functions for this but I do not know how to do it.

can anybody help me?

1 Answers1

3

Would your notifications be the answers to these user questions?

If so, I would replicate this scenario with Firebase Realtime Database (or Firestore). I would make everything work with the FB DB without the notifications and then add the notifications as needed.

Firebase Realtime Database allows you to perform offline access to this data and it synchronizes when there's internet again. You wouldn't be needing to save this data in an additional local database or json file.

As notifying groups or users, every device will have a fcmToken (firebase cloud messaging token), so you cloud store these tokens in your user profile and use them to direct your notifications.

However, in my experience, it will be A LOT easier to use topics both for groups and the individual user. Then your notifications would be directed to topics instead of specific tokens. For instance, a given user would subscribe to two topics, one named questions.group.finance and another just like user.id.131231. That way, you don't have to maintain a topics database and you can just infer them based on the answers details.

That also makes it easy to support multiple devices for a same user as well.

So, you could have a DB having a structure like that

 questions
    + 001
      - subject: What's ...? 
      - department: Finance
      - user: 131231
      + answers
          001
             - text: That's a ...
             - user: 432

And you could set up a Cloud Function to be triggered when a new answer is created.

export const answerCreate = functions.database.ref('/questions/{questionKey}/answers/{answerKey}')
  .onCreate(async (snapshot, context) => {
    // INCOMPLETE AND UNTESTED CODE

    const questionKey = context.params.questionKey
    const questionSnap = await fbadmin.database().ref(`/questions/${questionKey}`).once('value')
    const question = questionSnap.val()

    const answerKey = context.params.answerKey
    const answer = snapshot.val()

    const payload = {
      notification: {
        title: question.subject,
        body: `${answer.user.name} replied: ${answer.text}`,
        // icon: question.photoURL,
      }
    }

    const topic = `questions.group.${question.department}`
    return fbadmin.messaging().sendToTopic(topic, payload)
  })

If you REALLY want to CAPTURE the notification data in the background, I would send DATA notifications, as this table from firebase_messaging repo states,

On Android, DATA messages are received through onMessage while app stays in the background.

On iOS, DATA messages are stored by FCM and delivered to app via onMessage when the app is brought back to foreground

(reedited from that table)

However, if the app is terminated, not running AT ALL, these DATA messages will be lost, as the table also states.

That's why I suggest you to make your app work without the notifications and NOT use them to transfer the actual data, but just to notify the user that there's new data available. The notification could point the user to the right place in the app, but it wouldn't be necessary for the app's main usage.

Feu
  • 4,133
  • 1
  • 23
  • 48