|
1 | 1 | import firebase_admin
|
2 |
| -from firebase_admin import auth, db, messaging |
| 2 | +from firebase_admin import auth, db, messaging, exceptions |
3 | 3 | from firebase_functions import db_fn
|
4 | 4 |
|
5 | 5 | firebase_admin.initialize_app()
|
| 6 | +messaging.UnregisteredError |
6 | 7 |
|
7 | 8 |
|
8 | 9 | @db_fn.on_value_written(reference=r"followers/{followedUid}/{followerUid}")
|
@@ -45,10 +46,13 @@ def send_follower_notification(event: db_fn.Event[db_fn.Change]) -> None:
|
45 | 46 | if batch_response.failure_count < 1:
|
46 | 47 | # Messages sent sucessfully. We're done!
|
47 | 48 | return
|
| 49 | + |
48 | 50 | # Clean up the tokens that are not registered any more.
|
49 |
| - for response in batch_response.responses: |
50 |
| - if response.exception.code in ( |
51 |
| - "messaging/invalid-registration-token", |
52 |
| - "messaging/registration-token-not-registered", |
53 |
| - ): |
54 |
| - tokens_ref.child(response.message_id).delete() |
| 51 | + for i in range(len(batch_response.responses)): |
| 52 | + exception = batch_response.responses[i].exception |
| 53 | + if not isinstance(exception, exceptions.FirebaseError): |
| 54 | + continue |
| 55 | + message = exception.http_response.json()["error"]["message"] |
| 56 | + if (isinstance(exception, messaging.UnregisteredError) or |
| 57 | + message == "The registration token is not a valid FCM registration token"): |
| 58 | + tokens_ref.child(msgs[i].token).delete() |
0 commit comments