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



我已经在一个项目上工作了一段时间,我一直在里面使用Firebase。我已经实现了应用程序所需的所有Firebase内容。但唯一被我困住的是,FCM。

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

我已经研究了很长一段时间,我唯一能收集到的关于它的事情是我应该使用主题。但我想知道,如果我使用主题,它是否仍会将通知发送到消费者应用程序。因此,我需要询问是否可以使用 node.js 向特定包名称发送 FCM 通知。如果这是不可能的,我怎么能真正实现它。提前非常感谢

编辑 1: 这是我的索引.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');

});
});

});
});

不能发送到某个包名称。您只能发送到topics或特定device ids

相关内容

  • 没有找到相关文章

最新更新