Skip to content

Message

chad63e edited this page Jan 29, 2024 · 1 revision

Message

Class Description

Message is a portable class in Anvil, designed to represent a Firebase Cloud Messaging (FCM) message. It encapsulates various parameters such as data payload, web push configuration, FCM options, and targeting information (like token, topic, or condition).

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.
  • token (str, optional): The registration token of the device to which the message should be sent.
  • topic (str, optional): The topic name for sending the message to multiple devices subscribed to this topic.
  • condition (str, optional): A condition expression that specifies the devices receiving the message.

Constructor

__init__(self, data=None, webpush=None, fcm_options=None, token=None, topic=None, condition=None)

Initializes a new instance of the Message class, allowing various parameters to be set for an FCM message.

Parameters:

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

Methods

to_dict()

Converts the 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

  • This class is an Anvil portable class, meaning it can be passed between client and server code in Anvil applications.
  • It's designed to work seamlessly with Firebase Messaging services, providing a structured way to create and manipulate messages to be sent via FCM.
  • The Message class is versatile, supporting various types of messages including notification messages, data messages, and messages with both notification and data.

Example

from Firebase.messages import Message

# Creating a message with data payload and topic
message = Message(
    data={"key1": "value1", "key2": "value2"},
    topic="news_updates"
)

# Converting the message to a dictionary
message_dict = message.to_dict()
print(message_dict)

In this example, the Message class is used to create an FCM message with a data payload and a specified topic. The to_dict method is then utilized to convert the message object into a dictionary format, which can be used with Firebase SDKs for sending the message.