I am trying to implement a deletion of FCM notification tokens if the tokens are expired or not registered. I added a try-except block to catch specific errors related to the invalid tokens but it seems to not be triggering. However, manually looping through the batch response seems to work but I can't find the documentation of the different exceptions message.
- Based on the docs,
response.exceptionis aFirebaseErrorbut how do I know that the error is specifically related to a invalid token? response.exceptionprints a string and is not aFirebaseErrorObject such as the docs which gives access to the cause and code properties. Why is this so?- Why doesn't the except catch FirebaseErrors?
Would appreciate some help and recommendations on how to handle deletion of inactive tokens.
Code as follows:
try:
batch_response = messaging.send_multicast(message)
except messaging.UnregisteredError as err:
print('Error message:', err)
print('Error code:', err.code)
print('HTTP response:', err.http_response)
except exceptions.FirebaseError as ex:
print('Something else went wrong')
for (index, response) in enumerate(batch_response.responses):
print(f'status: {response.success}')
print(f'exception: {response.exception}')
print('-----------------------------------')
terminal prints:
status: False
exception: Requested entity was not found.
-----------------------------------
status: True
exception: None
-----------------------------------
status: False
exception: The registration token is not a valid FCM registration token
-----------------------------------
status: False
exception: The registration token is not a valid FCM registration token
-----------------------------------