Skip to content

SimpleMessage

chad63e edited this page Jan 29, 2024 · 1 revision

SimpleMessage

Class Description

SimpleMessage is a subclass of the Message class, designed for creating simple notifications in Firebase Cloud Messaging (FCM). This Anvil portable class extends Message with additional attributes specifically for setting up and sending notification-style messages.

Attributes

  • Inherits all attributes from Message, including data, token, topic, and condition.
  • title (str): The title of the notification.
  • body (str): The body text of the notification.
  • custom_data (dict, optional): Custom data to be included with the notification.
  • headers (dict, optional): Headers for the web push notification.
  • icon (str, optional): URL of the icon image for the notification.
  • image (str, optional): URL of an image for the notification.
  • badge (str, optional): URL of the badge for the notification.
  • link (str, optional): Link to open when the notification is clicked.
  • actions (list, optional): Actions available for the notification.
  • direction (str, optional): Text direction of the notification, defaults to 'auto'.
  • language (str, optional): Language of the notification.
  • renotify (bool, optional): If true, renotifies the user.
  • require_interaction (bool, optional): If true, requires user interaction.
  • silent (bool, optional): If true, makes the notification silent.
  • tag (str, optional): Tag for the notification.
  • timestamp_millis (int, optional): Timestamp of the notification in milliseconds.
  • vibrate (list, optional): Vibration pattern for the notification.

Constructor

__init__(self, title, body, token=None, topic=None, condition=None, data=None, custom_data=None, headers=None, icon=None, image=None, badge=None, link=None, actions=None, direction="auto", language=None, renotify=False, require_interaction=False, silent=False, tag=None, timestamp_millis=None, vibrate=None)

Initializes a new instance of the SimpleMessage class, specifically configured for sending notifications.

Parameters:

  • Parameters for title, body, notification settings, targeting, and additional notification options.

Methods

Inherits all methods from Message, including:

  • to_dict(): Converts the simple message object into a dictionary format.
  • message_type: A property that returns "simple_message" indicating the type of message.

Usage Notes

  • SimpleMessage is ideal for creating and sending notification-style messages through Firebase.
  • As an Anvil portable class, it can be seamlessly used between client and server code in Anvil applications.
  • This class provides a convenient and structured approach to crafting notification messages, ensuring all necessary details are included and appropriately formatted.

Example

from Firebase.messages import SimpleMessage

# Creating a simple notification message
simple_message = SimpleMessage(
    title="Hello",
    body="This is a notification",
    token="device_token",
    icon="https://example.com/icon.png",
    link="https://example.com"
)

# Converting the simple message to a dictionary
simple_message_dict = simple_message.to_dict()
print(simple_message_dict)

In this example, a SimpleMessage is created with a title, body, device token, and other notification parameters. The to_dict method is then used to convert the simple message object into a dictionary format, suitable for sending through Firebase SDKs.