Skip to content

Instantly share code, notes, and snippets.

@JoshHighland
Forked from tmarshall/aws-sns-example.js
Created April 17, 2016 06:39
Show Gist options
  • Select an option

  • Save JoshHighland/194fce30faccb14a2bc8a7b947c724ec to your computer and use it in GitHub Desktop.

Select an option

Save JoshHighland/194fce30faccb14a2bc8a7b947c724ec to your computer and use it in GitHub Desktop.

Revisions

  1. @tmarshall tmarshall renamed this gist Nov 7, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @tmarshall tmarshall created this gist Nov 7, 2014.
    52 changes: 52 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    var AWS = require('aws-sdk');

    AWS.config.update({
    accessKeyId: '{AWS_KEY}',
    secretAccessKey: '{AWS_SECRET}',
    region: '{SNS_REGION}'
    });

    var sns = new AWS.SNS();

    sns.createPlatformEndpoint({
    PlatformApplicationArn: '{APPLICATION_ARN}',
    Token: '{DEVICE_TOKEN}'
    }, function(err, data) {
    if (err) {
    console.log(err.stack);
    return;
    }

    var endpointArn = data.EndpointArn;

    var payload = {
    default: 'Hello World',
    APNS: {
    aps: {
    alert: 'Hello World',
    sound: 'default',
    badge: 1
    }
    }
    };

    // first have to stringify the inner APNS object...
    payload.APNS = JSON.stringify(payload.APNS);
    // then have to stringify the entire message payload
    payload = JSON.stringify(payload);

    console.log('sending push');
    sns.publish({
    Message: payload,
    MessageStructure: 'json',
    TargetArn: endpointArn
    }, function(err, data) {
    if (err) {
    console.log(err.stack);
    return;
    }

    console.log('push sent');
    console.log(data);
    });
    });