从 PYTHON(flask) 发送 fcm 但未在 android 端接收
Sending fcm from PYTHON(flask) but not receiving on android side
以下是我尝试发送数据消息 fcm 的三种不同方式。
1.使用 pyfcm
def send_fcm(fcm_tokens, title=None, body=None, data_message=None):
push_service = FCMNotification(api_key=app.config['FCM_KEY'])
try:
if type(fcm_tokens) is list:
print(fcm_tokens, data_message)
result = push_service.notify_multiple_devices(registration_ids=fcm_tokens, data_message=data_message)
print(result, '++++++++++++++', flush=True)
else:
print(fcm_tokens, 'single device', data_message)
result = push_service.notify_single_device(registration_id=fcm_tokens, data_message=data_message)
print(result, flush=True)
except errors.InvalidDataError as e:
print(e, flush=True)
2。使用 firebase_admin sdk
def send_fcm_with_sdk(tokens, data_message=None):
if type(tokens) is list:
message = messaging.MulticastMessage(
data=data_message,
tokens=tokens, )
response = messaging.send_multicast(message)
print('{0} messages were sent successfully'.format(response.success_count), flush=True)
else:
message = messaging.Message(
data=data_message,
token=tokens,
)
print(message)
response = messaging.send(message)
print('Successfully sent message:', response)
3。休息 api
def send_fcm_with_rest(tokens, title=None, body=None, image=None, data_message=None):
headers = {
'Content-Type': 'application/json',
'Authorization': 'key=xxxxxxxKEY',
'Content-Type': 'application/json'
}
url = 'https://fcm.googleapis.com/fcm/send'
if type(tokens) is list:
payload = {
"registration_ids": tokens,
"collapse_key": "type_a",
"data": data_message
}
else:
payload = {
"to": tokens,
"collapse_key": "type_a",
"data": data_message
}
print(json.dumps(payload))
resp = requests.post(url, headers=headers, data=json.dumps(payload))
print(resp.text.encode('utf8'), flush=True)
return resp
奇怪的是他们三个都运行正确并显示成功消息,success_id,但是android端收到了fcm的none .
我尝试从 POSTMAN 发送,从 Postman 发送,fcm 是在 Andriod 端接收的。
谁能告诉我我的代码有什么问题吗?
您的代码没问题,但是您应该使用 flask==1.1.2
版本,因为它是稳定版本。
三种发送fcm的方法都是正确的,后来查明是前端小哥的问题。所以任何人都可以根据需要使用这三种方法。
以下是我尝试发送数据消息 fcm 的三种不同方式。
1.使用 pyfcm
def send_fcm(fcm_tokens, title=None, body=None, data_message=None):
push_service = FCMNotification(api_key=app.config['FCM_KEY'])
try:
if type(fcm_tokens) is list:
print(fcm_tokens, data_message)
result = push_service.notify_multiple_devices(registration_ids=fcm_tokens, data_message=data_message)
print(result, '++++++++++++++', flush=True)
else:
print(fcm_tokens, 'single device', data_message)
result = push_service.notify_single_device(registration_id=fcm_tokens, data_message=data_message)
print(result, flush=True)
except errors.InvalidDataError as e:
print(e, flush=True)
2。使用 firebase_admin sdk
def send_fcm_with_sdk(tokens, data_message=None):
if type(tokens) is list:
message = messaging.MulticastMessage(
data=data_message,
tokens=tokens, )
response = messaging.send_multicast(message)
print('{0} messages were sent successfully'.format(response.success_count), flush=True)
else:
message = messaging.Message(
data=data_message,
token=tokens,
)
print(message)
response = messaging.send(message)
print('Successfully sent message:', response)
3。休息 api
def send_fcm_with_rest(tokens, title=None, body=None, image=None, data_message=None):
headers = {
'Content-Type': 'application/json',
'Authorization': 'key=xxxxxxxKEY',
'Content-Type': 'application/json'
}
url = 'https://fcm.googleapis.com/fcm/send'
if type(tokens) is list:
payload = {
"registration_ids": tokens,
"collapse_key": "type_a",
"data": data_message
}
else:
payload = {
"to": tokens,
"collapse_key": "type_a",
"data": data_message
}
print(json.dumps(payload))
resp = requests.post(url, headers=headers, data=json.dumps(payload))
print(resp.text.encode('utf8'), flush=True)
return resp
奇怪的是他们三个都运行正确并显示成功消息,success_id,但是android端收到了fcm的none .
我尝试从 POSTMAN 发送,从 Postman 发送,fcm 是在 Andriod 端接收的。 谁能告诉我我的代码有什么问题吗?
您的代码没问题,但是您应该使用 flask==1.1.2
版本,因为它是稳定版本。
三种发送fcm的方法都是正确的,后来查明是前端小哥的问题。所以任何人都可以根据需要使用这三种方法。