
    (phV                     \   S r SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSK	r	SSK	J
r
  SSK	Jr  SSK	Jr  SSK	Jr  SS	K	Jr  SS
K	Jr  Sr/ SQr\R$                  r\R&                  r\R(                  r\R*                  r\R,                  r\R.                  r\R0                  r\R2                  r\R4                  r\R6                  r\R8                  r\R:                  r\R<                  r\R>                  r\R@                  r \RB                  r!\RD                  r"\RF                  r#\RH                  r$\RJ                  r%\RL                  r&\RN                  r'S r(SS jr)SS jr*SS jr+SS jr,SS jr-S S jr.S S jr/ " S S5      r0 " S S5      r1 " S S5      r2 " S S5      r3 " S S5      r4g)!z Firebase Cloud Messaging module.    N)http)_auth)_http_client)_messaging_encoder)_messaging_utils)_gapic_utils)_utils)
exceptions
_messaging)!AndroidConfigAndroidFCMOptionsAndroidNotification
APNSConfigAPNSFCMOptionsAPNSPayloadApsApsAlertBatchResponseCriticalSound	ErrorInfo
FCMOptionsLightSettingsMessageMulticastMessageNotificationQuotaExceededErrorSenderIdMismatchErrorSendResponseThirdPartyAuthErrorTopicManagementResponseUnregisteredErrorWebpushConfigWebpushFCMOptionsWebpushNotificationWebpushNotificationActionsendsend_allsend_multicast	send_eachsend_each_for_multicastsubscribe_to_topicunsubscribe_from_topicc                 B    [         R                  " U [        [        5      $ N)r	   get_app_service_MESSAGING_ATTRIBUTE_MessagingService)apps    K/var/www/html/venv/lib/python3.13/site-packages/firebase_admin/messaging.py_get_messaging_servicer4   d   s    !!#';=NOO    c                 6    [        U5      R                  X5      $ )a  Sends the given message via Firebase Cloud Messaging (FCM).

If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
recipients. Instead FCM performs all the usual validations, and emulates the send operation.

Args:
    message: An instance of ``messaging.Message``.
    dry_run: A boolean indicating whether to run the operation in dry run mode (optional).
    app: An App instance (optional).

Returns:
    string: A message ID string that uniquely identifies the sent message.

Raises:
    FirebaseError: If an error occurs while sending the message to the FCM service.
    ValueError: If the input arguments are invalid.
)r4   r&   )messagedry_runr2   s      r3   r&   r&   g   s    $ "#&++G==r5   c                 6    [        U5      R                  X5      $ )a  Sends each message in the given list via Firebase Cloud Messaging.

If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
recipients. Instead FCM performs all the usual validations, and emulates the send operation.

Args:
    messages: A list of ``messaging.Message`` instances.
    dry_run: A boolean indicating whether to run the operation in dry run mode (optional).
    app: An App instance (optional).

Returns:
    BatchResponse: A ``messaging.BatchResponse`` instance.

Raises:
    FirebaseError: If an error occurs while sending the message to the FCM service.
    ValueError: If the input arguments are invalid.
)r4   r)   messagesr8   r2   s      r3   r)   r)   {   s    $ "#&00CCr5   c                 J   [        U [        5      (       d  [        S5      eU R                   Vs/ s HN  n[	        U R
                  U R                  U R                  U R                  U R                  U R                  US9PMP     nn[        U5      R                  XA5      $ s  snf )a  Sends the given mutlicast message to each token via Firebase Cloud Messaging (FCM).

If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
recipients. Instead FCM performs all the usual validations, and emulates the send operation.

Args:
    multicast_message: An instance of ``messaging.MulticastMessage``.
    dry_run: A boolean indicating whether to run the operation in dry run mode (optional).
    app: An App instance (optional).

Returns:
    BatchResponse: A ``messaging.BatchResponse`` instance.

Raises:
    FirebaseError: If an error occurs while sending the message to the FCM service.
    ValueError: If the input arguments are invalid.
@Message must be an instance of messaging.MulticastMessage class.datanotificationandroidwebpushapnsfcm_optionstoken)
isinstancer   
ValueErrortokensr   r?   r@   rA   rB   rC   rD   r4   r)   multicast_messager8   r2   rE   r;   s        r3   r*   r*      s    $ ')9::[\\ %++- ,% ##&33!))!))##%11 ,  - "#&00CC-s   AB c                 l    [         R                  " S[        5        [        U5      R	                  X5      $ )a  Sends the given list of messages via Firebase Cloud Messaging as a single batch.

If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
recipients. Instead FCM performs all the usual validations, and emulates the send operation.

Args:
    messages: A list of ``messaging.Message`` instances.
    dry_run: A boolean indicating whether to run the operation in dry run mode (optional).
    app: An App instance (optional).

Returns:
    BatchResponse: A ``messaging.BatchResponse`` instance.

Raises:
    FirebaseError: If an error occurs while sending the message to the FCM service.
    ValueError: If the input arguments are invalid.

send_all() is deprecated. Use send_each() instead.
z2send_all() is deprecated. Use send_each() instead.)warningswarnDeprecationWarningr4   r'   r:   s      r3   r'   r'      s*    ( MMFHZ[!#&//BBr5   c                    [         R                  " S[        5        [        U [        5      (       d  [        S5      eU R                   Vs/ s HN  n[        U R                  U R                  U R                  U R                  U R                  U R                  US9PMP     nn[        U5      R                  XA5      $ s  snf )a  Sends the given mutlicast message to all tokens via Firebase Cloud Messaging (FCM).

If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
recipients. Instead FCM performs all the usual validations, and emulates the send operation.

Args:
    multicast_message: An instance of ``messaging.MulticastMessage``.
    dry_run: A boolean indicating whether to run the operation in dry run mode (optional).
    app: An App instance (optional).

Returns:
    BatchResponse: A ``messaging.BatchResponse`` instance.

Raises:
    FirebaseError: If an error occurs while sending the message to the FCM service.
    ValueError: If the input arguments are invalid.

send_multicast() is deprecated. Use send_each_for_multicast() instead.
zFsend_multicast() is deprecated. Use send_each_for_multicast() instead.r=   r>   )rL   rM   rN   rF   r   rG   rH   r   r?   r@   rA   rB   rC   rD   r4   r'   rI   s        r3   r(   r(      s    ( MMZ$&')9::[\\ %++- ,% ##&33!))!))##%11 ,  - "#&//BB-s   
AB;c                 8    [        U5      R                  XS5      $ )a  Subscribes a list of registration tokens to an FCM topic.

Args:
    tokens: A non-empty list of device registration tokens. List may not have more than 1000
        elements.
    topic: Name of the topic to subscribe to. May contain the ``/topics/`` prefix.
    app: An App instance (optional).

Returns:
    TopicManagementResponse: A ``TopicManagementResponse`` instance.

Raises:
    FirebaseError: If an error occurs while communicating with instance ID service.
    ValueError: If the input arguments are invalid.
ziid/v1:batchAddr4   make_topic_management_requestrH   topicr2   s      r3   r+   r+      s"      "#&DD(* *r5   c                 8    [        U5      R                  XS5      $ )a  Unsubscribes a list of registration tokens from an FCM topic.

Args:
    tokens: A non-empty list of device registration tokens. List may not have more than 1000
        elements.
    topic: Name of the topic to unsubscribe from. May contain the ``/topics/`` prefix.
    app: An App instance (optional).

Returns:
    TopicManagementResponse: A ``TopicManagementResponse`` instance.

Raises:
    FirebaseError: If an error occurs while communicating with instance ID service.
    ValueError: If the input arguments are invalid.
ziid/v1:batchRemoverQ   rS   s      r3   r,   r,      s"      "#&DD+- -r5   c                   >    \ rS rSrSrS r\S 5       r\S 5       rSr	g)r   i  zBAn error encountered when performing a topic management operation.c                     Xl         X l        g r.   _index_reason)selfindexreasons      r3   __init__ErrorInfo.__init__  s    r5   c                     U R                   $ )zBIndex of the registration token to which this error is related to.)rY   r[   s    r3   r\   ErrorInfo.index  s     {{r5   c                     U R                   $ )z*String describing the nature of the error.)rZ   ra   s    r3   r]   ErrorInfo.reason       ||r5   rX   N)
__name__
__module____qualname____firstlineno____doc__r^   propertyr\   r]   __static_attributes__ r5   r3   r   r     s2    L    r5   r   c                   N    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	Sr
g)	r    i!  z8The response received from a topic management operation.c                 x   [        U[        5      (       a  SU;  a  [        SR                  U5      5      eSU l        SU l        / U l        [        US   5       H^  u  p#SU;   a>  U =R
                  S-  sl        U R                  R                  [        X#S   5      5        MI  U =R                  S-  sl        M`     g )Nresultsz*Unexpected topic management response: {0}.r   error   )
rF   dictrG   format_success_count_failure_count_errors	enumerateappendr   )r[   respr\   results       r3   r^    TopicManagementResponse.__init__$  s    $%%$)>IPPQUVWW&tI7ME& ##q(###IeG_$EF##q(# 8r5   c                     U R                   $ )zCNumber of tokens that were successfully subscribed or unsubscribed.ru   ra   s    r3   success_count%TopicManagementResponse.success_count1       """r5   c                     U R                   $ )zLNumber of tokens that could not be subscribed or unsubscribed due to errors.)rv   ra   s    r3   failure_count%TopicManagementResponse.failure_count6  r   r5   c                     U R                   $ )z;A list of ``messaging.ErrorInfo`` objects (possibly empty).)rw   ra   s    r3   errorsTopicManagementResponse.errors;  re   r5   )rw   rv   ru   N)rf   rg   rh   ri   rj   r^   rk   r   r   r   rl   rm   r5   r3   r    r    !  sF    B) # # # #  r5   r    c                   N    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	Sr
g)	r   iA  z:The response received from a batch request to the FCM API.c                     Xl         [        U Vs/ s H  o"R                  (       d  M  UPM     sn5      U l        g s  snf r.   )
_responseslensuccessru   )r[   	responsesrz   s      r3   r^   BatchResponse.__init__D  s*    #!I"NID4I"NO"Ns   ;;c                     U R                   $ )z>A list of ``messaging.SendResponse`` objects (possibly empty).)r   ra   s    r3   r   BatchResponse.responsesH       r5   c                     U R                   $ r.   r~   ra   s    r3   r   BatchResponse.success_countM  s    """r5   c                 F    [        U R                  5      U R                  -
  $ r.   )r   r   r   ra   s    r3   r   BatchResponse.failure_countQ  s    4>>"T%7%777r5   )r   ru   N)rf   rg   rh   ri   rj   r^   rk   r   r   r   rl   rm   r5   r3   r   r   A  sG    DP   # # 8 8r5   r   c                   N    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	Sr
g)	r   iV  zHThe response received from an individual batched request to the FCM API.c                 \    X l         S U l        U(       a  UR                  SS 5      U l        g g )Nname)
_exception_message_idget)r[   rz   	exceptions      r3   r^   SendResponse.__init__Y  s*    ##xx5D r5   c                     U R                   $ )z9A message ID string that uniquely identifies the message.)r   ra   s    r3   
message_idSendResponse.message_id_  s     r5   c                 N    U R                   SL=(       a    U R                  (       + $ )z3A boolean indicating if the request was successful.N)r   r   ra   s    r3   r   SendResponse.successd  s!     t+CDOO0CCr5   c                     U R                   $ )zTA ``FirebaseError`` if an error occurs while sending the message to the FCM service.)r   ra   s    r3   r   SendResponse.exceptioni  r   r5   )r   r   N)rf   rg   rh   ri   rj   r^   rk   r   r   r   rl   rm   r5   r3   r   r   V  sH    R6     D D  r5   r   c                       \ rS rSrSrSrSrSrSS0r\	R                  " 5       r\\\\\S.rS	 r\S
 5       rSS jrSS jrSS jrS rS rS rS rS rS r\S 5       r\S 5       r\S 5       rSr g)r1   io  zKService class that implements Firebase Cloud Messaging (FCM) functionality.z8https://fcm.googleapis.com/v1/projects/{0}/messages:sendz https://fcm.googleapis.com/batchzhttps://iid.googleapis.comaccess_token_authtrue)APNS_AUTH_ERRORQUOTA_EXCEEDEDSENDER_ID_MISMATCHTHIRD_PARTY_AUTH_ERRORUNREGISTEREDc                    UR                   nU(       d  [        S5      e[        R                  R	                  U5      U l        SSR	                  [        R                  5      S.U l        UR                  R                  S[        R                  5      nUR                  R                  5       U l        [        R                   " U R                  US9U l        [$        R&                  U l        g )NzProject ID is required to access Cloud Messaging service. Either set the projectId option, or use service account credentials. Alternatively, set the GOOGLE_CLOUD_PROJECT environment variable.2zfire-admin-python/{0})zX-GOOG-API-FORMAT-VERSIONzX-FIREBASE-CLIENThttpTimeout)
credentialtimeout)
project_idrG   r1   FCM_URLrt   _fcm_urlfirebase_admin__version___fcm_headersoptionsr   r   DEFAULT_TIMEOUT_SECONDSr   get_credential_credentialJsonHttpClient_clientr   authorized_http_build_transport)r[   r2   r   r   s       r3   r^   _MessagingService.__init__  s    ^^
=> > *1188D),!8!?!?@Z@Z![
 ++//-1U1UV>>88:#22d>N>NX_` % 5 5r5   c                 x    [        U[        5      (       d  [        S5      eU R                  R	                  U5      $ )Nz7Message must be an instance of messaging.Message class.)rF   r   rG   JSON_ENCODERdefault)clsr7   s     r3   encode_message _MessagingService.encode_message  s2    '7++VWW''00r5   c                     U R                  X5      n U R                  R                  SU R                  U R                  US9nUS   $ ! [
        R                  R                   a  nU R                  U5      eSnAff = f)z2Sends the given message to FCM via the FCM v1 API.posturlheadersjsonr   N)	_message_datar   bodyr   r   requestsr
   RequestException_handle_fcm_error)r[   r7   r8   r?   rz   rq   s         r3   r&   _MessagingService.send  s    !!'3
	 <<$$MM))	 % D < ""33 	0((//	0s   0A A<&A77A<c                    ^  [        U[        5      (       d  [        S5      e[        U5      S:  a  [        S5      eU 4S jnU Vs/ s H  nT R	                  XB5      PM     nn [
        R                  R                  [        U5      S9 nUR                  X55       Vs/ s H  owPM     nn[        U5      sSSS5        $ s  snf s  snf ! , (       d  f       g= f! [         a)  n	[        R                  " SR                  U	5      U	S9eSn	A	ff = f)	z3Sends the given messages to FCM via the FCM v1 API.7messages must be a list of messaging.Message instances.  1messages must not contain more than 500 elements.c                   >  TR                   R                  STR                  TR                  U S9n[	        US S9$ ! [
        R                  R                   a#  n[	        S TR                  U5      S9s S nA$ S nAff = f)Nr   r   )r   )rz   r   )	r   r   r   r   r   r   r
   r   r   )r?   rz   r   r[   s      r3   	send_data._MessagingService.send_each.<locals>.send_data  s    	:||(( --	 )  $DD99 &&77 \#9O9OPY9Z[[\s   0= A>A93A>9A>)max_workersNz4Unknown error while making remote service calls: {0})r7   cause)rF   listrG   r   r   
concurrentfuturesThreadPoolExecutormapr   	Exceptionr
   UnknownErrorrt   )
r[   r;   r8   r   r7   message_dataexecutorrz   r   rq   s
   `         r3   r)   _MessagingService.send_each  s    (D))VWWx=3PQQ
	: MUUH**7<HU	##663|CT6UYa.6ll9.ST.SdT.S	T$Y/ VU V U VU  	))NUUV[\ 	sN   B?$&C 
C	C)C	5	C C		
CC C 
D$$DDc           
      r  ^ ^
 [        U[        5      (       d  [        S5      e[        U5      S:  a  [        S5      e/ m
U
U 4S jn[        R
                  " U[        R                  S9nT R                  T R                  5      nU Hp  n[        R                  " T R                  Xb5      5      n[        R                  " UT R                  T R                  SUT R                   S9nUR#                  U5        Mr      UR%                  5         ['        T
5      $ ! [(         a  n	T R+                  U	5      eSn	A	ff = f)	z2Sends the given messages to FCM via the batch API.r   r   r   c                 r   > S nU(       a  TR                  U5      n[        X5      nTR                  U5        g r.   )_handle_batch_errorr   ry   )_responserq   r   send_responser   r[   s        r3   batch_callback2_MessagingService.send_all.<locals>.batch_callback  s3    I 44U;	(=M]+r5   )callback	batch_uriPOST)r   postprocurimethodr   r   N)rF   r   rG   r   r   BatchHttpRequestr1   FCM_BATCH_URLr   r   r   dumpsr   HttpRequest	_postprocr   r   addexecuter   r   r   )r[   r;   r8   r   batch	transportr7   r   reqrq   r   s   `         @r3   r'   _MessagingService.send_all  s   (D))VWWx=3PQQ		, %%#/@/N/NP))$*:*:;	G::d00BCD""MM))C IIcN  	,MMO !++  	2**511	2s   ;D 
D6 D11D6c                    [        U[        5      (       a  U/n[        U[        5      (       a  U(       d  [        S5      eU Vs/ s H#  n[        U[        5      (       a	  U(       a  M!  UPM%     nnU(       a  [        S5      e[        U[        5      (       a  U(       d  [        S5      eUR	                  S5      (       d  SR                  U5      nUUS.nSR                  [        R                  U5      n U R                  R                  SUU[        R                  S	9n[        U5      $ s  snf ! [        R                  R                   a  n	U R                  U	5      eS
n	A	ff = f)z;Invokes the IID service for topic management functionality.z7Tokens must be a string or a non-empty list of strings.z!Tokens must be non-empty strings.z!Topic must be a non-empty string.z/topics/z/topics/{0})toregistration_tokensz{0}/{1}r   )r   r   r   N)rF   strr   rG   
startswithrt   r1   IID_URLr   r   IID_HEADERSr    r   r
   r   _handle_iid_error)
r[   rH   rT   	operationtinvalid_strr?   r   rz   rq   s
             r3   rR   /_MessagingService.make_topic_management_request  s8   fc""XF&$''vVWW"(L&Q
1c0B0B!q&L@AA%%%U@AA
++!((/E#)
 088)D
	1<<$$)55	 % D +400/ M( ""33 	0((//	0s$    D+(D+6*D0 0E$EE$c                 L    S[         R                  U5      0nU(       a  SUS'   U$ )Nr7   Tvalidate_only)r1   r   )r[   r7   r8   r?   s       r3   r   _MessagingService._message_data  s*    ,;;GDE$(D!r5   c                 J    [         R                  " UR                  5       5      $ )z'Handle response from batch API request.)r   loadsdecode)r[   r   r   s      r3   r   _MessagingService._postproc  s     zz$++-((r5   c                 L    [         R                  " U[        R                  5      $ )z)Handles errors received from the FCM API.)r	   #handle_platform_error_from_requestsr1   _build_fcm_error_requestsr[   rq   s     r3   r   #_MessagingService._handle_fcm_error  s#    99$>>@ 	@r5   c                    UR                   c  [        R                  " U5      e0 n UR                   R                  5       n[	        U[
        5      (       a  UnUR                  S5      nSnU(       a  SR                  U5      nOHSR                  UR                   R                  UR                   R                  R                  5       5      n[        R                  " X5      $ ! [         a     Nf = f)z1Handles errors received from the Instance ID API.Nrq   z(Error while calling the IID service: {0}z4Unexpected HTTP response with status: {0}; body: {1})r   r	   handle_requests_errorr   rF   rs   rG   r   rt   status_codecontentr  )r[   rq   r?   parsed_bodycodemsgs         r3   r  #_MessagingService._handle_iid_error  s    >>!..u55	..--/K+t,,"
 xx <CCDICHOO**ENN,B,B,I,I,KMC ++E77  		s   1C" "
C/.C/c                 L    [         R                  " U[        R                  5      $ )zMHandles errors received from the googleapiclient while making batch requests.)r   *handle_platform_error_from_googleapiclientr1    _build_fcm_error_googleapiclientr  s     r3   r   %_MessagingService._handle_batch_error0  s#    FF$EEG 	Gr5   c                 X    U R                  U5      nU(       a  U" X!UR                  S9$ S$ z^Parses an error response from the FCM API and creates a FCM-specific exception if
appropriate.)r   http_responseN)_build_fcm_errorr   )r   rq   r7   
error_dictexc_types        r3   r  +_MessagingService._build_fcm_error_requests5  s-     ''
3OWxENNKa]aar5   c                 D    U R                  U5      nU(       a  U" X!US9$ S$ r$  )r&  )r   rq   r7   r'  r%  r(  s         r3   r!  2_MessagingService._build_fcm_error_googleapiclient<  s)     ''
3NVxMJ`\``r5   c                     U(       d  g S nUR                  S/ 5       H+  nUR                  S5      S:X  d  M  UR                  S5      n  O   [        R                  R                  U5      $ )Ndetailsz@typez3type.googleapis.com/google.firebase.fcm.v1.FcmError	errorCode)r   r1   FCM_ERROR_TYPES)r   r'  fcm_codedetails       r3   r&  "_MessagingService._build_fcm_errorC  s^     nnY3Fzz'"&[[!::k2 4 !0044X>>r5   )r   r   r   r   r   N)F)!rf   rg   rh   ri   rj   r   r   r  r  r   MessageEncoderr   r   r   r   r!   r/  r^   classmethodr   r&   r)   r'   rR   r   r   r   r  r   r  r!  r&  rl   rm   r5   r3   r1   r1   o  s    UHG6M*G&/K%446L /,3"5)O6" 1 1
 :$,L1>)
@
80G
 b b a a ? ?r5   r1   )FNr.   )5rj   concurrent.futuresr   r   rL   r   googleapiclientr   r   r   r   r   r   r   r	   r
   r0   __all__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r#   r$   r%   r   r   r   r!   r4   r&   r)   r*   r'   r(   r+   r,   r   r    r   r   r1   rm   r5   r3   <module>r8     s   '       !  ' - + ' ! % $ #L !..$66 &:: ((
!00**$$ ..((
 ..

$
$%66 ,, ..$66 &:: ,FF %88 (>> &:: $66 P>(D(D>C.!CF*&-( $ @8 8* 2]? ]?r5   