kaz*_*621 3 android python-3.x firebase firebase-cloud-messaging
我正在使用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)
Run Code Online (Sandbox Code Playgroud)
onMessageRecieved在这里
override fun onMessageReceived(message: RemoteMessage?) {
val from = message!!.from
val data = message.data
Log.d(TAG, "from:" + from!!)
Log.d(TAG, "data:$data")
}
Run Code Online (Sandbox Code Playgroud)
印刷的答复如下。
项目/比赛-XXXXX /消息/ 0:1554291593xxxxxx%43f99108f9xxxxxx
使用Firebase Cloud Messaging,您可以发送通知有效负载或数据有效负载,或同时发送两者。
通知有效负载包含标题-通知标题正文-通知正文
密钥名称已固定,无法更改。
另一方面,Data Payload只是一个键值对,您可以发送任何以字符串类型作为其值的键名。
FCM行为:
根据应用程序是处于前台还是后台,以及是否存在通知有效负载或数据有效负载,或两者都存在,FCM消息将由应用程序中的不同组件接收。
根据文档处理FCM通知,
当您的应用程序在后台时传递的通知消息。在这种情况下,通知将传递到设备的系统托盘。用户点击通知会默认打开应用启动器。
在后台接收时同时具有通知和数据有效负载的消息。在这种情况下,通知将传递到设备的系统托盘,而数据有效载荷将在启动器活动的意图之外传递。
在“ 接收消息”部分中已清楚说明了此行为。
如您所见,如果仅在独立发送Notification有效负载的情况下,则不必构建Notification UI。否则,您将在调用时创建Notification UIonMessageReceived。
使用Python:
通知有效负载示例:
message = messaging.Message(
notification=messaging.Notification(
title='This is a Notification Title',
body='This is a Notification Body',
),
token=registration_token,
)
Run Code Online (Sandbox Code Playgroud)
数据有效负载示例:
message = messaging.Message(
data={
'score': '850',
'time': '2:45',
},
token=registration_token,
Run Code Online (Sandbox Code Playgroud)
都:
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,
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
311 次 |
| 最近记录: |