Skip to content

FirebaseMessaging

chad63e edited this page Jan 29, 2024 · 1 revision

FirebaseMessaging

Table of Contents

Class Description

FirebaseMessaging is a class designed to facilitate server-side operations using Firebase services such as Firebase Cloud Messaging (FCM). It provides methods to initialize Firebase Admin SDK, manage topic subscriptions, and send messages to client devices.

Constructor

__init__(self, with_logging: Optional[bool] = True)

Constructs a FirebaseMessaging instance with optional logging capabilities.

Parameters:

  • with_logging (Optional[bool]): If set to True, enables logging of operations and responses.

Public Methods

initialize_firebase_admin(service_account_creds: FCMServiceAccountCredentials) -> bool

Initializes the Firebase Admin SDK with the provided service account credentials.

Parameters:

  • service_account_creds (FCMServiceAccountCredentials): The FCM service account credentials.

Returns:

  • bool: True if Firebase Admin SDK was successfully initialized, False otherwise.

subscribe_to_topic(topic: str, token: str) -> TopicManagementResponse

Subscribes a device to a specified topic using its FCM device token.

Parameters:

  • topic (str): The topic to subscribe to.
  • token (str): The FCM device token of the device.

Returns:

  • TopicManagementResponse: The response from the FCM server after subscription.

unsubscribe_from_topic(topic: str, token: str) -> TopicManagementResponse

Unsubscribes a device from a specified topic.

Parameters:

  • topic (str): The topic to unsubscribe from.
  • token (str): The FCM device token of the device.

Returns:

  • TopicManagementResponse: The response from the FCM server after unsubscription.

send(message: Union[Message, SimpleMessage], dry_run: Optional[bool] = False) -> Response

Sends a message to the FCM server.

Parameters:

  • message (Union[Message, SimpleMessage]): The message to send.
  • dry_run (Optional[bool]): If True, the message is sent in dry run mode.

Returns:

  • Response: The response from the FCM server.

send_all(messages: list[Union[Message, SimpleMessage]], dry_run: Optional[bool] = False) -> BatchResponse

Sends a list of messages to the FCM server.

Parameters:

  • messages (list[Union[Message, SimpleMessage]]): A list of messages to send.
  • dry_run (Optional[bool]): If True, the messages are sent in dry run mode.

Returns:

  • BatchResponse: The response from the FCM server.

send_multicast(message: MulticastMessage, dry_run: Optional[bool] = False) -> BatchResponse

Sends a multicast message to the FCM server.

Parameters:

  • message (MulticastMessage): The multicast message to send.
  • dry_run (Optional[bool]): If True, the message is sent in dry run mode.

Returns:

  • BatchResponse: The response from the FCM server.

Private Methods

_compile_webpush_notification_actions(message_dict: dict)

Compiles web push notification actions from the given message dictionary.

_compile_webpush_notification(message_dict: dict)

Compiles a web push notification from the given message dictionary.

_compile_webpush_config(message_dict: dict)

Compiles a web push configuration from the given message dictionary.

_compile_fcm_options(message_dict: dict)

Compiles FCM options from the given message dictionary.

_compile_message(message: Union[Message, SimpleMessage])

Compiles a message into the format required for sending via FCM.

_compile_multicast_message(message: MulticastMessage)

Compiles a multicast message for sending via FCM.

_process_data(data: dict) -> dict

Processes the data dictionary for sending in a message.

_log(message: str) -> None

Logs a message if logging is enabled.

Code Examples

Initializing FirebaseMessaging and Firebase Admin SDK

from anvil.secrets
from Firebase.server import FirebaseMessaging, FCMServiceAccountCredentials

# Retrieve credentials JSON string from Anvil Secrets
json_credentials = anvil.secrets.get_secret("YOUR_SECRET_KEY")

# Retrieve FCM Service Account Credentials
fcm_credentials = FCMServiceAccountCredentials.from_json_string(json_credentials)

# Initialize FirebaseMessaging
firebase_server = FirebaseMessaging(with_logging=True)

# Initialize Firebase Admin SDK
firebase_server.initialize_firebase_admin(fcm_credentials)

Subscribing to a Topic

topic = "news"
device_token = "your_device_token"
response = firebase_server.subscribe_to_topic(topic, device_token)

Sending a Message

from Firebase.messages import SimpleMessage

# Construct a simple message
message = SimpleMessage(token=device_token, data={"key": "value"})

# Send the message
response = firebase_server.send(message)

In these examples, the FirebaseMessaging class is used for server-side Firebase operations, including initializing the Firebase Admin SDK, managing topic subscriptions, and sending messages to devices. The class provides a structured approach to interact with Firebase services, ensuring efficient and secure server-side communication.

Clone this wiki locally