1

I have been working on a project for quite a while and I have been using Firebase in it. I have implemented every Firebase stuff that is required by the app. But the only thing, I am stuck at is, FCM.

Basically, I have two apps linked to the Firebase project. One is a consumer app and other is an admin app. Now,I am trying to notify an admin about whenever a new order arrives or whenever a user wants to chat with them. I have successfully been able to notify a user by the consumer app using node js and Firebase functions, but I am not able to notify an admin. Both the apps have different package names. But whenever, a trigger, which is supposed to notify an admin, occurs, it sends the notification to the consumer app rather than the admin app.

I have been researching for quite sometime about this, and the only thing I can gather about it, is that I should use topics. But I wonder, if I use topics, will it still send the notification to the consumer app. Therefore, I need to ask whether I can send a FCM notification to a particular package name using node.js. and if this is not possible, how can I actually make it happen. Thanks a lot in advance

Edit 1: Here is my index.js code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite(event => {

  const user_id = event.params.user_id;
  const notification = event.params.notification_id;

  if(!event.data.val()){
  return console.log('A Notification has been deleted from Firebase');
}

console.log('The User ID is : ', user_id);

const fromUser = admin.database().ref(`/notifications/${user_id}/${notification}`).once('value');

return fromUser.then(fromUserResult => {

  const from_user_name = fromUserResult.val().from;
  const msg_type = fromUserResult.val().type;
  const message = fromUserResult.val().msg;
  console.log('Notification from: ', from_user_name);

  const deviceToken = admin.database().ref(`/Users/${user_id}`).once('value');
  console.log('Created deviceToken');
  return deviceToken.then (result => {

    const token_id = result.val().device_token;
    const order_id = result.val().order_id;
    console.log('Token ID: ', token_id);

    var payload;

    const token_id_aman = "eDYB-...ClU";

    if (msg_type == "text") {

      payload = {

        notification: {
        title : `New message from ${from_user_name}`,
        body: `${from_user_name}: ${message}`,
        icon: "default",
        tag: "text",
        sound: "default",
        click_action: "com.androidsword.techno.miniclip_TARGET_CHAT_NOTIFICATION"
        },
        data: {
          order_id: order_id,
          user_id: user_id,
          device_token: token_id
        }

        };

        if (from_user_name != "Admin") {

          payload = {

            notification: {
            title : `New message from ${from_user_name}`,
            body: `${from_user_name}: ${message}`,
            icon: "default",
            tag: "text",
            sound: "default",
            click_action: "com.androidsword.techno.adminminiclip_TARGET_NOTIFICATION"
            },
            data: {
              order_id: `${order_id}`,
              user_id:`${user_id}`,
              device_token:`${token_id}`
            }

            };

          return admin.messaging().sendToDevice(token_id_aman, payload).then(response => {
          console.log('This was a notification feature to Admin');
        });

      }

    }

    else if (msg_type == "status_changed") {

      payload = {

        notification: {
        title : `Order Status Changed`,
        body: `${message}`,
        icon: "default",
        tag: "status_changed",
        sound: "default",
        }
        };

    }

    else if (msg_type == "order") {

      payload = {

        notification: {
        title : `New Order`,
        body: `A new order has arrived`,
        icon: "default",
        tag: "order",
        sound: "default",
      }
        };

        return admin.messaging().sendToDevice(token_id_aman, payload).then(response => {
        console.log('This was a notification feature to Admin to inform about new order');
      });

    }


      return admin.messaging().sendToDevice(token_id, payload).then(response => {

      console.log('This was a notification feature');


    });

  });


});

});
KENdi
  • 7,205
  • 2
  • 14
  • 26
Aman
  • 13
  • 4
  • The other way- you can achieve same results. Firebase has things called `subscribeToTopics` and then send notifications to these `topics` , so for `com.example.a` you can subscriber users to topic "a" and for `com.example.b` , you can subscriber users to topic "b" .. then send notifications to any topic. You can read more about it on firebase docs. – BeingExpert Jan 05 '18 at 07:35
  • @BeingExpert, you are the best brother!!! You solved my issue!!! I am able to send notifications only to Admins now!!! I would have made your answer as Accepted, but I dont think StackOverFlow has the ability to make Comments as answer! But srsly, you made my day! – Aman Jan 05 '18 at 11:01
  • @Aman : Thank you. I would have been given answer and not comment. Anyways No issues. – BeingExpert Jan 05 '18 at 15:13

1 Answers1

0

You can't send to a certain package name. You can only send to topics or to specific device ids.

Florescu Cătălin
  • 4,320
  • 1
  • 20
  • 32
  • Hey, first of all thanks for your answer. I know about using topics, but do you think, it will work, when I am using two different packages and not one? I am just gonna give it a try though and will report back my results – Aman Jan 05 '18 at 09:35
  • Yes, it should if you have both apps registered on the same project. And the topics can be the package names of the apps or what you want. – Florescu Cătălin Jan 05 '18 at 12:00
  • Yes, it worked flawlessly. Thanks for your advice! – Aman Jan 05 '18 at 12:02