Implement FCM notifications with node js server from backend using rest api - node.js

I have Android Client application which runs and E-comm app and my backend server handles the data with MongoDB now I want to use push Notifications on my android app using FCM maybe using REST API which will be triggered from backend and i am not able to do so .
Please guide on how to use FCM from backend to android application.
I tried using FCM documentation and the following code:
var request = require('request');
var options = { method': 'POST', 'url': 'https://fcm.googleapis.com/fcm/send', 'headers': { 'Content-Type': 'application/json' },
body: JSON.stringify({ "to": "YOUR_FCM_TOKEN_WILL_BE_HERE", "collapse_key": "type_a",
"data": { "body": "Sending Notification Body From Data",
"title": "Notification Title from Data" }})};
request(options, function (error, response) { if (error) throw new Error(error); console.log(response.body); });

Related

node.js how to send multipart form data in post request

I am attempting to get data in the form of an image sent from elsewhere using multipartform, however when trying to understand this via the great sanctuary(stack overflow) there are missing elements I don't quite understand.
const options = {
method: "POST",
url: "https://api.LINK.com/file",
port: 443,
headers: {
"Authorization": "Basic " + auth,
"Content-Type": "multipart/form-data"
},
formData : {
"image" : fs.createReadStream("./images/scr1.png")
}
};
request(options, function (err, res, body) {
if(err) console.log(err);
console.log(body);
});
2 questions:
what is the variable auth, what do I initialize it to/where/how do I declare it
what is the url "api.LINK.com", is this just the site url where this code is on
After your comments I think I may be doing this wrong. The goal is to send data(an image) from somewhere else(like another website) to this node app, then the nodeapp uses the image and sends something back.
So that I would be the one creating the API endpoint

Agora.io : Having issue with acquire POST call REST API in cloud recording

I am trying to set up cloud recording in Agora.io video call.According to agora docs first step is to call acquire API.
Initially I had issue with unescaped character is URL using axios NodeJS so I used encodeURI to bypass that error.My requests config is as follows
{
"url":"https://api.agora.io%E2%80%8B/v1%E2%80%8B/apps%E2%80%8B/xxxxxxx_APPID_xxxx%E2%80%8B/cloud_recording%E2%80%8B/acquire",
"method":"post",
"data":"{\"cname\":\"5f30xxxx-xx33-xxa9-adxx-xxxxxxxx\",\"uid\":\"75\",\"clientRequest\":{\"resourceExpiredHour\":24}}",
"headers":{
"Accept":"application/json, text/plain, */*",
"Content-Type":"application/json;charset=utf-8",
"Authorization":"Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"User-Agent":"axios/0.19.2",
"Content-Length":102
},
"transformRequest":[
null
],
"transformResponse":[
null
],
"timeout":0,
"xsrfCookieName":"XSRF-TOKEN",
"xsrfHeaderName":"X-XSRF-TOKEN",
"maxContentLength":-1
}
I get this response
Error: Request failed with status code 400
I have enabled cloud recording in agora console still the same error.
I would recommend taking a look at the Agora Postman Collection, which helps provide properly formatted requests.
In your request you are missing the
For example your request should look like this:
var axios = require('axios');
var data = JSON.stringify({"cname":"demo","uid":"527841","clientRequest":{ "resourceExpiredHour": 24}});
var config = {
method: 'post',
url: 'https://api.agora.io/v1/apps/<xxxx_APPID_xxxx>/cloud_recording/acquire',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
Here is a guide I've written for getting started with the Postman Collection for Agora's Cloud Recording.

Fetch function in React Native not working when sending SMS

Fetch function works in postman, but not in react-native, while trying to send an SMS with twilio API
I am trying to make an app that sends an sms when a button is pressed. In my onPress, i use this function.
SendMessage = () => {
try {
fetch("https://api.twilio.com/2010-04-01/Accounts/[AccountSID]/Messages.json", {
body: "To=[toPhoneNumber]&From=[fromPhoneNumber]&Body=[message]=",
headers: {
Authorization: "Basic [Auth]",
"Cache-Control": "no-cache",
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
}
catch (err) {
console.log("Error fetching data-----------", err);
}
}
When i press the button nothing happens. please bear over with me because i am new to react native.

FCM delivers duplicated copies of Push Notifications

I am using fcm node modules to send push notifications. Tried to send to a group(group with one device token). And all it delivers duplicated copies(4 copies everytime) of the same push for a single alert. It was supposed to be delivered only one. I have used fcm-node (version: 1.2.1), fcm-push (version: 1.1.3) etc.
The nodejs code is as shown below:
Alerts.pushTry = function (cb) {
var serverKey = "abcd"; //put your server key here
var deviceToken = 'group_key'; // required
var FCM = require('fcm-node');
var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
to: deviceToken,
notification: {
title: 'Group 555 test'+ ((new Date()).getTime()),
body: 'Body of your push notification'
},
data: {
my_key: 'my value',
my_another_key: 'my another value'
}
};
fcm.send(message, function(err, response) {
console.log("sent");
if (err) {
console.log(err);
console.log("Something has gone wrong!");
cb(err);
} else {
console.log("Successfully sent with response: ", response);
cb(null, response);
}
});
};
Can anyone help me?
There are some open issues exists for the above mentioned node modules.. I have used the request module and connected the fcm push notification url directly without depending on these types of npm modules with open issues.
var request = require('request');
request({
url: 'fcm.googleapis.com/fcm/send',
method: "POST",
headers: { "content-type" : "application/json",
"Authorization": key=${constants.FCM_SERVER_KEY},
"project_id": constants.FCM_SENDER_ID, },
json: message
}, function(error, response, body) {
//You code
});

Firebase Cloud Messaging Node.JS Notification Not receiving

I programmed a Node.JS Server with FCM and POST Protocol and my Client app is made with Swift code.
In Terminal, It prints success code like this
id=0:1476620858011270%00f7ba1cf9fd7ecd
But In my Client App, there is no notification. So I tried with the notification tab in my Firebase Console, and it worked very well.
This is my header in my server file
var headers = {
'Content-Type': 'application/json',
'Authorization': 'key=<Server Key>',
'project_id': '<Project ID>'
}
and This is my Request Code
request({
headers: headers,
url: "https://fcm.googleapis.com/fcm/send",
method: "POST",
form: {
"notification" :{
"title": "titie",
"body": "body"
},
"content-available": true,
"priority": "high",
"to": "<Firebase Token>"
}
}, function(error, response, body) {
console.log(body);
});

Resources