How to structure a notification system for a chat app using Firebase Database and Firebase Notification

I'm developing a chat application using Firebase Database in Android.

I've already done the core (chat and user's list activities), but I have yet to do the notification system.

What I want is that when a user is added to a conversation (single or group) and another user writes a new message to the conversation, the first user has to receive a notification that if clicked opens the conversation activity.

My big dilemma is how to structure the service that runs in background to receive the push notification or to listen to the firebase database node I need to look at to know if there are any messages.

I figured out two different approaches:

Approach 1) Use the firebase notification

With this approach I simply send a data notification from the sender client to all the other clients in the conversation when the sender sends a message, and so the receiver will decide to show the notification (if the chat activity it's not opened) and handle the click action.

With this approach I think I will save CPU consumption and then battery.

What I don't understand is how to register a user to receive a group notification (topic notification) because as I understood, I have to subscribe that client to the topic, but if the application is in background or close, how does it knows that a new group, with its user inside, has been created and so it has to subscribe to the topic? For the two-users conversation scenario this is not a problem as I can send the notification directly to the destination user without needing him to be subscribed to any topic.

Approach 2) Listen to a firebase database data node with a background service

With this approach I just need to create a bootable service that listen to a specific node of the database (with ValueEventListener) and show a notification when data shows that a new message/conversation is coming. An example of the node to listen to, can be the data about the unseen messages as following:

conversation_user_unseen_messages
    $conversationId1
        $user1: 3
    $conversationId2
        $user2: 1

Then, if the data shows new messages/conversations the android app client will decide to show a system notification.

I think that with this approach there will be more energy consumption as it has to constantly check if there are any new message on the db.

Final consideration

I have found a very useful guide written by the mythical Frank van Puffelen,that explains how to set up the system I need, with using an additional server side component (node.js server). My last question is: do I need to set up a server? Is a better solution than handling everything by the clients (using for example http requests)?

What solution do you think is the best? Many thanks.

EDIT

I'm still figuring it out, but here it is some consideration.

I have to requesting and using a InstanceID

Instance ID provides a unique ID per instance of your apps.

So i have to request an InstanceID when user is connected and the InstanceId it is avalaible.

And then don't use topics.

Topic messages are optimized for throughput rather than latency. For fast, secure delivery to single devices or small groups of devices, target messages to tokens, not topics.

as said in the topic messagin guide that instead suggests to target message to tokens .

To do so I have to collect the user token in my user database reference:

users: {
    $userId1: {
        name: "John",
        email: "[email protected]",
        token: "Ax8HiP3Edf7....",
    }
}

and then when my app client send a new message it has to also has to send a notification for all users involved in the chat, thing that I already can do with my current db structure.

How do I handle and collect the requests? I implement a node.js server app that connect to Firebase Database and listens for the notification requests made by the app and then sends the notification request by http call to every destination app.

When do I have to register the user token? When a app client starts for the first time or when the InstanceID expire (onTokenRefresh).

Is this the right way to do it?

EDIT 2

I found a big hole in the FCM implementation. It seems that I can not handle at all notifications delivered to iOs apps that are not in foreground.

As found in the Data notification documentation

On iOS, FCM stores the message and delivers it only when the app is in the foreground and has established a FCM connection. On Android, a client app receives a data message in onMessageReceived() and can handle the key-value pairs accordingly.

And I need to catch the data notification even when the app is in background, I need that specifically because I want to update my badge counter on the app icon to let the user know how many unread messages he has.

I'm now tryng the OneSignal solution can receive notification even when in background, it's free and interfaces with GCM. I'm sad to not stay with Google but if I can't update the badge count using FCM I have to look to an other side.

Any consideration will be appreciated.

Upvotes: 4

Views: 5078

Answers (2)

Denis Hue
Denis Hue

Reputation: 1

Now you can simply achieve this using firebase functions ...

Here's mine

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendFollowerNotification = 
functions.database.ref('/whatever/{groupUID}/{userUID}/{todoUID}')
.onWrite(async (change, context) => {

  const useruuid = context.params.userUID;
  const thisdata = change.after.val();
  console.log('sendto:'+useruuid);

  var ref = admin.database().ref(`userdatas/${useruuid}/phonetkn`);

return ref.once("value", function(snapshot){
     const payload = {
          notification: {

            image: "default",
            sound:"default",
            vibrate:"true" ,

              title: 'New Mission !',
              body: thisdata.titre ,
              color: '#22c064',
              icon: "notification_icon"
          }
     };
     admin.messaging().sendToDevice(snapshot.val(), payload)

}, function (errorObject) {
    console.log("The read failed: " + errorObject.code);
});


});

//

Upvotes: 0

Wilik
Wilik

Reputation: 7720

Approach 1 is the one that you should use. Frank's guide is using the first approach, so you need to set up a server.

Is it a better solution than handling everything by the clients (using for example http requests)?

Yes. If you send the notification in the client (the app), your API Key will be exposed (via network sniffing or reverse engineering) and you definitely would want to avoid that.

how to subscribe a user to a new group topic if the app is closed or in the background?

Looks like you have to create relation mapping on the server, by calling https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME with Authorization: key=YOUR_API_KEY as the header, the full description is here. Follow this guide to get the Instance ID token.

I hope my answer answers your questions. Cheers :)

Upvotes: 2

Related Questions