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');
});
});
});
});
subscribeToTopicsand then send notifications to thesetopics, so forcom.example.ayou can subscriber users to topic "a" and forcom.example.b, you can subscriber users to topic "b" .. then send notifications to any topic. You can read more about it on firebase docs.