Skip to content

Instantly share code, notes, and snippets.

@albovieira
Created August 16, 2018 13:37
Show Gist options
  • Save albovieira/4816ef5e8c49cd5bf7ddd46106810ba8 to your computer and use it in GitHub Desktop.
Save albovieira/4816ef5e8c49cd5bf7ddd46106810ba8 to your computer and use it in GitHub Desktop.
const CLOUD_MESSAGER = {
android: {
messager: 'GCM',
arn: process.env.AWS_SNS_ANDROID_ARN
},
ios: {
messager: 'APNS',
arn: process.env.AWS_SNS_IOS_ARN
}
};
async function sender(os, token, message){
const sns = new AWS.SNS();
const platform = await new Promise(resolve => {
sns.createPlatformEndpoint(
{
PlatformApplicationArn: process.env.AWS_SNS_ANDROID_ARN,
Token: token[os]
},
(err, data) => {
if (err) {
logger.error(err);
throw err;
}
logger.info(data);
resolve(data);
}
);
});
const endpointArn = platform.EndpointArn;
let payload = {
default: 'this is default',
[CLOUD_MESSAGER[os].messager]: message
};
// first have to stringify the inner APNS object...
payload.GCM = JSON.stringify(payload.GCM);
// then have to stringify the entire message payload
payload = JSON.stringify(payload);
return new Promise(resolve => {
sns.publish(
{
Message: payload,
MessageStructure: 'json',
TargetArn: endpointArn
},
(err, data) => {
if (err) {
throw err;
}
resolve(data);
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment