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 in Cloud Functions for Firebase (2nd gen)
that handle alert events.
How does it work?
You can trigger functions in response to alert events emitted by these sources:
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.
Example notification for a new fatal crash issue
The function listens for the event corresponding to Firebase
publishing a new fatal issue:
Node.js
exports.postfatalissuetodiscord = onNewFatalIssuePublished(async (event) => {
Python
@crashlytics_fn.on_new_fatal_issue_published(secrets=["DISCORD_WEBHOOK_URL"])
def post_fatal_issue_to_discord(event: crashlytics_fn.CrashlyticsNewFatalIssueEvent) -> None:
"""Publishes a message to Discord whenever a new Crashlytics fatal issue occurs."""
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:
Node.js
// 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}\`
`;
Python
# Construct a helpful message to send to Discord.
app_id = event.app_id
issue = event.data.payload.issue
message = f"""
?? New fatal issue for {app_id} in version {issue.app_version} ??
# {issue.title}
{issue.subtitle}
ID: `{issue.id}`
""".strip()
Finally, the function sends the constructed message to Discord through
an HTTP request:
Node.js
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);
}
Python
response = post_message_to_discord("Crashlytics Bot", message, DISCORD_WEBHOOK_URL.value)
if response.ok:
print(f"Posted fatal Crashlytics alert {issue.id} for {app_id} to Discord.")
pprint.pp(event.data.payload)
else:
response.raise_for_status()
To learn about all the Crashlytics alert events that you can capture, go to
the reference documentation for
Crashlytics alerts
.
This example exports a function that listens for performance threshold alert events:
Node.js
exports.postperformancealerttodiscord = onThresholdAlertPublished(
async (event) => {
Python
@performance_fn.on_threshold_alert_published(secrets=["DISCORD_WEBHOOK_URL"])
def post_performance_alert_to_discord(event: performance_fn.PerformanceThresholdAlertEvent) -> None:
"""Publishes a message to Discord whenever a performance threshold alert is fired."""
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:
Node.js
// 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}
`;
Python
# Construct a helpful message to send to Discord.
app_id = event.app_id
perf = event.data.payload
message = f"""
?? Performance Alert for {perf.metric_type} of {perf.event_type}: **{perf.event_name}** ??
App ID: {app_id}
Alert condition: {perf.threshold_value} {perf.threshold_unit}
Percentile (if applicable): {perf.condition_percentile}
App version (if applicable): {perf.app_version}
Violation: {perf.violation_value} {perf.violation_unit}
Number of samples checked: {perf.num_samples}
**Investigate more:** {perf.investigate_uri}
""".strip()
Finally, the function sends the constructed message to Discord through an HTTP
request:
Node.js
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);
}
Python
response = post_message_to_discord("App Performance Bot", message,
DISCORD_WEBHOOK_URL.value)
if response.ok:
print(f"Posted Firebase Performance alert {perf.event_name} to Discord.")
pprint.pp(event.data.payload)
else:
response.raise_for_status()
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 function listens for events that are 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.
Node.js
exports.postnewduuidtodiscord = onNewTesterIosDevicePublished(async (event) => {
Python
@app_distribution_fn.on_new_tester_ios_device_published(secrets=["DISCORD_WEBHOOK_URL"])
def post_new_udid_to_discord(event: app_distribution_fn.NewTesterDeviceEvent) -> None:
"""Publishes a message to Discord whenever someone registers a new iOS test device."""
The function then parses the returned object, parsing useful information from the event
payload and constructing a message to post to the Discord channel:
Node.js
// 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}
`;
Python
# Construct a helpful message to send to Discord.
app_id = event.app_id
app_dist = event.data.payload
message = f"""
?? New iOS device registered by {app_dist.tester_name} <{app_dist.tester_email}> for {app_id}
UDID **{app_dist.tester_device_identifier}** for {app_dist.tester_device_model_name}
""".strip()
Finally, the function sends the constructed message to Discord through an HTTP
request:
Node.js
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);
}
Python
response = post_message_to_discord("App Distro Bot", message, DISCORD_WEBHOOK_URL.value)
if response.ok:
print(f"Posted iOS device registration alert for {app_dist.tester_email} to Discord.")
pprint.pp(event.data.payload)
else:
response.raise_for_status()
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
.