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 explain to you in my answer how can be done using the new Cloud Firestore
. For that I recommend 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 following 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
Besides 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 collection to the user document named notifications
, in which you need to add the notification
you need to send and the sender
, every time 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 the tokenId
to send the notification to a specific user. The notification will be handled by the Android system and will be displayed to the user. Note, this will work only when the app is in background
. You can receive notifications also when the app is in the foreground
by implementing FirebaseMessagingService
.