Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Trigger Function - How can I send notifications to ALL users?

I am totally new to Firebase Cloud Functions (2 days exposure). I am trying to send notifications to ALL users of my app when Firebase Database detects that new data has been added. Here is what I have so far:

exports.sendNotification = functions.database.ref("/uploads/{pushId}").onCreate(event => {

  const snapshot = event.data;
  var str = snapshot.child("name").val();
  console.log(str);

  if (snapshot.previous.val()) {
    return 0;
  }

  if (snapshot.val().name != "ADMIN") {
    return 0;
  }

  const text = snapshot.val().text;
  const payload = {
    notification: {
      title: snapshot.name,
      body: ""
    }
  }
  //return admin.messaging().sendToDevice(tokens, payload);
});

I know the code is in a state of mess right now, its due to a couple of copy and testing from various tutorial sites. I can succesfully get the data's name from console.log but am unable to send notification to ALL users.

I am aware that most use tokens and device IDs. But is there any easier way to send to each and every one of my users ? And do I need to add any java codes for my app for this notification to work ?

EDIT 1: Following Peter's suggestions, I have updated my functions:

exports.sendNotification = functions.database.ref("/uploads/{pushId}").onCreate(event => {

  const snapshot = event.data;
  var str = snapshot.child("name").val();
  console.log(str);

  if (snapshot.previous.val()) {
    console.log("RETURN 1");
    return 0;
  }

  const payload = {
    notification: {
      title: str,
      body: ""
    }
  }

  return admin.messaging().sendToTopic("Users", payload)
  .then(function(response){
        console.log("Notification sent ", response);
  })
  .catch(function(error){
        console.log("Error sending notification: ", error);
  });
});

I have also added the following java to my code:

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

Problem I am having now is that on the Firebase console it says that the notification is being sent successfully, but on my phone I am not receiving anything. Is it a must to use the onMessageReceived method in my case ?

One thing I noticed is that the above statement is being called each time the app launches. Will this effect the result in any way ?

like image 354
Loke WaiEu Avatar asked Oct 20 '25 11:10

Loke WaiEu


1 Answers

I think the easiest one is topics, you can subscribe all the users to a single topic and then send a notification to that topic. You have to change your return statement to this:

return admin.messaging().sendToTopic("Cat", payload);

So now all the users subscribed to the topic "Cat" will receive the notification. Of course you can change the topic also to anything you want..

To subscribe users to a topic, all you need to do is write this:

FirebaseMessaging.getInstance().subscribeToTopic("Cat"); //in java code

check this for more info topic messaging

like image 62
Peter Haddad Avatar answered Oct 22 '25 23:10

Peter Haddad