3

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 !

tR4x
  • 65
  • 2
  • 8
  • if you are using .NET for API then you should use SignalR – Nirav Joshi Jan 17 '18 at 10:37
  • you do not need a database you can actually create an api in your web-api that gets triggered when something new is added and then using that api call firebase and send notifications very easily – FreakyAli Jan 17 '18 at 10:38
  • I don't have access to the API code. I'm using a 3rd party API (i.e. a popular weather REST API). So how can I do in that case ? Do I need to add some kind of gateway, such as a Node.js API that calls the weather API and then give the results back to my app ? (so that I can trigger notifications from the Node API) – tR4x Jan 17 '18 at 10:42
  • If you don't have access to the API, then _something's_ got to keep polling it to look for updates/changes, so it's either got to be your client/app or a server (e.g. in Node) that then notifies the clients/apps. You should probably also check the Ts&Cs of the 3rd-party API to make sure you're allowed to use it in this way. – TripeHound Jan 17 '18 at 11:09
  • You can use firebase remote config based on that show your notification – hemen Jan 17 '18 at 11:10

1 Answers1

10

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: john@email.com
     |
     --- 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: john@email.com
      |
      --- 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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193