Ahmad Abbas
Ahmad Abbas

Reputation: 21

Android Firebase Send Push Notification When New Data Added

This is the structure of my app's data in which user can log in and post the image with image's detail in the database. I want that if a user post image, every user can see the notification Seth Post Something I cannot find any link how to send a notification to all users when new data is added. Can anyone specify the roadmap, code snippet how to do this after configuration of FCM

enter image description here

Upvotes: 2

Views: 3268

Answers (2)

Virendra Varma
Virendra Varma

Reputation: 935

To achieve your requirement you can use cloud function to send messages to all devices and the steps to achieve this.

  1. take device access token for all user and save it in your database because without access token you can't send the message to devices never forget to update this toke whenever it changed.
  2. cloud message will trigger always whenever any event occurs in a targeted part of the database.
  3. For cloud message link of tutorials are here

Fcm Tutorial, cloud function Tutorial and this tutorial is very helpful for me.

Upvotes: 1

Android_K.Doe
Android_K.Doe

Reputation: 753

First thing to do is registering your users to one topic.

FirebaseMessaging.getInstance().subscribeToTopic("MySampleApp");

Then send a post request to fcm api https://fcm.googleapis.com/fcm/send

//HEADERS
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GB,./,Udno5aA

//BODY
{
  "to": "/topics/MySampleApp",
  "data": {
      "message": "Hello Everyone!",
  }
}

Then handle the notification in your FirebaseMessagingService. Hope this helps

Upvotes: 0

Related Questions