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 警報的參考文檔。
要了解如何使用App Distribution 的應用內反饋 Firebase 警報觸發的功能,請參閱向 Jira 發送應用內反饋。