How to schedule notifications on firebase realtime database?

前端 未结 1 620
情书的邮戳
情书的邮戳 2021-01-07 06:57

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

相关标签:
1条回答
  • 2021-01-07 07:43

    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.

    0 讨论(0)
提交回复
热议问题