推送内容更改通知 [英] Push notifications on content change

查看:24
本文介绍了推送内容更改通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 Android 应用程序,它只是使用从随机 REST API 获取的内容构建一个 ListView.

Let's say I have an Android application that simply builds a ListView with content got from a random REST API.

想象一下,当 API 上有新内容可用时,我需要发送推送通知.最简单的方法是什么?

Imagine now I need to send push notifications when new content is available on the API. What's the simplest way to do this ?

我对 Android 上的推送通知过程有点困惑.我看了一下 Firebase,但我不知道我是否需要在 Firebase 上有一个数据库来存储结果,因为新内容可通过 API 获得,然后触发数据库更新通知,或者如果我不需要需要一个数据库等

I'm a bit confused with the process of push notifications on Android. I took a look at Firebase, but I don't know if I mandantory need a database on Firebase that stores the result as new content is available through the API, and then triggers a notification on the database update, or if I don't need a database, etc.

如您所见,我对此非常不清楚,因此非常感谢您的帮助.谢谢!

As you can see it's very unclear to me so any help is much appreciated. Thanks !

推荐答案

实现这一目标的最佳方法是使用 Firebase 云函数.这将帮助您在发生有趣的事情时通知用户,在您的情况下,当新内容可用时.您可以使用 Cloud FirestoreFirebase 实时数据库 来实现这一点.我将在我的回答中向您解释如何使用新的 Cloud Firestore.为此,我建议您还实施 Firebase 身份验证.这将有助于您在发生新事情时向特定用户或一组用户发送通知.

The best way for achieving this is to use Firebase Cloud Functions. This will help you notify users when something interesting happens, in your case, when new content is available. You can use either Cloud Firestore or Firebase Realtime Database to achieve this. I will exaplain you in my answer how can be done using the new Cloud Firestore. For that I recomand you implement also Firebase Authentication. This will help you send notifications to a particular user or to a group of users when something new happens.

为了实现这一点,请考虑按照以下步骤操作.

In order to achieve this, please consider follow the steps below.

  1. 实施 Firebase 身份验证.一旦实现,就创建一个用户集合,其中每个用户都将是用户集合中的一个文档.您的数据库结构应如下所示:

  1. Implement Firebase Authentication. As soon as it is implemented, create a collection of users in which each user will be a document within users collection. Your database structure should look like this:

Firebase-root
    |
    --- users (collection)
          |
          --- uid1 (document)
          |    |
          |    --- //user properties
          |
          --- uid2 (document)
               |
               --- //user properties

除了用户详细信息之外,您还需要为每个用户添加一个 tokenId.你可以很简单地使用以下代码行:

Beside user details, you need to add to each user a tokenId. You get can it very simply using the following line of code:

String tokenId = FirebaseInstanceId.getInstance().getToken();

用户文档应如下所示:

uid1
 |
 --- userName: "John"
 |
 --- userEmail: john@email.com
 |
 --- tokenId: "e_wLukMfq..." //very long token
 |
 --- //other details

  • 现在,在名为notifications的用户文档中添加一个新的coolection,在其中添加需要发送的notification发件人,每次有新的事情发生.它应该看起来像这样:

  • Now, add a new coolection to the user document named notifications, in which you need to add the notification you need to send and the sender, everytime something new happens. It should look something like this:

    uid1
     |
     --- userName: "John"
     |
     --- userEmail: john@email.com
     |
     --- tokenId: "e_wLukMfq..." //very long token
     |
     --- notifications (collection)
     |      |
     |      --- notificationId1
     |              |
     |              --- notificationMessage: "My Notification"
     |              |
     |              --- fromUser: "My Notification"
     |
     --- //other details
    

  • 现在您需要使用 Node.jsCloud Functions 中编写函数 将侦听此引用中出现的每个新通知:

  • Now you need to use Node.js to write a function in Cloud Functions that will listen for every new notification that appears within this reference:

    "users/{uid}/notifications/{notificationId}"
    

    一旦出现新通知,您可以使用 sendToDevice 函数和 tokenId 将通知发送给特定用户.通知将由 Android 系统处理并显示给用户.请注意,这仅在应用处于 backgroud 时才有效.通过实现 FirebaseMessagingService,您还可以在应用处于 前台 时收到通知.

    Once a new notification appears, you can use sendToDevice function and the tokenId to send the notification to a specific user. The notification will be handled by Android system and will be displayed to the user. Note, this will work only when the app is in backgroud. You can receive notifications also when the app is in foreground by implementing FirebaseMessagingService.

    这篇关于推送内容更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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