format request to send dynamic link push notification nodejs - node.js

I am attempting to add dynamic links to push notifications so users tap them and open to a specific app page. I have been following the documentation and the links do not open to the page they should when I tap the push notifications but when tested in the browser the links do open to the correct page. From what I have read, there are 2 possible solutions depending on the version of firebase the project is using. The older "way" is to add a click_action property to the apns payload object, the newer way is to create a webpush object with the fcm_options property set to the value of the link. Neither of these options seems to work regardless of where the objects are placed in the request. Is my request formatted incorrectly or am I possibly missing something? Below is the current request:
const [title, body, deepLink] = titleAndBody;
let accessToken = await getAccessToken();
accessToken = "Bearer " + accessToken;
const options = {
method: "POST",
url: URL,
headers: {
Authorization: accessToken,
"Content-Type": "application/json",
},
body: JSON.stringify({
message: {
token: fcmToken,
notification: {
body: body,
title: title,
},
webpush: {
fcm_options: {
link: deepLink,
}
},
apns: {
headers: {
priority: "10",
},
payload: {
//click_action: deepLink,
aps: {
badge: 0,
mutable_content: 1,
},
},
}
},
}),
};```

Related

How to add a contact to ActiveCampaign using API v1 contact_sync/contact_add

I am having trouble adding a contact to ActiveCampaign. I read a post in here: How to add a contact to a list in ActiveCampaign API v3 and am using v1 of the API. I used their contact_sync documentation to the best of my availability.
I'm developing using Gatsby/React --> GitHub --> Netlify, using a lamda function for the POST request.
Here is my axios POST:
{
method: 'post',
url: 'https://ACCOUNT.api-us1.com/admin/api.php?api_key=xxxxxxxxxxxx&api_action=contact_sync&api_output=json',
headers: { 'Content-Type': 'Content-Type: application/x-www-form-urlencoded' },
body: {
email: 'email#email.com',
first_name: 'John'
}
}
And received the following response:
{
result_code: 0,
result_message: 'Could not add contact; missing email address',
result_output: 'json'
}
I'm talking to their endpoint. I just can't figure out how to feed the endpoint the email address?
Does anyone have a working example they would be kind enough to share? Guidance of any kind would be greatly appreciated!
I wanted to make sure to close this and share my answer.
Thanks so much to #reza jafari for his comment in this post where he brought to my attention the code window on the right margin of Postman where you can choose the language/server from a dropdown and it provides the correctly formatted response.
(I don't have enough reputation to upvote #reza's response so wanted to acknowledge it here.)
I was able to get my post working in Postman, and this little trick squared me away. I'll go ahead and post my solution to close this post.
const axios = require("axios")
const qs = require("qs")
exports.handler = async function (event) {
const { email, first_name } = JSON.parse(event.body)
const data = qs.stringify({
email: email,
first_name: first_name,
tags: '"api"',
"p[1]": "1",
})
const config = {
method: "post",
url: "https://ACCOUNT.api-us1.com/admin/api.php?api_key=xxxxxxxxx&api_action=contact_sync&api_output=json",
headers: {
"Api-Token":
"xxxxxxxxx",
"Content-Type": "application/x-www-form-urlencoded",
},
data: data,
}
try {
const response = await axios(config)
return {
statusCode: 200,
body: JSON.stringify(response.data),
}
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify(err),
}
}
}

How to send FCM push notification to multiple devices using google apis?

I want to send push notification to multiple devices at once. Can i pass array of tokens?
Is it possible to do using google api? Please help me with this.
request(
{
method: 'POST',
uri:
'https://fcm.googleapis.com/v1/projects/projectId/messages:send',
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${token}`,
'apns-priority': 'high',
content_available: true
},
body: {
message: {
token: token,
notification: {
body: 'This is an FCM notification message!',
title: 'FCM Title'
},
android: {
notification: {
sound: 'default'
}
}
}
},
json: true
},
async function(error, response, body) {
if (error) {
console.log(error);
}
console.log(body);
}
);
If you want to make the POST requests by yourself, you can prefer to:
1) Use the legacy HTTP server protocol. With that you will be able to use the "registration_ids" field which expects an array of token strings in POST body. Docs here.
2) Stick to HTTP v1 API that you're using right now, but since "token" field is expecting only string, you can first subscribe your users to a specific topic and use that topic in "topic" field in POST body.
Additionally, you may prefer to use Admin SDK to send batch notifications. You can check for further here.

Discord Profil Picture Update from ElectronJS using request PATCH

I'm trying to code an application into Electron JS to allow the person to change their profile picture at the same time on several applications.
For this I use the APIs of each platform.
For Twitter it works correctly, but I block at the level of Discord.
I can make a GET request on the profile, but I can't do a : PATCH/users/#me
https://discordapp.com/developers/docs/resources/user#modify-current-user
I do not know if it's the token that does not offer enough power, because I only asked for Identity as permission on my application.
I tried to pass JSON between true and false,
to add a content type, but I still have the same answer: {code: 0, message: '401: Unauthorized'}
function postDiscord(image) {
const imageDataURI = require('image-data-uri')
let {token} = store.get('discordToken') //get stored token
imageDataURI.encodeFromFile(image)
.then(res => {
request({
method: 'PATCH',
url: 'https://discordapp.com/api/v6/users/#me',
headers: {
'Authorization': 'Bearer '+token,
'User-Agent': 'someBot (site, v0.1)'
},
body: {
'avatar': res
},
json: true
}, function(err, res) {
if(err) {
console.error(err);
} else {
console.log(res.body)
}
}
);
})
}
{code: 0, message: '401: Unauthorized'}
Refering to Discord :https://github.com/discordapp/discord-api-docs/issues/1057
Cannot upload new pics with Oauth :/

Request must have at least one newMediaItem - Google Photos API

I'm trying to create media item using Google Photos API. Endpoint is documented here. If I copy and paste my payload to an API explorer which can be found in documentation, I get success:
Request inputed to an API Explorer and Successful response.
If I do the same in my node js program, I'm getting an error.
This is what I do:
const accessToken = "MyAccessToken";
payload = {
"newMediaItems": [
{
"description": "picture",
"simpleMediaItem": {
"uploadToken": "CAIS+QIASsyg4OQLX2Ao5hy6I734/b01mjk3Mqpom6DQ24iv7ZfAYLiXAy0WpOXCWJBNHrmBs6FE+a9Axu5CML+Ryu4VGawyf4skxM763mzC5GcjMY4rS/r6IwOekBIoE/aMJLJpRr1gW8jdhVJM89+kioTx9d+shyYeQDbVI8ezb1lXGp6irc9hZl7QA6xd+msXzbLD5nb+wc5CA6du95tP3buh5R5N/Knn+NwByebdEPCusl+X3p7DZ6ha72kLthUqdvwFsp8dpnGbNQBq8AFPVNHXB4C543iq+dYiRFYtICCxO8xi2cpONVT54Jl6l9rGh3Vnidwj5IwkbsXkyiN96HfRb9XLh0rCBw4ydV6Y9+C+OmTAqlQwIKy50I/ykHyzggroeJSbgphiQwFR2EbHwAeSKdsdIB03ItnunHtf3F2LRIitDRGI1n4VUEYE1dYjrrjR791ao24Dp8J3Hg8IRb8E3vFTeYMWyOk4mh/zQGInfNBnRY2ruHH0JA"
}
}
]
};
const response = await fetch('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${accessToken}`
},
payload: JSON.stringify(payload)
});
const json = await response.json();
console.log(json);
And getting this response:
{ error:
{ code: 400,
message: 'Request must have at least one newMediaItem.',
status: 'INVALID_ARGUMENT' } }
What could go wrong here?
Any help is appreciated, thank you.
It looks like you are setting the JSON payload in the wrong parameter in your call to fetch. It should be set in the parameter body (and not in payload). The JSON itself looks okay.
This snippet should work:
const response = await fetch('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${accessToken}`
},
body: JSON.stringify(payload)
});
Source: node-fetch 'Post with JSON' example

Mailchimp: The API key provided is linked to a different datacenter

I am trying to update a Mailchimp list but receive the following error:
{
"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title":"Wrong Datacenter",
"status":403,
"detail":"The API key provided is linked to a different datacenter",
"instance":""
}
However, the data-center referenced in my request URL is the same (us14) as the one suffixing my API key.
request.put({
url: 'https://us14.api.mailchimp.com/3.0/lists/xxxxxxxxx/members/',
auth: {
user: 'apikey:xxxxxxxxxxxxxxxxxxxxx-us14'
},
data: {
email_address: email,
status_if_new: 'subscribed',
email_type: 'html'
}
}
I have tried generating new API keys to no avail (they're all in us14).
Ok I was able to get this to work by first passing your API Key via the headers object. Second, I wrapped my data in JSON.stringify to ensure MailChimp was receiving a proper JSON Object on post. See below for sample code, hope this helps:
request.post({
url: 'https://usXX.api.mailchimp.com/3.0/lists/xxxxxxx/members',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxx-usXX'
},
form: JSON.stringify({
email_address: req.body.email,
status: 'subscribed',
interests: { 'xxxxxxx': true } // Interest Group
})
}, function(err, httpResponse, body) {
res.send(body);
});
const options = {
method: "POST",
auth: "uname:apikey656a******d2dfdb37c071a7cc-us19" //Should not give a space after a colon after uname
}
I had given a Space after the colon of uname. Now the API is working fine

Resources