1515"""Firebase Cloud Messaging module."""
1616
1717from __future__ import annotations
18- from typing import Callable , List , Optional
18+ from typing import Any , Callable , Dict , List , Optional , cast
1919import concurrent .futures
2020import json
2121import warnings
3737 exceptions ,
3838 App
3939)
40- from firebase_admin ._retry import HttpxRetryTransport
4140
4241logger = logging .getLogger (__name__ )
4342
111110def _get_messaging_service (app : Optional [App ]) -> _MessagingService :
112111 return _utils .get_app_service (app , _MESSAGING_ATTRIBUTE , _MessagingService )
113112
114- def send (message , dry_run = False , app : Optional [App ] = None ):
113+ def send (message : Message , dry_run : bool = False , app : Optional [App ] = None ) -> str :
115114 """Sends the given message via Firebase Cloud Messaging (FCM).
116115
117116 If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
@@ -131,7 +130,11 @@ def send(message, dry_run=False, app: Optional[App] = None):
131130 """
132131 return _get_messaging_service (app ).send (message , dry_run )
133132
134- def send_each (messages , dry_run = False , app = None ):
133+ def send_each (
134+ messages : List [Message ],
135+ dry_run : bool = False ,
136+ app : Optional [App ] = None
137+ ) -> BatchResponse :
135138 """Sends each message in the given list via Firebase Cloud Messaging.
136139
137140 If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
@@ -451,7 +454,7 @@ class _MessagingService:
451454 'UNREGISTERED' : UnregisteredError ,
452455 }
453456
454- def __init__ (self , app ) -> None :
457+ def __init__ (self , app : App ) -> None :
455458 project_id = app .project_id
456459 if not project_id :
457460 raise ValueError (
@@ -476,7 +479,7 @@ def encode_message(cls, message):
476479 raise ValueError ('Message must be an instance of messaging.Message class.' )
477480 return cls .JSON_ENCODER .default (message )
478481
479- def send (self , message , dry_run = False ):
482+ def send (self , message : Message , dry_run : bool = False ) -> str :
480483 """Sends the given message to FCM via the FCM v1 API."""
481484 data = self ._message_data (message , dry_run )
482485 try :
@@ -489,9 +492,9 @@ def send(self, message, dry_run=False):
489492 except requests .exceptions .RequestException as error :
490493 raise self ._handle_fcm_error (error )
491494 else :
492- return resp ['name' ]
495+ return cast ( str , resp ['name' ])
493496
494- def send_each (self , messages , dry_run = False ):
497+ def send_each (self , messages : List [ Message ] , dry_run : bool = False ) -> BatchResponse :
495498 """Sends the given messages to FCM via the FCM v1 API."""
496499 if not isinstance (messages , list ):
497500 raise ValueError ('messages must be a list of messaging.Message instances.' )
@@ -683,7 +686,10 @@ def _build_fcm_error_requests(cls, error, message, error_dict):
683686
684687 @classmethod
685688 def _build_fcm_error_httpx (
686- cls , error : httpx .HTTPError , message , error_dict
689+ cls ,
690+ error : httpx .HTTPError ,
691+ message : str ,
692+ error_dict : Optional [Dict [str , Any ]]
687693 ) -> Optional [exceptions .FirebaseError ]:
688694 """Parses a httpx error response from the FCM API and creates a FCM-specific exception if
689695 appropriate."""
@@ -702,7 +708,11 @@ def _build_fcm_error_googleapiclient(cls, error, message, error_dict, http_respo
702708 return exc_type (message , cause = error , http_response = http_response ) if exc_type else None
703709
704710 @classmethod
705- def _build_fcm_error (cls , error_dict ) -> Optional [Callable [..., exceptions .FirebaseError ]]:
711+ def _build_fcm_error (
712+ cls ,
713+ error_dict : Optional [Dict [str , Any ]]
714+ ) -> Optional [Callable [..., exceptions .FirebaseError ]]:
715+ """Parses an error response to determine the appropriate FCM-specific error type."""
706716 if not error_dict :
707717 return None
708718 fcm_code = None
0 commit comments