python - 如何使用 Python 通过 Firebase 通过 Lambda 发送推送通知
问题描述
我正在尝试通过 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
解决方案
一切都很完美——除了标题。您必须在实际密钥之前添加“Key=”。请参阅下面的代码:
headers = {'Content-type': 'application/json', 'Authorization':'Key=AAAA…...0HuQH'}
推荐阅读
- javascript - 从 html oninput() 事件调用 javascript 中的对象方法
- javascript - 无法更改:使用 JavaScript 悬停 CSS 属性
- flutter - 来自 Json 的 Flutter 中的多个复选框
- node.js - 使用 axios 发布请求时出现网络错误
- python - AttributeError:“NoneType”对象没有属性“legendHandles”,同时手动为自定义图例中的每个标签分配颜色
- jquery - 生成随机电子邮件地址并隐藏表单字段
- html - 后代的 CSS 选择器,我该如何选择它?
- javascript - 为每个组件传递一个 action() 时出错
- python-3.x - 带有 wtforms 的烧瓶:tinymce 不替换 textarea 表单
- perl - Perl 绕过 5 秒等待页面