Last active
July 24, 2018 12:19
-
-
Save sohamdodia/33d47e1ad5538dbddc7e11ba121fbef3 to your computer and use it in GitHub Desktop.
Code snippet to send push notifications to android device using AWS push notification service.
This file contains hidden or 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 AWS = require ('aws-sdk'); | |
AWS.config.update({ | |
accessKeyId: 'ACCESS_KEY', | |
secretAccessKey: 'SECRET_KEY', | |
region: 'REGION' | |
}); | |
const sns = new AWS.SNS(); | |
sns.createPlatformEndpoint({ | |
PlatformApplicationArn: 'PLATFORM_APPLICATION_ARN', | |
Token: 'DEVICE_TOKEN' | |
}, (error, data) => { | |
if (error) { | |
console.log({ error }); | |
} else { | |
const endpointArn = data.EndpointArn; | |
//Payload for android | |
let payload = { | |
default: 'Test', | |
GCM: { | |
notification: { | |
body: 'Test body', | |
title: 'Test title', | |
sound: 'default' | |
}, | |
data: { | |
key: 'key value', | |
key2: 'key2 value' | |
} | |
} | |
} | |
payload.GCM = JSON.stringify(payload.GCM); | |
// then have to stringify the entire message payload | |
payload = JSON.stringify(payload); | |
//Payload for ios | |
// let payload = { | |
// default: 'Test', | |
// APNS: { | |
// aps: { | |
// alert: 'Test alert', | |
// sound: 'default', | |
// badge: 1 | |
// } | |
// } | |
// }; | |
// payload.APNS = JSON.stringify(payload.APNS); | |
// // then have to stringify the entire message payload | |
// payload = JSON.stringify(payload); | |
sns.publish({ | |
Message: payload, | |
MessageStructure: 'json', | |
TargetArn: endpointArn | |
}, (error, data) => { | |
if (error) { | |
console.log({ error }); | |
} | |
console.log({ data }); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment