使用Lambda和AWS SNS发送推送通知单个设备

时间:2018-10-26 07:03:01

标签: amazon-sns

我使用aws SNS为发送推送(ios)做了很多工作。我手动创建一个平台应用程序,然后添加deviceToken然后进行推送。它运作良好。 但是我想在lambda运行时中将该设备添加到应用程序平台中,并将推送发送到该设备令牌。有什么可以帮到我吗?预先感谢。

1 个答案:

答案 0 :(得分:2)

function sendPushMessage(deviecToken, pushMessage)
{
  var SNS = require('sns-mobile'),EVENTS = SNS.EVENTS;
  var SNS_KEY_ID = 'AWS_USER_AWSAccessKeyId',
    SNS_ACCESS_KEY = 'AWS_USER_AWSSecretKey',
    IOS_ARN = "SNS_APPLICATION_ARN";
  var iosApp = new SNS({
    platform: SNS.SUPPORTED_PLATFORMS.IOS,
    region: 'us-west-1',
    apiVersion: '2010-03-31',
    accessKeyId: SNS_ACCESS_KEY,
    secretAccessKey: SNS_KEY_ID,
    platformApplicationArn: IOS_ARN,
    sandbox: true 
  });
  // Add a user, the endpointArn is their unique id
  // endpointArn is required to send messages to the device
  iosApp.addUser(deviecToken, JSON.stringify({
    some: 'extra data'
  }), function(err, endpointArn) {
    if (err) {
      // callback(null, err);
    }
    else{
      let endpp = endpointArn;
      // Send a simple String or data to the client
      iosApp.sendMessage(endpp, pushMessage, function(err, messageId) {
          if (err) {
            // callback(null, err);
          }
          else{
            // callback(null, messageId);
          }
      });
    }
  });
}
let response;
let test = (event, context, callback) => {

  let deviceToken  = "YOUR DEVICE TOKEN";

                let apnPayload = { aps: { alert: { title: "Hello", body: "This is the content of our push notification." }, badge: 6 } };
                let msg = {
                       "APNS_SANDBOX": JSON.stringify(apnPayload)
                };
                sendPushMessage(deviceToken, msg);


};
module.exports = test;

注意:首先,您必须“ npm install sns-mobile”此软件包。然后创建测试lambda并复制粘贴此代码。我正在使用此代码并且运行良好。