如何在Firebase实时数据库上安排通知? [英] How to schedule notifications on firebase realtime database?

查看:96
本文介绍了如何在Firebase实时数据库上安排通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为所工作的公司开发一款颤抖的通信应用程序,但遇到了两个问题。

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

这是我需要做的:
1)将通知发送到用户组或特定用户,并将这些通知保存在数据库或json文件中。此列表将在应用程序的主屏幕上显示为最新新闻。

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)创建一种与我们交谈,员工可以在其中提出问题并从董事会获得答案。

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.

有人可以帮助我吗?

推荐答案

您的通知将作为这些用户问题的答案吗?

Would your notifications be the answers to these user questions?

如果是,我将用Firebase复制此方案实时数据库(或Firestore)。我会在没有通知的情况下使所有功能都与FB DB一起使用,然后根据需要添加通知。

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允许您执行对此数据的脱机访问并进行同步当再次有互联网时。您不需要将这些数据保存在其他本地数据库或json文件中。

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.

作为通知组或用户,每台设备都会有一个 fcmToken (firebase云消息传递令牌),因此您将这些令牌存储在用户配置文件中,并使用它们来定向通知。

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.

但是,根据我的经验,对于组和单个用户而言,使用主题将变得更加容易。然后,您的通知将定向到主题,而不是特定的标记。例如,给定的用户将订阅两个主题,一个主题为 questions.group.finance ,另一个主题为 user.id.131231 。这样,您就不必维护主题数据库,您只需根据答案详细信息推断出它们即可。

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

您可以将Cloud Function设置为在创建新答案时触发。

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)
  })

如果您确实要在我会发送数据通知,因为此表来自 firebase_messaging回购状态

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,


在Android上,DATA消息通过 onMessage 接收,而应用程序
保留在后台。

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

在iOS上,DATA消息由FCM存储并在应用程序通过
onMessage 传递到应用程序时被带回到前台

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

(从该表中编辑)

但是,如果应用程序终止,而不是完全运行,则这些DATA消息也会丢失,如表中所示。

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.

这篇关于如何在Firebase实时数据库上安排通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆