firebase_admin.messaging module

Firebase Cloud Messaging module.

Exceptions

exception firebase_admin.messaging.ApiCallError(code, message, detail=None)

Bases: exceptions.Exception

Represents an Exception encountered while invoking the FCM API.

code

A string error code.

message

A error message string.

detail

Original low-level exception.

Classes

class firebase_admin.messaging.APNSConfig(headers=None, payload=None)

Bases: object

APNS-specific options that can be included in a message.

Refer to APNS Documentation for more information.

Parameters:
  • headers – A dictionary of headers (optional).
  • payload – A messaging.APNSPayload to be included in the message (optional).
class firebase_admin.messaging.APNSPayload(aps, **kwargs)

Bases: object

Payload of an APNS message.

Parameters:
  • aps – A messaging.Aps instance to be included in the payload.
  • kwargs – Arbitrary keyword arguments to be included as custom fields in the payload (optional).
class firebase_admin.messaging.AndroidConfig(collapse_key=None, priority=None, ttl=None, restricted_package_name=None, data=None, notification=None)

Bases: object

Android-specific options that can be included in a message.

Parameters:
  • collapse_key – Collapse key string for the message (optional). This is an identifier for a group of messages that can be collapsed, so that only the last message is sent when delivery can be resumed. A maximum of 4 different collapse keys may be active at a given time.
  • priority – Priority of the message (optional). Must be one of high or normal.
  • ttl – The time-to-live duration of the message (optional). This can be specified as a numeric seconds value or a datetime.timedelta instance.
  • restricted_package_name – The package name of the application where the registration tokens must match in order to receive the message (optional).
  • data – A dictionary of data fields (optional). All keys and values in the dictionary must be strings. When specified, overrides any data fields set via Message.data.
  • notification – A messaging.AndroidNotification to be included in the message (optional).
class firebase_admin.messaging.AndroidNotification(title=None, body=None, icon=None, color=None, sound=None, tag=None, click_action=None, body_loc_key=None, body_loc_args=None, title_loc_key=None, title_loc_args=None, channel_id=None)

Bases: object

Android-specific notification parameters.

Parameters:
  • title – Title of the notification (optional). If specified, overrides the title set via messaging.Notification.
  • body – Body of the notification (optional). If specified, overrides the body set via messaging.Notification.
  • icon – Icon of the notification (optional).
  • color – Color of the notification icon expressed in #rrggbb form (optional).
  • sound – Sound to be played when the device receives the notification (optional). This is usually the file name of the sound resource.
  • tag – Tag of the notification (optional). This is an identifier used to replace existing notifications in the notification drawer. If not specified, each request creates a new notification.
  • click_action – The action associated with a user click on the notification (optional). If specified, an activity with a matching intent filter is launched when a user clicks on the notification.
  • body_loc_key – Key of the body string in the app’s string resources to use to localize the body text (optional).
  • body_loc_args – A list of resource keys that will be used in place of the format specifiers in body_loc_key (optional).
  • title_loc_key – Key of the title string in the app’s string resources to use to localize the title text (optional).
  • title_loc_args – A list of resource keys that will be used in place of the format specifiers in title_loc_key (optional).
  • channel_id – channel_id of the notification (optional).
class firebase_admin.messaging.Aps(alert=None, badge=None, sound=None, content_available=None, category=None, thread_id=None, mutable_content=None, custom_data=None)

Bases: object

Aps dictionary to be included in an APNS payload.

Parameters:
  • alert – A string or a messaging.ApsAlert instance (optional).
  • badge – A number representing the badge to be displayed with the message (optional).
  • sound – Name of the sound file to be played with the message (optional).
  • content_available – A boolean indicating whether to configure a background update notification (optional).
  • category – String identifier representing the message type (optional).
  • thread_id – An app-specific string identifier for grouping messages (optional).
  • mutable_content – A boolean indicating whether to support mutating notifications at the client using app extensions (optional).
  • custom_data – A dict of custom key-value pairs to be included in the Aps dictionary (optional).
class firebase_admin.messaging.ApsAlert(title=None, body=None, loc_key=None, loc_args=None, title_loc_key=None, title_loc_args=None, action_loc_key=None, launch_image=None)

Bases: object

An alert that can be included in messaging.Aps.

Parameters:
  • title – Title of the alert (optional). If specified, overrides the title set via messaging.Notification.
  • body – Body of the alert (optional). If specified, overrides the body set via messaging.Notification.
  • loc_key – Key of the body string in the app’s string resources to use to localize the body text (optional).
  • loc_args – A list of resource keys that will be used in place of the format specifiers in loc_key (optional).
  • title_loc_key – Key of the title string in the app’s string resources to use to localize the title text (optional).
  • title_loc_args – A list of resource keys that will be used in place of the format specifiers in title_loc_key (optional).
  • action_loc_key – Key of the text in the app’s string resources to use to localize the action button text (optional).
  • launch_image – Image for the notification action (optional).
class firebase_admin.messaging.ErrorInfo(index, reason)

Bases: object

An error encountered when performing a topic management operation.

index

Index of the registration token to which this error is related to.

reason

String describing the nature of the error.

class firebase_admin.messaging.Message(data=None, notification=None, android=None, webpush=None, apns=None, token=None, topic=None, condition=None)

Bases: object

A message that can be sent via Firebase Cloud Messaging.

Contains payload information as well as recipient information. In particular, the message must contain exactly one of token, topic or condition fields.

Parameters:
  • data – A dictionary of data fields (optional). All keys and values in the dictionary must be strings.
  • notification – An instance of messaging.Notification (optional).
  • android – An instance of messaging.AndroidConfig (optional).
  • webpush – An instance of messaging.WebpushConfig (optional).
  • apns – An instance of messaging.ApnsConfig (optional).
  • token – The registration token of the device to which the message should be sent (optional).
  • topic – Name of the FCM topic to which the message should be sent (optional). Topic name may contain the /topics/ prefix.
  • condition – The FCM condition to which the message should be sent (optional).
class firebase_admin.messaging.Notification(title=None, body=None)

Bases: object

A notification that can be included in a message.

Parameters:
  • title – Title of the notification (optional).
  • body – Body of the notification (optional).
class firebase_admin.messaging.TopicManagementResponse(resp)

Bases: object

The response received from a topic management operation.

errors

A list of messaging.ErrorInfo objects (possibly empty).

failure_count

Number of tokens that could not be subscribed or unsubscribed due to errors.

success_count

Number of tokens that were successfully subscribed or unsubscribed.

class firebase_admin.messaging.WebpushConfig(headers=None, data=None, notification=None)

Bases: object

Webpush-specific options that can be included in a message.

Parameters:
  • headers – A dictionary of headers (optional). Refer Webpush Specification for supported headers.
  • data – A dictionary of data fields (optional). All keys and values in the dictionary must be strings. When specified, overrides any data fields set via Message.data.
  • notification – A messaging.WebpushNotification to be included in the message (optional).
class firebase_admin.messaging.WebpushNotification(title=None, body=None, icon=None, actions=None, badge=None, data=None, direction=None, image=None, language=None, renotify=None, require_interaction=None, silent=None, tag=None, timestamp_millis=None, vibrate=None, custom_data=None)

Bases: object

Webpush-specific notification parameters.

Refer to the Notification Reference for more information.

Parameters:
  • title – Title of the notification (optional). If specified, overrides the title set via messaging.Notification.
  • body – Body of the notification (optional). If specified, overrides the body set via messaging.Notification.
  • icon – Icon URL of the notification (optional).
  • actions – A list of messaging.WebpushNotificationAction instances (optional).
  • badge – URL of the image used to represent the notification when there is not enough space to display the notification itself (optional).
  • data – Any arbitrary JSON data that should be associated with the notification (optional).
  • direction – The direction in which to display the notification (optional). Must be either ‘auto’, ‘ltr’ or ‘rtl’.
  • image – The URL of an image to be displayed in the notification (optional).
  • language – Notification language (optional).
  • renotify – A boolean indicating whether the user should be notified after a new notification replaces an old one (optional).
  • require_interaction – A boolean indicating whether a notification should remain active until the user clicks or dismisses it, rather than closing automatically (optional).
  • silentTrue to indicate that the notification should be silent (optional).
  • tag – An identifying tag on the notification (optional).
  • timestamp_millis – A timestamp value in milliseconds on the notification (optional).
  • vibrate – A vibration pattern for the device’s vibration hardware to emit when the notification fires (optional). The pattern is specified as an integer array.
  • custom_data – A dict of custom key-value pairs to be included in the notification (optional)
class firebase_admin.messaging.WebpushNotificationAction(action, title, icon=None)

Bases: object

An action available to the users when the notification is presented.

Parameters:
  • action – Action string.
  • title – Title string.
  • icon – Icon URL for the action (optional).

Functions

firebase_admin.messaging.send(message, dry_run=False, app=None)

Sends the given message via Firebase Cloud Messaging (FCM).

If the dry_run mode is enabled, the message will not be actually delivered to the recipients. Instead FCM performs all the usual validations, and emulates the send operation.

Parameters:
  • message – An instance of messaging.Message.
  • dry_run – A boolean indicating whether to run the operation in dry run mode (optional).
  • app – An App instance (optional).
Returns:

A message ID string that uniquely identifies the sent the message.

Return type:

string

Raises:
  • ApiCallError – If an error occurs while sending the message to FCM service.
  • ValueError – If the input arguments are invalid.
firebase_admin.messaging.subscribe_to_topic(tokens, topic, app=None)

Subscribes a list of registration tokens to an FCM topic.

Parameters:
  • tokens – A non-empty list of device registration tokens. List may not have more than 1000 elements.
  • topic – Name of the topic to subscribe to. May contain the /topics/ prefix.
  • app – An App instance (optional).
Returns:

A TopicManagementResponse instance.

Return type:

TopicManagementResponse

Raises:
  • ApiCallError – If an error occurs while communicating with instance ID service.
  • ValueError – If the input arguments are invalid.
firebase_admin.messaging.unsubscribe_from_topic(tokens, topic, app=None)

Unsubscribes a list of registration tokens from an FCM topic.

Parameters:
  • tokens – A non-empty list of device registration tokens. List may not have more than 1000 elements.
  • topic – Name of the topic to unsubscribe from. May contain the /topics/ prefix.
  • app – An App instance (optional).
Returns:

A TopicManagementResponse instance.

Return type:

TopicManagementResponse

Raises:
  • ApiCallError – If an error occurs while communicating with instance ID service.
  • ValueError – If the input arguments are invalid.

Send feedback about...

Need help? Visit our support page.