0

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
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
  • 1
    You can catch the exception and inspect the full response payload sent by the FCM service, which should contain more details about the issue. See https://firebase.google.com/docs/reference/admin/error-handling#python_4 – Hiranya Jayathilaka Mar 10 '21 at 20:14
  • @HiranyaJayathilaka Thanks for the link. I added some error handling and it says my `registration token` is invalid. However, I never change that for the calls that fail and succeed. – Prerak Sola Mar 11 '21 at 15:08
  • Registration tokens can renew on their own for various reasons. See https://stackoverflow.com/questions/41982619/when-does-a-fcm-token-expire – Hiranya Jayathilaka Mar 11 '21 at 18:36
  • Yes I am aware of these. But nothing fits my scenario. If a token has renewed, it should never work again. But in my case, it works -> fails -> works. – Prerak Sola Mar 12 '21 at 11:17

0 Answers0