gpt4 book ai didi

javascript - 使用 Amazon SNS 推送通知( Node )

转载 作者:行者123 更新时间:2023-11-29 22:50:25 25 4
gpt4 key购买 nike

我正在尝试在 Node.js 中使用 Amazon SNS 实现推送通知。我创建了一个主题并使用以下代码发布了一条消息

创建主题

var createTopicPromise = new AWS.SNS({apiVersion: '2010-03-31'}).createTopic({Name: "TOPIC_NAME"}).promise();

// Handle promise's fulfilled/rejected states
createTopicPromise.then(
function(data) {
console.log("Topic ARN is " + data.TopicArn);
}).catch(
function(err) {
console.error(err, err.stack);
});

我有一个 TopicArn 像这样 arn:aws:sns:us-east-1:xxxxxxxxxx:TOPIC_NAME

发布

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'REGION'});

// Create publish parameters
var params = {
Message: 'You Got Message!! Hello....', /* required */
TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxx:TOPIC_NAME'
};

// Create promise and SNS service object
var publishTextPromise = new AWS.SNS({apiVersion: '2010-03-31'}).publish(params).promise();

// Handle promise's fulfilled/rejected states
publishTextPromise.then(
function(data) {
console.log("Message ${params.Message} send sent to the topic ${params.TopicArn}");
console.log("MessageID is " + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});

现在消息已经发布,我需要在我的手机上看到它。所以我使用了这样的订阅代码

    var params = {
Protocol: 'application', /* required */
TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxx:TOPIC_NAME', /* required */
Endpoint: 'MOBILE_ENDPOINT_ARN'
};

// Create promise and SNS service object
var subscribePromise = new AWS.SNS({ apiVersion: '2010-03-31' }).subscribe(params).promise();

req;
res;
// Handle promise's fulfilled/rejected states
subscribePromise.then(
function (data) {
console.log("Subscription ARN is " + data.SubscriptionArn);
}).catch(
function (err) {
console.error(err, err.stack);
});
}

我的问题是订阅参数中的 Endpoint 是什么。我应该从哪里得到这个?到目前为止,我是对的吗?请帮帮我。

最佳答案

此处的端点是您需要向 AWS 注册的移动应用程序的 ARN。这是官方文档的片段

For Amazon SNS to send notification messages to mobile endpoints, whether it is direct or with subscriptions to a topic, you first need to register the app with AWS.

来源:https://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-register.html

关于javascript - 使用 Amazon SNS 推送通知( Node ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57566934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com