gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:55:34 24 4
gpt4 key购买 nike

我从事一个项目已经有一段时间了,并且一直在其中使用 Firebase。我已经实现了应用程序所需的所有 Firebase 内容。但我唯一坚持的是 FCM。

基本上,我有两个应用链接到 Firebase 项目。一个是消费者应用程序,另一个是管理应用程序。现在,我试图通知管理员每当有新订单到达或每当用户想要与他们聊天时。我已经成功地能够使用 Node 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');


});

});


});

});

最佳答案

您无法发送到特定的包名称。您只能发送到主题或特定设备 ID

关于android - 如何使用 Node.js FCM 将通知发送到特定包名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48107789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com