For my mobile applications (Android and iOS), I am using Firebase Cloud Messaging (FCM) for push notifications.
My backend server, uses the FCM Admin Python SDK to send the notification to a particular device with some additional data. The code responsible for sending the notification is as follows:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import messaging
cred = credentials.Certificate('cert.json')
firebase_admin.initialize_app(cred)
def sendNotification(token, sessionId, authenticationUrl, deviceId):
# This registration token comes from the client FCM SDKs.
registration_token = token
message = messaging.Message(
token=registration_token,
data={
'sessionId': sessionId,
'authenticationUrl': authenticationUrl,
'deviceId': deviceId
},
android=messaging.AndroidConfig(ttl=0, priority='high'),
apns=messaging.APNSConfig(
headers={'apns-priority': '10'},
payload=messaging.APNSPayload(
aps=messaging.Aps(content_available=True)
)
)
)
# Send a message to the device corresponding to the provided registration token.
try:
response = messaging.send(message)
print('Successfully sent message:', response)
except Exception as e:
print(e)
Before, I had the follwing also as part of Aps
object but I removed it as the message and body is generated by the mobile application now (depending on mobile language):
alert=messaging.ApsAlert(title='Authentication Request', body='Would you like to authenticate?')
Since removing the ApsAlert
, the above code fails randomly with the error:
Request contains an invalid argument.
If I call the same function again without any changes, it succeeds after few attempts and again fails on some runs. There is no common pattern due to which this fails.
Is there a way, I can get some more information regarding the error in order to resolve it? Maybe, which is the invalid arguement.
Update
Based on @Hiranya's comment, I added some error handling and I was able to determine for which arguement it returns "Invalid arguement".
It does that for registration_token
but interestingly, the token is never modified during consecutive runs that fail and succeed.
So, still no clue regarding this behavior.
For info:
- Python version: 3.8
- firebase-admin version: 4.5.2