0

I wanted to ask how can i send AWS SNS push notification end to end means i don't want to use the console for creating endpoints and arn I want to send the notification using nodejs. I am able to send the notification for one device using console and taking that endpoint to publish the notification. I wanted to ask how can i implement fully using nodejs. here is my tried solution

var AWS = require('aws-sdk');

AWS.config.update({
    accessKeyId: "",
  secretAccessKey:"",
  region: "us-east-1"
});
var sns = new AWS.SNS();

let payload2 = JSON.stringify({
    default: 'Practice',
    GCM:  JSON.stringify({
      notification : {
        body : 'great match!',
        title : 'Portugal vs. Denmark'       
      },
      data:{
        testdata: 'Check out these awesome deals!',
        url: 'www.amazon.com'
      }
    })
  });
  console.log(payload2)


  console.log('sending push');

  sns.publish({
    Message: payload2,      // Required
     MessageStructure: 'json',
    TargetArn: 'Arn from console' // Required
  }, function(err, data) {
    if (err) {
      console.log(err.stack);
      return;
    }

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

I also wants to know how can i send the batch push notification to the multiple devices?

1 Answers1

1

How can I implement fully using NodeJS

// Use AWS-SDK
import * as AWS from 'aws-sdk';

// Known constants
const fcmToken = 'SAMPLE_FCM_TOKEN';
const applicationArn = 'FCM_APPLICATION_ARN';
const topicArn = 'SNS_TOPIC_ARN';


// Initialize SNS
const sns = new AWS.SNS({
    apiVersion: '2010-03-31',
    accessKeyId: 'YOUR_ACCESS_KEY',
    secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
    region: 'YOUR_REGION'
});

// Function to register an application-endpoint using FCM token
async function registerDevice(deviceFcmToken: string): Promise<string> {
    console.log('Registering device endpoint');
    const endpointArn = await sns.createPlatformEndpoint({
        PlatformApplicationArn: applicationArn,
        Token: deviceFcmToken
    })
    .promise()
    .then((data) => {
        return data.EndpointArn;
    })
    .catch((error) => {
        return null;
    });
    return endpointArn;
}

// Function to subscribe to an SNS topic using an endpoint
async function subscribeToSnsTopic(endpointArn: string): Promise<string> {
    console.log('Subscribing device endpoint to topic');
    const subscriptionArn = await sns.subscribe({
        TopicArn: topicArn,
        Endpoint: endpointArn,
        Protocol: 'application'
    })
    .promise()
    .then((data) => {
        return data.SubscriptionArn;
    })
    .catch((error) => {
        return null;
    });
    return subscriptionArn;
}

// Send SNS message to a topic
var params = {
    Message: 'Hello World',
    TopicArn: topicArn
};
sns.publish(params)
    .promise()
    .then((data) => {
        console.log(`Message ${params.Message} send sent to the topic ${params.TopicArn}`);
        console.log("MessageID is " + data.MessageId);
    })
    .catch((err) => {
        console.error(err, err.stack);
    });


How can I send the batch push notification to the multiple devices?

Assuming you want to do this by using SNS itself. You can add/register application-endpoints for all devices under the platform-application. Then from every endpoint subscribe to an SNS topic, so whenever you send a message to an SNS topic it will automatically be sent to all subscribers.

PS: Not sure how well this approach would scale.

adisakshya
  • 11
  • 1
  • 3