gpt4 book ai didi

python - 使用 Firebase 云消息传递、Python API 发送通知失败

转载 作者:行者123 更新时间:2023-12-04 10:30:39 24 4
gpt4 key购买 nike

阅读 Firebase 文档,我编写了 Python 脚本,以便向我的 Android 应用程序发送一些通知。

import firebase_admin 
from firebase_admin import credentials, messaging

#USING ENV VARIABLE: GOOGLE_APPLICATION_CREDENTIALS
app = firebase_admin.initialize_app()

topic = 'uat-test'
notification = messaging.Notification(title="Title", body="Body")

# See documentation on defining a message payload.
message = messaging.Message(
notification=notification,
topic=topic, )

# Send a message to the devices subscribed to the provided topic.
messaging.send(message)

# Response is a message ID string.
print('Successfully sent message:', message)

身份验证信息存储在 GOOGLE_APPLICATION_CREDENTIALS 环境变量中,该变量指向我从 firebase.google.com 下载的私钥 json 文件。

即使我得到消息 ID 作为输出并且没有显示错误消息,我也没有在手机上看到任何通知。只有当我通过网络应用程序 (firebase.google.com) 使用 Firebase 控制台手动发送通知时,我才能成功收到通知,但我需要使该过程自动化。手动分派(dispatch)时发出的通知告诉我,我的应用程序成功订阅了该主题,但我不明白 Python API 为何不起作用。我不认为这是一个环境问题,因为我也尝试过 Node API,我得到了相同的结果。任何人都有一些线索?我觉得我错过了什么。我从我的电脑上运行脚本,我不知道它是否可以计算。

最佳答案

我找到了一种显示通知的方法。事实证明,由于我使用 Flutter Framework 进行开发,Android 应用只能通过旧版应用服务器协议(protocol)处理通知:Firebase documentation legacy protocol

使用这种方法,最终会显示通知。不知道iOS会不会有同样的问题。

关于python - 使用 Firebase 云消息传递、Python API 发送通知失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60441141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com