Boto3 发布消息到 SNS

3

我觉得我正在尝试做一些非常简单的事情,但在某个地方做错了或者漏掉了什么。

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

我有应用程序密钥、registrationId等等...我的代码是

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

然后我从AWS收到消息ID,在移动终端上弹出通知,但总是空白的,没有任何文本,“Hi there”在这种情况下。我尝试使用AWS SNS控制台,它可以正常工作,手机可以正常接收带有文本的通知。
我还尝试发送JSON,但结果相同,如果最简单的目标都失败了...最好修复此问题再尝试JSON :p 欢迎提出任何建议。
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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接