Skip to content

MulticastMessage

chad63e edited this page Jan 29, 2024 · 1 revision

MulticastMessage

Class Description

MulticastMessage is an Anvil portable class specifically designed for sending Firebase Cloud Messaging (FCM) messages to multiple devices. It encapsulates various parameters such as data payload, web push configuration, FCM options, and a list of device tokens for targeting.

Attributes

  • data (dict, optional): Key-value pairs that constitute the data payload of the message.
  • webpush (WebpushConfig, optional): Configuration for web push notifications.
  • fcm_options (WebpushFCMOptions, optional): FCM-specific options for the message.
  • tokens (list[str], optional): A list of FCM device tokens, representing the devices to which the message will be sent.
  • condition (str, optional): A condition expression specifying the devices that will receive the message.

Constructor

__init__(self, data=None, webpush=None, fcm_options=None, tokens=None, condition=None)

Initializes a new instance of the MulticastMessage class, facilitating the sending of an FCM message to multiple devices.

Parameters:

  • data (dict, optional): Data for the message.
  • webpush (WebpushConfig, optional): Webpush configuration.
  • fcm_options (WebpushFCMOptions, optional): FCM options.
  • tokens (list[str], optional): Target device tokens.
  • condition (str, optional): Conditional expression for targeting.

Methods

to_dict()

Converts the multicast message object to a dictionary, suitable for serialization and use with FCM SDKs.

message_type

A property that returns the type of the message, useful for identifying the message class in a system with multiple message types.

Usage Notes

  • MulticastMessage is designed for scenarios where you need to send the same message to multiple devices.
  • As an Anvil portable class, it can be used seamlessly between client and server code in Anvil applications.
  • This class provides a structured approach to create and manage multicast messages for Firebase, ensuring efficient and organized communication.

Example

from Firebase.messages import MulticastMessage

# Creating a multicast message
multicast_message = MulticastMessage(
    data={"key1": "value1", "key2": "value2"},
    tokens=["token1", "token2", "token3"]
)

# Converting the multicast message to a dictionary
multicast_message_dict = multicast_message.to_dict()
print(multicast_message_dict)

In this example, a MulticastMessage is created with a specific data payload and a list of device tokens. The to_dict method is used to convert the multicast message object into a dictionary format, which can then be used with Firebase SDKs for sending the message to the specified devices.