如何使用node.js将FCM通知发送到特定包名称

时间:2018-01-05 05:39:36

标签: android node.js firebase firebase-cloud-messaging

我已经在一个项目上工作了很长一段时间,我一直在使用Firebase。我已经实现了应用程序所需的每个Firebase内容。但唯一让我坚持的是,FCM。

基本上,我有两个与Firebase项目相关联的应用。一个是消费者应用程序,另一个是管理员应用程序。现在,我正在尝试通知管理员有关新订单何时到达或用户何时想要与他们聊天的通知。我已成功通过使用节点js和Firebase功能的消费者应用程序通知用户,但我无法通知管理员。这两个应用程序都有不同的包名称。但无论什么时候,都会发生一个应该通知管理员的触发器,它会将通知发送给消费者应用而不是管理员应用。

我一直在研究这个问题,我唯一可以收集到的,就是我应该使用主题。但我想知道,如果我使用主题,它仍然会将通知发送给消费者应用程序。因此,我需要询问是否可以使用node.js向特定包名发送FCM通知。如果这是不可能的,我怎么能真正实现它。非常感谢提前

编辑1: 这是我的index.js代码:

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');


    });

  });


});

});

1 个答案:

答案 0 :(得分:0)

您无法发送到某个包名称。您只能发送到d或特定topics