我正在使用Firebase云消息传递构建android应用。 我的应用程序可以从FCM控制台接收消息。 但是,尽管响应良好,但它无法从python接收。 你能给我一些建议吗?
class fbMessaging():
def __init__(self):
cred = credentials.Certificate('./env/firebase.json')
firebase_admin.initialize_app(cred)
def send_to_device(self, text, token):
message = messaging.Message(
data = {
'title': 'test',
'body': text,
},
token = token,
)
response = messaging.send(message)
return response
def main():
fm = fbMessaging()
res = fm.send_to_device('test', 'MY CORRECT TOKEN')
print(res)
onMessageRecieved在这里
override fun onMessageReceived(message: RemoteMessage?) {
val from = message!!.from
val data = message.data
Log.d(TAG, "from:" + from!!)
Log.d(TAG, "data:$data")
}
下面是打印的回复。
projects / match-XXXXX / messages / 0:1554291593xxxxxx%43f99108f9xxxxxx
答案 0 :(得分:2)
使用Firebase Cloud Messaging,您可以发送通知有效载荷或数据有效载荷或两者。
通知有效载荷包含 title-通知标题 正文-通知正文
键名是固定的,不能更改。
另一方面,数据有效负载只是一个键值对,您可以发送任何以字符串类型作为其值的键名。
FCM行为:
根据应用是在前台还是在后台以及是否存在通知有效负载或数据有效负载,或两者都存在,FCM消息由应用中的不同组件接收。
根据文档处理FCM通知
您的应用在后台运行时发送的通知消息。在这种情况下,通知将发送到设备的系统托盘。用户点击通知会默认打开应用启动器。
在后台接收到的同时具有通知和数据有效载荷的消息。在这种情况下,通知将传递到设备的系统托盘,数据有效载荷将在附加组件中传递启动器活动的意图。
Receive Messages Section中对此行为进行了清楚的解释。
如您所见,如果仅在独立发送Notification有效负载的情况下,则不必构建Notification UI。否则,在调用onMessageReceived
时会有create the Notification UI。
使用Python:
通知有效负载示例:
message = messaging.Message(
notification=messaging.Notification(
title='This is a Notification Title',
body='This is a Notification Body',
),
token=registration_token,
)
数据有效负载示例:
message = messaging.Message(
data={
'score': '850',
'time': '2:45',
},
token=registration_token,
两者:
message = messaging.Message(
notification=messaging.Notification(
title='This is a Notification Title',
body='This is a Notification Body',
),
data={
'score': '850',
'time': '2:45',
},
token=registration_token,