Skip to main content
 首页 » 编程设计

python之使用自定义变量消息通过 AWS Lambda/Python 作为 JSON 发送 Apple 推送通知

2024年10月17日29insus

你好,我正在尝试发送这样的推送通知

message3 = {"APNS_SANDBOX":"{\"aps\": {\"alert\": \"some alert\",\"sound\": \"default\",\"badge\": 1},\"message\": \"additional information\",\"id\": 1234}"} 
 
response = client.publish( 
    #TopicArn='string', 
    TargetArn = someEndpoint, 
    Message = json.dumps(message3), 
    MessageStructure= 'json' 
    #Subject='string', 
) 

一切正常。但我需要警报是可变的。如果我尝试将自定义文本变量放入 message3 json 对象中,我会不断收到此错误:

"errorType": "ClientError", 
"errorMessage": "An error occurred (InvalidParameter) when calling the Publish operation: Invalid parameter: Message Reason: Invalid notification for protocol APNS_SANDBOX: Notification is malformed" 

有人可以帮忙吗?谢谢!!

请您参考如下方法:

它似乎是这样工作的。

test = "test" 
message = {'aps': {'alert': test, 'sound': 'default','badge': 1},'message': 'additional information','id': 1234} 
dumped = json.dumps(message) 
 
message3 = {"APNS_SANDBOX":dumped} 
 
response = client.publish( 
    #TopicArn='string', 
    TargetArn = someEndpoint, 
    Message = json.dumps(message3), 
    MessageStructure= 'json' 
    #Subject='string', 
) 

这是你的意思吗,@kichik?非常感谢!