Created
August 16, 2018 13:37
-
-
Save albovieira/4816ef5e8c49cd5bf7ddd46106810ba8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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