Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

database update notification in firebase for specific users

I am trying to create an android application and would like to use firebase database. As I read firebase allows instant messaging and also notify clients when database is updated. I need to create a database and when any database changes happen, related users needs to be notified only. Is that possible with firebase or should I use backend server for this purpose?

like image 755
user1474111 Avatar asked Nov 23 '17 20:11

user1474111


People also ask

How do you send notifications to specific users with FCM in flutter?

Using FCM Console To send a notification, go to Firebase Console → Cloud Messaging and click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token.


2 Answers

You can do it using the Firebase only. What you needs are

  1. Firebase Database (Realtime database/Firestore)
  2. Firebase Cloud Functions
  3. Firebase Cloud Messaging (FCM)

You should write two side codes:

  1. Client side (Android)
  2. Server side (Cloud Functions)

In Client side (Android)

  1. You should register token. (To send a notification using the FCM, Token is needed.) You can do this easily (https://firebase.google.com/docs/cloud-messaging/android/client)
  2. I recommend that you can store the token into the database/firestore too. for example:

    class User { long id; String email; String token; }

  3. You may write the code to modify the database/firestore.

  4. Implement receiving code (https://firebase.google.com/docs/cloud-messaging/android/receive)

In Server side (Cloud Functions)

  1. Add some trigger to the database/firestore.
  2. Get the token from the database/firestore
  3. just send FCM message (https://firebase.google.com/docs/cloud-messaging/admin/send-messages)

You can get the sample code for Server side (Cloud Functions) https://github.com/firebase/functions-samples/tree/master/fcm-notifications

like image 56
yoonhok Avatar answered Sep 30 '22 13:09

yoonhok


Yes you can achieve this using Firebase Cloud Functions which:

lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment.

And with Firebase Cloud Messaging which:

Is a cross-platform messaging solution that lets you reliably deliver messages at no cost. Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention.

So you can write a function in Firebase Cloud Functions which will trigger a notification every time something is changing in your database. Both documetations are very well explained.

like image 24
Alex Mamo Avatar answered Sep 30 '22 13:09

Alex Mamo