使用 Node JS 的 AWS SNS 推送通知

Joe*_*Joe 2 push-notification amazon-web-services apple-push-notifications node.js amazon-sns

我搜索了 AWS 文档并浪费了几个小时,但找不到使用 Node JS 发送推送通知的 API 和代码。有人可以帮助在 Android 和 iOS 设备上使用 Node JS 发送 AWS SNS 推送通知吗?

Joe*_*Joe 5

得到了答案。

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

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

var sns = new AWS.SNS();

  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,      // Required
    MessageStructure: 'json',
    TargetArn: {{TargetArn}} // Required
  }, function(err, data) {
    if (err) {
      console.log(err.stack);
      return;
    }

    console.log('push sent');
    console.log(data);
  });
});
Run Code Online (Sandbox Code Playgroud)