如何使用Python通过Lambda发送推送通知

2024-04-25 23:17:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过AWS Lambda从Firebase云消息发送推送通知。API响应授权错误。在

导入请求 导入json

def lambda_handler(event, context):
    message = event['Message']
    tokens = event['PushNotificationTokens']
    for token in tokens:
        data = {"notification": { "title": "My Awesome App", "body": message,}, "to": token}
        data_json = json.dumps(data)
        print(data_json)
        headers = {'Content-type': 'application/json', 'Authorization':'AAAA…...0HuQH'}

    url = 'https://fcm.googleapis.com/fcm/send'    

    response = requests.post(url, data=data_json, headers=headers)

    jsonResponse = json.loads(response.content)
    print(jsonResponse)


    return jsonResponse

Tags: lambdatokenawseventjsonurlmessagedata
1条回答
网友
1楼 · 发布于 2024-04-25 23:17:43

一切都很完美-除了头球。必须在实际键之前添加“Key=”。参见以下代码:

headers = {'Content-type': 'application/json', 'Authorization':'Key=AAAA…...0HuQH'}

相关问题 更多 >