boto3发布消息SNS

问题描述 投票:0回答:2

我想我正在尝试做一些非常简单的事情,但我在某些时候做错了一些事情或遗漏了一些东西。

目标:使用 boto3(aws sdk 库)从 python 2.7 发送消息

我有应用程序密钥、注册ID等...我的代码是

response = client.publish(
                    TargetArn=platform_endpoint['EndpointArn'],
                    Message="Hi there"
                )

然后我从 aws 收到 messageId,并在终端 movil 中弹出通知,但始终为空,没有任何文本,在本例中为“您好”。我尝试使用 aws SNS 控制台并正常工作,手机正常收到文本通知。

我也尝试发送 JSON,但结果相同,如果最简单的目标失败了...最好修复此问题以使用 JSON :p

有任何建议都欢迎

python-2.7 amazon-sns boto3
2个回答
2
投票

最终解决方案是使用 json.dumps 将 JSON 转换为字符串

response = client.publish(
                TargetArn=platform_endpoint['EndpointArn'],
                Message=json.dumps(jsonObj),
                MessageStructure='json'
            )

0
投票

这将有助于发布电子邮件,先决条件是在 SNS 中创建主题并在 AWS 中的主题中确认订阅

import boto3
import ipaddress
import warnings
    
warnings.filterwarnings("ignore")

region = 'ap-southeast-1'
sns_client = boto3.client("sns", region_name=region)

topic_arn = '<topic-arn>'
msg = 'test string'
sub = 'test'


def publish_to_sns(msg, sub, topic_arn):
    topic_arn = topic_arn
    response = sns_client.publish(
        TopicArn=topic_arn,
        Message=msg,
        Subject=sub
    )
    print(response)


publish_to_sns(msg, sub, topic_arn)
© www.soinside.com 2019 - 2024. All rights reserved.