Firebase 알림 트리거

Firebase는 다양한 프로젝트 및 앱 관리 이벤트에 대한 알림을 제공합니다. 다음은 Firebase에서 이러한 유형의 알림을 보낼 수 있는 몇 가지 예시 이벤트입니다.

  • Crashlytics의 경우 앱의 비정상 종료가 급격하게 증가하면 알림을 보냅니다.
  • Performance Monitoring의 경우 앱 시작 시간이 구성된 임곗값을 초과하면 알림을 보낼 수 있습니다.
  • 앱 배포의 경우 테스터 중 한 명이 새 iOS 기기를 등록하면 알림을 보낼 수 있습니다.

알림 및 프로젝트 구성원이 설정한 환경설정에 따라 Firebase에서는 이러한 유형의 알림을 Firebase Console에 표시하거나 이메일을 통해 전송합니다.

이 페이지에서는 알림 이벤트를 처리하는 함수를 작성하는 방법을 설명합니다.

기본 원리

다음 소스에서 내보낸 알림 이벤트에 대한 응답으로 함수를 트리거할 수 있습니다.

일반적인 수명 주기에서 알림 이벤트에 의해 트리거되는 함수는 다음을 수행합니다.

  1. Firebase에서 내보낼 특정 알림 유형을 리슨하거나 대기합니다.
  2. 알림을 내보낼 때 트리거하고 이벤트에 대한 특정 정보가 포함된 이벤트 페이로드를 수신합니다.
  3. 함수 코드를 호출하여 이벤트 페이로드를 처리합니다.

알림 이벤트 발생 시 함수 트리거

firebase-functions/v2/alerts 하위 패키지를 사용하여 알림 이벤트를 처리하는 함수를 작성합니다. 다음 제품별 예시에서는 Firebase에서 제품 알림을 내보낼 때 함수가 웹훅을 사용하여 Discord 채널에 메시지를 게시하는 워크플로를 보여줍니다.

Crashlytics 알림 이벤트 처리

다음 Crashlytics 예시에서는 Firebase용 Cloud Functions를 사용하여 새로운 심각한 비정상 종료 문제에 대한 알림 이벤트를 처리합니다. 이 함수는 메시지의 알림 정보를 Discord 채널에 게시합니다.

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 알림 참조 문서를 확인하세요.

Performance Monitoring 알림 이벤트 처리

이 섹션의 예시에서는 성능 임곗값 알림 함수를 작성하는 방법을 보여줍니다.

이 예시에서 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);
        }

캡처할 수 있는 모든 성능 알림 이벤트에 대한 자세한 내용은 Performance Monitoring 알림 참조 문서를 확인하세요.

앱 배포 알림 이벤트 처리

이 섹션의 예시에서는 새 테스터 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);
    }

캡처할 수 있는 모든 앱 배포 알림 이벤트에 대한 자세한 내용은 앱 배포 알림 참조 문서를 확인하세요.

앱 배포의 인앱 의견 Firebase 알림에 의해 트리거되는 함수를 사용하는 방법을 알아보려면 Jira에 인앱 의견 보내기를 참조하세요.