-
Notifications
You must be signed in to change notification settings - Fork 0
FirebaseMessaging
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.
Constructs a FirebaseMessaging
instance with optional logging capabilities.
-
with_logging (Optional[bool])
: If set to True, enables logging of operations and responses.
Initializes the Firebase Admin SDK with the provided service account credentials.
-
service_account_creds (FCMServiceAccountCredentials)
: The FCM service account credentials.
-
bool
: True if Firebase Admin SDK was successfully initialized, False otherwise.
Subscribes a device to a specified topic using its FCM device token.
-
topic (str)
: The topic to subscribe to. -
token (str)
: The FCM device token of the device.
-
TopicManagementResponse
: The response from the FCM server after subscription.
Unsubscribes a device from a specified topic.
-
topic (str)
: The topic to unsubscribe from. -
token (str)
: The FCM device token of the device.
-
TopicManagementResponse
: The response from the FCM server after unsubscription.
Sends a message to the FCM server.
-
message (Union[Message, SimpleMessage])
: The message to send. -
dry_run (Optional[bool])
: If True, the message is sent in dry run mode.
-
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.
-
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.
-
BatchResponse
: The response from the FCM server.
Sends a multicast message to the FCM server.
-
message (MulticastMessage)
: The multicast message to send. -
dry_run (Optional[bool])
: If True, the message is sent in dry run mode.
-
BatchResponse
: The response from the FCM server.
Compiles web push notification actions from the given message dictionary.
Compiles a web push notification from the given message dictionary.
Compiles a web push configuration from the given message dictionary.
Compiles FCM options from the given message dictionary.
Compiles a message into the format required for sending via FCM.
Compiles a multicast message for sending via FCM.
Processes the data dictionary for sending in a message.
Logs a message if logging is enabled.
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)
topic = "news"
device_token = "your_device_token"
response = firebase_server.subscribe_to_topic(topic, device_token)
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.
Home - Quick Start
Client
Messages
- Message
- SimpleMessage
- MulticastMessage
- WebpushConfig
- FCMOptions
- WebpushFCMOptions
- WebpushNotification
- WebpushNotificationAction
- Response
- BatchResponse
- TopicManagementResponse
Server