4

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 ?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Loke WaiEu
  • 65
  • 1
  • 6
  • Possible duplicate of [Firebase Cloud Messaging - Send message to all users](https://stackoverflow.com/questions/39772167/firebase-cloud-messaging-send-message-to-all-users) – AL. Dec 05 '17 at 18:19

2 Answers2

3

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

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • It does sound very simple. Will try it 1st thing tomorrow, it's almost 3am here. Will keep you updated ! – Loke WaiEu Dec 05 '17 at 17:41
  • I have tried your suggestion, in the console it says that the notification is sent successfully but I am unable to receive the notification on my phone. I have updated my question with the changes, please have a look. – Loke WaiEu Dec 06 '17 at 08:09
  • if you are just sending from console.. you dont need cloud functions all you need is to register the user to the topic and wait one day so the topic appears in the console and send the message, no I dont think it will affect result, you can make a button and when they click it they recieve notification.. – Peter Haddad Dec 06 '17 at 08:10
  • I am not actually sending from the console, I am trying to create a cloud trigger which will automatically send notifications to all users each time someone uploads a file to the database. – Loke WaiEu Dec 06 '17 at 08:16
  • I found out that it's actually working without onMessageReceived, its just that the sender wouldn't be able to receive notification, but other users will. So yeah, silly me for not checking it thoroughly. Thanks a bunch ! – Loke WaiEu Dec 06 '17 at 08:32
0

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

Lucem
  • 2,912
  • 3
  • 20
  • 33