Firebase provides alerting for a wide range of project and app management events. Here are a few example events for when Firebase can send you this type of alert:
- For Crashlytics, we can alert you if your app has a dramatic increase in crashes.
- For Performance Monitoring, we can alert you if your app's start-up time crosses your configured threshold.
- For App Distribution, we can alert you if one of your testers registers a new iOS device.
Depending on the alert and the preferences set by the project member, Firebase shows these types of alerts in the Firebase console or sends them via email.
This page describes how to write functions that handle alert events.
How does it work?
You can trigger functions in response to alert events emitted by these sources:
- Handle an App Distribution alert event
- Handle a Crashlytics alert event
- Handle a Performance Monitoring alert event
In a typical lifecycle, a function triggered by an alert event does the following:
- Listens/waits for a specific alert type to be emitted from Firebase.
- Triggers when the alert is emitted, and receives the event payload which contains specific information about the event.
- Invokes your function's code to handle the event payload.
Trigger a function on alert events
Use the firebase-functions/v2/alerts
subpackage to write a function that
handles alerts events. The following product-specific examples demonstrate a
workflow where a function uses a webhook to post a message to a Discord channel
when an alert for that product is emitted from Firebase.
Handle a Crashlytics alert event
For the following Crashlytics example, you use Cloud Functions for Firebase to handle an alert event of a new fatal crash issue. This function posts the alert information in a message to a Discord channel.
The function listens to the onNewFatalIssuePublished
event:
exports.postfatalissuetodiscord = onNewFatalIssuePublished(async (event) => {
The function then parses the returned event object, constructing a message to post to the Discord channel:
// construct a helpful message to send to Discord const appId = event.appId; const {id, title, subtitle, appVersion} = event.data.payload.issue; const message = ` 🚨 New fatal issue for ${appId} 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} for ${appId} to Discord`, event.data.payload, ); } else { throw new Error(response.error); }
To learn about all the Crashlytics alert events that you can capture, go to the reference documentation for Crashlytics alerts.
Handle a Performance Monitoring alert event
The example in this section shows you how to write a function for performance threshold alerts.
In this example, on the onThresholdAlertPublished
function exports a cloud
function that listens to events of type
PerformanceEvent<ThresholdAlertPayload>
, which is sent every time a
performance threshold alert fires:
exports.postperformancealerttodiscord = onThresholdAlertPublished( async (event) => {
The function then parses the returned event object, parsing useful information from the event payload and constructing a message to post to the Discord channel:
// construct a helpful message to send to Discord const appId = event.appId; const { eventName, metricType, eventType, numSamples, thresholdValue, thresholdUnit, conditionPercentile, appVersion, violationValue, violationUnit, investigateUri, } = event.data.payload; const message = ` ⚠️ Performance Alert for ${metricType} of ${eventType}: **${eventName}** ⚠️ App id: ${appId} Alert condition: ${thresholdValue} ${thresholdUnit} Percentile (if applicable): ${conditionPercentile} App version (if applicable): ${appVersion} Violation: ${violationValue} ${violationUnit} Number of samples checked: ${numSamples} **Investigate more:** ${investigateUri} `;
Finally, the function sends the constructed message to Discord through an HTTP request:
const response = await postMessageToDiscord( "Firebase Performance Bot", message); if (response.ok) { logger.info( `Posted Firebase Performance alert ${eventName} to Discord`, event.data.payload, ); } else { throw new Error(response.error); }
To learn about all the performance alert events that you can capture, go to the reference documentation for Performance Monitoring alerts.
Handle an App Distribution alert event
The example in this section shows you how to write a function for new tester iOS device alerts.
In this example, the onNewTesterIosDevicePublished
function exports a
cloud function that listens to events of type
AppDistributionEvent<NewTesterDevicePayload>
, which is sent every time a
tester registers a new iOS device. When a new iOS device is registered, you need
to update your provisioning profile with that device's UDID and then
redistribute the app.
exports.postnewduuidtodiscord = onNewTesterIosDevicePublished(async (event) => {
The function then parses the returned, parsing useful information from the event payload and constructing a message to post to the Discord channel:
// construct a helpful message to send to Discord const appId = event.appId; const { testerDeviceIdentifier, testerDeviceModelName, testerEmail, testerName, } = event.data.payload; const message = ` 📱 New iOS device registered by ${testerName} <${testerEmail}> for ${appId} UDID **${testerDeviceIdentifier}** for ${testerDeviceModelName} `;
Finally, the function sends the constructed message to Discord through an HTTP request:
const response = await postMessageToDiscord("AppDistribution Bot", message); if (response.ok) { logger.info( `Posted iOS device registration alert for ${testerEmail} to Discord`, ); } else { throw new Error(response.error); }
To learn about all the App Distribution alert events that you can capture, go to the reference documentation for App Distribution alerts.
To learn how to use a function triggered by an in-app feedback Firebase alert from App Distribution, see Send in-app feedback to Jira.