[英]Firebase Cloud Messaging Multicast not throwing exceptions when tokens are invalid
如果令牌已過期或未注冊,我正在嘗試刪除 FCM 通知令牌。 我添加了一個 try-except 塊來捕獲與無效令牌相關的特定錯誤,但它似乎沒有觸發。 但是,手動循環批處理響應似乎可行,但我找不到不同異常消息的文檔。
response.exception
是FirebaseError
但我怎么知道該錯誤與無效令牌特別相關?response.exception
打印一個字符串,而不是FirebaseError
Object,例如可以訪問原因和代碼屬性的文檔。 為什么會這樣?希望獲得有關如何處理刪除非活動令牌的一些幫助和建議。
代碼如下:
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('-----------------------------------')
終端打印:
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
-----------------------------------
根據文檔, response.exception 是 FirebaseError 但我怎么知道該錯誤與無效令牌特別相關?
您可以檢查response.exception
的類型。 例如:
isinstance(response.exception, messaging.UnregisteredError)
response.exception 打印一個字符串,而不是 FirebaseError Object,例如可以訪問原因和代碼屬性的文檔。 為什么會這樣?
它正在打印一個字符串,因為您在代碼中執行print(f'exception: {response.exception}')
。 它實際上是文檔中提到的FirebaseError
object。
為什么 except 不能捕獲 FirebaseErrors?
send_multicast()
不會引發部分失敗。 所以沒什么可抓的。 您必須檢查返回值,並相應地處理返回 object 中的任何錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.