简体   繁体   中英

Firebase Cloud Messaging Multicast not throwing exceptions when tokens are invalid

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.

  1. Based on the docs , response.exception is a FirebaseError but how do I know that the error is specifically related to a invalid token?
  2. response.exception prints a string and is not a FirebaseError Object such as the docs which gives access to the cause and code properties. Why is this so?
  3. 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
-----------------------------------

Based on the docs, response.exception is a FirebaseError but how do I know that the error is specifically related to a invalid token?

You can inspect the type of response.exception . For example:

isinstance(response.exception, messaging.UnregisteredError)

response.exception prints a string and is not a FirebaseError Object such as the docs which gives access to the cause and code properties. Why is this so?

It's printing a string since you're doing print(f'exception: {response.exception}') in your code. It is actually a FirebaseError object as mentioned in the docs.

Why doesn't the except catch FirebaseErrors?

send_multicast() doesn't throw on partial failures. So there's nothing to catch. You have to inspect the return value, and handle any errors in the return object accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM