Firebase 为范围广泛的项目和应用程序管理事件提供警报。以下是 Firebase 何时可以向您发送此类警报的一些示例事件:
- 对于 Crashlytics,如果您的应用程序崩溃次数急剧增加,我们会提醒您。
- 对于性能监控,如果您的应用程序的启动时间超过您配置的阈值,我们会提醒您。
- 对于 App Distribution,如果您的测试人员之一注册了新的 iOS 设备,我们会提醒您。
根据警报和项目成员设置的首选项,Firebase 在 Firebase 控制台中显示这些类型的警报或通过电子邮件发送它们。
本页介绍如何编写处理警报事件的函数。
它是如何工作的?
您可以触发函数以响应这些来源发出的警报事件:
在典型的生命周期中,由警报事件触发的函数执行以下操作:
- 侦听/等待从 Firebase 发出的特定警报类型。
- 在发出警报时触发,并接收包含有关事件的特定信息的事件有效负载。
- 调用您的函数代码来处理事件负载。
触发警报事件的功能
使用firebase-functions/v2/alerts
子包编写处理警报事件的函数。以下特定于产品的示例演示了一个工作流程,其中当从 Firebase 发出该产品的警报时,函数使用 webhook 将消息发布到 Discord 频道。
处理 Crashlytics 警报事件
对于以下 Crashlytics 示例,您使用 Cloud Functions for Firebase 来处理新的致命崩溃问题的警报事件。此函数将消息中的警报信息发布到 Discord 频道。
该函数监听onNewFatalIssuePublished
事件:
exports.postfatalissuetodiscord = onNewFatalIssuePublished(async (event) => {
然后该函数解析返回的事件对象,构造一条消息以发布到 Discord 频道:
// 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}\` `;
最后,该函数将构建的消息发送到 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); }
要了解您可以捕获的所有 Crashlytics 警报事件,请转到Crashlytics 警报的参考文档。
处理性能监控警报事件
本节中的示例向您展示如何编写性能阈值警报函数。
在此示例中,在onThresholdAlertPublished
函数上导出了一个云函数,该函数侦听类型为PerformanceEvent<ThresholdAlertPayload>
的事件,每次触发性能阈值警报时都会发送该事件:
exports.postperformancealerttodiscord = onThresholdAlertPublished( async (event) => {
然后该函数解析返回的事件对象,解析来自事件有效负载的有用信息并构建消息以发布到 Discord 频道:
// 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} `;
最后,该函数通过 HTTP 请求将构建的消息发送到 Discord:
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); }
要了解您可以捕获的所有性能警报事件,请转到性能监控警报的参考文档。
处理 App Distribution 警报事件
本节中的示例向您展示了如何为新测试器 iOS 设备警报编写函数。
在此示例中, onNewTesterIosDevicePublished
函数导出一个云函数,用于侦听AppDistributionEvent<NewTesterDevicePayload>
类型的事件,每次测试人员注册新的 iOS 设备时都会发送该事件。注册新的 iOS 设备时,您需要使用该设备的 UDID 更新您的配置文件,然后重新分发该应用程序。
exports.postnewduuidtodiscord = onNewTesterIosDevicePublished(async (event) => {
然后该函数解析返回的、解析来自事件有效负载的有用信息并构造一条消息以发布到 Discord 频道:
// 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} `;
最后,该函数通过 HTTP 请求将构建的消息发送到 Discord:
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); }
要了解您可以捕获的所有 App Distribution 警报事件,请转到App Distribution 警报的参考文档。