Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push notifications on content change

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 !

like image 829
tR4x Avatar asked Oct 19 '25 21:10

tR4x


1 Answers

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.

  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
    

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: [email protected]
     |
     --- tokenId: "e_wLukMfq..." //very long token
     |
     --- //other details
  1. 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: [email protected]
      |
      --- tokenId: "e_wLukMfq..." //very long token
      |
      --- notifications (collection)
      |      |
      |      --- notificationId1
      |              |
      |              --- notificationMessage: "My Notification"
      |              |
      |              --- fromUser: "My Notification"
      |
      --- //other details
    
  2. 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.

like image 112
Alex Mamo Avatar answered Oct 22 '25 10:10

Alex Mamo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!