Firebase Alerts triggers

Firebase provides alerting for a wide range of events, such as billing changes or Crashlytics velocity alerts. Depending on the alert and the preferences set by the project member, an alert can be viewable in the Firebase console, sent via email, or sent via one of the available third-party integrations (Slack, Jira, and PagerDuty). Each of these alerts and its preferences are configured in the Firebase console.

Additionally, Firebase provides customizable programmatic access to some kinds of alerts through Cloud Functions.

You can trigger functions in response to Firebase Alert events emitted by these sources:

  • Crashlytics
  • App Distribution
  • Firebase billing

For example, you could capture a Crashlytics alert and send notifications of crashes to Discord. Or you could capture a billing event (like a change in the billing plan for the project), and trigger a function that sends the project Owner a notification.

In a typical lifecycle, a Firebase Alert function does the following:

  1. Listens/waits for a specific alert type to be emitted from Firebase Alerts.
  2. Triggers when the alert fires, and receives the event payload with specific information about the event.
  3. Performs the tasks defined in the function.

See the reference documentation for alert types and payloads for Crashlytics, App Distribution, and billing.

Trigger a function on alert events

Use the firebase-functions/v2/alerts subpackage to create a function that handles Firebase Alerts events. Code samples in this page are based on an app flow that uses a webhook to post a message in a Discord channel when a severe/fatal issue occurs in the app.

Example crash notification in Discord
Example crash notification

The sample function listens to the onNewFatalIssuePublished event as shown:

exports.postmessagetodiscord = onNewFatalIssuePublished(async (event) => {

The function then parses the event object returned, constructing a message to post to the Discord channel:

  // construct a helpful message to send to Discord
  const {id, title, subtitle, appVersion} = event.data.payload.issue;
  const message = `
🚨 New fatal issue in version ${appVersion} 🚨

**${title}**

${subtitle}

id: \`${id}\`
`;

Finally, the function sends the constructed message to Discord:

    const response = await postMessageToDiscord("Crashlytics Bot", message);
    if (response.ok) {
      logger.info(
          `Posted fatal Crashlytics alert ${id} to Discord`,
          event.data.payload,
      );
    } else {
      throw new Error(response.error);
    }

In this way, you can leverage Cloud Functions for Firebase to respond to Firebase Alerts events with custom logic that's most useful for your team. In addition to integrating with services like Discord, you can write functions to go beyond console-configured integrations with providers such as Jira, Slack, or PagerDuty. For example, you can add custom fields or other functionality not already supported.