I need to subscribe to topic in login function, I get the device token and push it into array named tokens
And I use fcm function to subscribe, First : import fcm and intialize a new fcm with my server_key :
var FCM = require("fcm-node");
let fcm = new FCM(process.env.SERVER_KEY);
Then use this function to subscribe to topic
const tokens = [];
// deviceToken it contain my device Token
tokens.push(deviceToken);
fcm.subscribeToTopic({ deviceToken: tokens }, { topicName: "general" })
.then((resposne) => {console.log(resposne)})
.catch((error) => {console.log(error)})
It return in terminal:
CB(err, res);
^
TypeError: CB is not a function
and i try to change the function to :
fcm.subscribeToTopic(deviceToken, "cars", (err, response) => {
if (err) {
console.log("subscribeToTopicError:", err);
} else {
console.log("subscribeToTopicResponse:", response);
}
});
it return { statusCode: 400, message: 'Bad Request' }
How can i subscribe to topic with nodejs ?
If someone meet this problem in the future the problem because the "fcm-node" It has no support since about 2017!
You can use "firebase-admin" Instead of that, Then you will not meet such this problem.