3
votes

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 ?

2

2 Answers

2
votes

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

0
votes

For people seeking a more direct answer

It is not possible to send to all users from firebase functions unless you are sending to targetted users. These would be users who have subscribed to a certain topic The proceeding function would be

sendToTopic(topic, payload); //for topic

Alternatively, You can use the console GUI that will send to every user even if one is not subscribed to a topic provided you don't specify a topic or send to device option

enter image description here