【问题标题】:How to send push notification via Lambda by Firebase using Python如何使用 Python 通过 Firebase 通过 Lambda 发送推送通知
【发布时间】:2019-08-05 04:41:24
【问题描述】:

我正在尝试通过 AWS Lambda 从 Firebase Cloud Message 发送推送通知。 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

【问题讨论】:

    标签: python firebase aws-lambda firebase-cloud-messaging


    【解决方案1】:

    一切都很完美 - 除了标题。您必须在实际密钥之前添加“Key=”。请看下面的代码:

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

    【讨论】:

    • 谢谢!这行得通。我认为“授权”头应该只包含实际的密钥。所以,我之前删除了“Key=”这个词。
    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 2020-12-24
    • 1970-01-01
    • 2017-07-03
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多