admin管理员组文章数量:1332404
I am developing a Flutter mobile app. From my Nodejs backend, I am trying to send FCM Notifications to the app. I downloaded the private key file from firebase console's project settings. Below is my Nodejs code.
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');
const serviceAccount = require("service-account.json");
const fetch = require('node-fetch');
var {
google
} = require('googleapis');
var MESSAGING_SCOPE = '.messaging';
var SCOPES = [MESSAGING_SCOPE];
const prop = PropertiesReader('properties.properties');
exports.sendSingleNotification = async (event, context) => {
const params = event.queryStringParameters;
var PROJECT_ID = "xxxx-xxxx";
var HOST = "fcm.googleapis";
var PATH = "/v1/projects/" + PROJECT_ID + "/messages:send";
try {
let promise = new Promise(function(resolve, reject) {
var key = require("service-account.json");
var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
SCOPES,
null
);
jwtClient.authorize(function(err, tokens) {
if (err) {
reject(err);
return {
"error": "error 1"
};
}
resolve(tokens.access_token);
});
});
let accessToken = await promise;
// var options = {
// hostname: HOST,
// path: PATH,
// method: 'POST',
// headers: {
// 'Authorization': 'Bearer ' + accessToken
// }
// // … plus the body of your notification or data message
// };
var message = {
"message": {
"token": params.fcmtoken,
"notification": {
"title": "Portugal vs. Denmark",
"body": "great match!"
}
}
};
const response = await fetch('/' + PROJECT_ID + '/messages:send', {
method: 'post',
body: JSON.stringify(message),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
}
});
const data = await response.json();
// var request = https.request(options, function(resp) {
// resp.setEncoding("utf8");
// resp.on("data", function(data) {
// console.log("Message sent to Firebase for delivery, response:");
// console.log(data);
// });
// });
// request.on("error", function(err) {
// console.log("Unable to send message to Firebase");
// console.log(err);
// });
// request.write(JSON.stringify(message));
// request.end();
return {
"message": data
}
} catch (error) {
console.log(error);
return {
"error 2": error
}
}
};
Below is the content in my service-account.json
file
{
"type": "service_account",
"project_id": "xxx-xxx",
"private_key_id": "xxxxxxxxxxx",
"private_key": "-----BEGIN PRIVATE KEY-----\xxxxxxxxxxxxxxxxxxxxxx\xxxxxxxx+xxxxx/xxxxx/xxxx\xxxxx\xxxx+xxxx/xxx+xxxx\xxxx/lw/jiWVj6R6ZXYFtq0SloyRSQQufiPROEtQVZ/kYoxrQs5BZ\nt+9McW3Jtq5SMnYQIMUV1WdXOG+8a8ru2szOZmzLLaQr5RCGLUgdmGE2OP+04aSp\nEMEu00DThQXYOLxUs5N2WOGfJ6Cpz9tfZR5fdBHbI0yJ/QEsFXKM5+gxsV23aFhj\neAqWu4Gxr+lKV1h4ccpLk4O4teWcrcjLZS/h+xxxxx/AETt8mff\nqy4PDZPsdYObjpPWM42VaaFv4wD2rjQigYN5SdglVhOjkihLAQM2+hhfO/gLA+Ub\nsD15Sp+xxxxx\xxxxx/xxxxx+mpPDpISVtN\ngFEBqWYHhQYpQIpdAmQ7HhBLh5Gquf7w9OzdEZnfCeZGNzfeJ9NQJyefzqD9Ejsp\xxxxx+Og6vSGIrr9jUhkImxtayRIfZSAhufXP/gexq\xxxxx+WVbeWjP8xt2Iule0hM3Qbtkppw9D\nxqxh+xxxxx\n2PJyY43L+iv/fLG5unhOBT4oYL6pFEt/DzSAaI+xxxxx\xxxx+xxxx/xxxx/xxxx\xxxxx/UJDCGr\xxxxx+Z5AoGBAJcLKoE0EaUVPN1BD7jg\xxxxx+hnMGS0qWUw2AsYxeJXTLa7zWpkd9i8X5Evd5QlNm8BWaOj\xxxx+xxxx\xxxx/EJagS1\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "xxxxxxxxxxxxxxxxxx",
"auth_uri": ";,
"token_uri": ";,
"auth_provider_x509_cert_url": ";,
"client_x509_cert_url": ".iam.gserviceaccount"
}
In my Flutter app, below is how I get the FCM Token.
String? token = await FirebaseMessaging.instance.getToken();
When I run my Node.JS code, I always end up with the following error. I am passing the FCM Token generated by the above flutter code.
{
"message": {
"error": {
"code": 400,
"message": "The registration token is not a valid FCM registration token",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
}
]
}
}
}
If it helps, below is the accessToken
I get from the Node.js code. I am posting this because it looks weird to me anyways. The real code is converted into xxx
here but notice the lot of dots (.) available in the authentication token!
xxx.c.xxx-G0WwTjZbMZQ1Bh0xxxxxxxxxxx_o5wl4U1A7cIJN3t87k10DqyEo4Wu1CJu_vFrva0-S9nkwxlVmG3AAjxfYIyVFsszcbrcF-xxxxxx........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Why is this happening and How to fix this issue? FYI I tried the firebase admin-sdk
for Nodejs and the result is same as this.
I am developing a Flutter mobile app. From my Nodejs backend, I am trying to send FCM Notifications to the app. I downloaded the private key file from firebase console's project settings. Below is my Nodejs code.
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');
const serviceAccount = require("service-account.json");
const fetch = require('node-fetch');
var {
google
} = require('googleapis');
var MESSAGING_SCOPE = 'https://www.googleapis./auth/firebase.messaging';
var SCOPES = [MESSAGING_SCOPE];
const prop = PropertiesReader('properties.properties');
exports.sendSingleNotification = async (event, context) => {
const params = event.queryStringParameters;
var PROJECT_ID = "xxxx-xxxx";
var HOST = "fcm.googleapis.";
var PATH = "/v1/projects/" + PROJECT_ID + "/messages:send";
try {
let promise = new Promise(function(resolve, reject) {
var key = require("service-account.json");
var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
SCOPES,
null
);
jwtClient.authorize(function(err, tokens) {
if (err) {
reject(err);
return {
"error": "error 1"
};
}
resolve(tokens.access_token);
});
});
let accessToken = await promise;
// var options = {
// hostname: HOST,
// path: PATH,
// method: 'POST',
// headers: {
// 'Authorization': 'Bearer ' + accessToken
// }
// // … plus the body of your notification or data message
// };
var message = {
"message": {
"token": params.fcmtoken,
"notification": {
"title": "Portugal vs. Denmark",
"body": "great match!"
}
}
};
const response = await fetch('https://fcm.googleapis./v1/projects/' + PROJECT_ID + '/messages:send', {
method: 'post',
body: JSON.stringify(message),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
}
});
const data = await response.json();
// var request = https.request(options, function(resp) {
// resp.setEncoding("utf8");
// resp.on("data", function(data) {
// console.log("Message sent to Firebase for delivery, response:");
// console.log(data);
// });
// });
// request.on("error", function(err) {
// console.log("Unable to send message to Firebase");
// console.log(err);
// });
// request.write(JSON.stringify(message));
// request.end();
return {
"message": data
}
} catch (error) {
console.log(error);
return {
"error 2": error
}
}
};
Below is the content in my service-account.json
file
{
"type": "service_account",
"project_id": "xxx-xxx",
"private_key_id": "xxxxxxxxxxx",
"private_key": "-----BEGIN PRIVATE KEY-----\xxxxxxxxxxxxxxxxxxxxxx\xxxxxxxx+xxxxx/xxxxx/xxxx\xxxxx\xxxx+xxxx/xxx+xxxx\xxxx/lw/jiWVj6R6ZXYFtq0SloyRSQQufiPROEtQVZ/kYoxrQs5BZ\nt+9McW3Jtq5SMnYQIMUV1WdXOG+8a8ru2szOZmzLLaQr5RCGLUgdmGE2OP+04aSp\nEMEu00DThQXYOLxUs5N2WOGfJ6Cpz9tfZR5fdBHbI0yJ/QEsFXKM5+gxsV23aFhj\neAqWu4Gxr+lKV1h4ccpLk4O4teWcrcjLZS/h+xxxxx/AETt8mff\nqy4PDZPsdYObjpPWM42VaaFv4wD2rjQigYN5SdglVhOjkihLAQM2+hhfO/gLA+Ub\nsD15Sp+xxxxx\xxxxx/xxxxx+mpPDpISVtN\ngFEBqWYHhQYpQIpdAmQ7HhBLh5Gquf7w9OzdEZnfCeZGNzfeJ9NQJyefzqD9Ejsp\xxxxx+Og6vSGIrr9jUhkImxtayRIfZSAhufXP/gexq\xxxxx+WVbeWjP8xt2Iule0hM3Qbtkppw9D\nxqxh+xxxxx\n2PJyY43L+iv/fLG5unhOBT4oYL6pFEt/DzSAaI+xxxxx\xxxx+xxxx/xxxx/xxxx\xxxxx/UJDCGr\xxxxx+Z5AoGBAJcLKoE0EaUVPN1BD7jg\xxxxx+hnMGS0qWUw2AsYxeJXTLa7zWpkd9i8X5Evd5QlNm8BWaOj\xxxx+xxxx\xxxx/EJagS1\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "xxxxxxxxxxxxxxxxxx",
"auth_uri": "https://accounts.google./o/oauth2/auth",
"token_uri": "https://oauth2.googleapis./token",
"auth_provider_x509_cert_url": "https://www.googleapis./oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis./robot/v1/metadata/xxx/firebase-adminsdk-fguv2%40xxx-xxx.iam.gserviceaccount."
}
In my Flutter app, below is how I get the FCM Token.
String? token = await FirebaseMessaging.instance.getToken();
When I run my Node.JS code, I always end up with the following error. I am passing the FCM Token generated by the above flutter code.
{
"message": {
"error": {
"code": 400,
"message": "The registration token is not a valid FCM registration token",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis./google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
}
]
}
}
}
If it helps, below is the accessToken
I get from the Node.js code. I am posting this because it looks weird to me anyways. The real code is converted into xxx
here but notice the lot of dots (.) available in the authentication token!
xxx.c.xxx-G0WwTjZbMZQ1Bh0xxxxxxxxxxx_o5wl4U1A7cIJN3t87k10DqyEo4Wu1CJu_vFrva0-S9nkwxlVmG3AAjxfYIyVFsszcbrcF-xxxxxx........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Why is this happening and How to fix this issue? FYI I tried the firebase admin-sdk
for Nodejs and the result is same as this.
- Do you get the access_token successfully – nhCoder Commented Jun 8, 2022 at 19:01
- @nhCoder I have posted my access token in my question. – PeakGen Commented Jun 9, 2022 at 3:41
-
have you checked on this - firebase.google./docs/cloud-messaging/…? Maybe try without that
message
nested-object, hm? Also have you triedonTokenRefresh
- firebase.google./docs/cloud-messaging/flutter/…? – Stefan Commented Jun 9, 2022 at 4:15
1 Answer
Reset to default 4I found the issue myself and wanted to share it for the benefit of others.
The important thing is that my code is correct, nothing wrong with it. However I was trying to execute this as a get
function and I have passed the FCM Token as a parameter. This didnt work because the parameters contains a max length limit. As a result, my parameter got truncated when sending to the server.
All what I had to do is convert this call to a post
and send the FCM Token in the body
.
本文标签: javascriptNodejs The registration token is not a valid FCM registration tokenStack Overflow
版权声明:本文标题:javascript - Node.js: The registration token is not a valid FCM registration token - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742279054a2445752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论