问题
Let\'s say I have an Android application that simply builds a ListView with content got from a random REST 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 ?
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 !
回答1:
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.
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
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();
A user document should look like this:
uid1 | --- userName: "John" | --- userEmail: john@email.com | --- tokenId: "e_wLukMfq..." //very long token | --- //other details
Now, add a new coolection to the user document named
notifications
, in which you need to add thenotification
you need to send and thesender
, 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
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}"
Once a new notification appears, you can use
sendToDevice
function and thetokenId
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 inbackgroud
. You can receive notifications also when the app is inforeground
by implementingFirebaseMessagingService
.
来源:https://stackoverflow.com/questions/48298993/push-notifications-on-content-change