ทริกเกอร์การแจ้งเตือนของ Firebase

Firebase ให้การแจ้งเตือนสำหรับเหตุการณ์การจัดการโปรเจ็กต์และแอปที่หลากหลาย ต่อไปนี้เป็นตัวอย่างเหตุการณ์ที่ Firebase สามารถส่งการแจ้งเตือนประเภทนี้ถึงคุณเมื่อใด:

  • สำหรับ Crashlytics เราแจ้งเตือนคุณได้หากแอปของคุณมีข้อขัดข้องเพิ่มขึ้นอย่างมาก
  • สำหรับการตรวจสอบประสิทธิภาพ เราสามารถแจ้งเตือนคุณหากเวลาเริ่มต้นของแอปของคุณเกินเกณฑ์ที่กำหนดไว้
  • สำหรับการเผยแพร่แอป เราสามารถแจ้งเตือนคุณได้หากผู้ทดสอบคนใดคนหนึ่งของคุณลงทะเบียนอุปกรณ์ iOS ใหม่

Firebase จะแสดงการแจ้งเตือนประเภทเหล่านี้ในคอนโซล Firebase หรือส่งทางอีเมล ทั้งนี้ขึ้นอยู่กับการแจ้งเตือนและ การตั้งค่าที่กำหนดโดยสมาชิกโปรเจ็กต์

หน้านี้อธิบายวิธีเขียนฟังก์ชันใน Cloud Functions for Firebase (รุ่นที่ 2) ที่จัดการเหตุการณ์การแจ้งเตือน

มันทำงานอย่างไร?

คุณสามารถทริกเกอร์ฟังก์ชันเพื่อตอบสนองต่อเหตุการณ์การแจ้งเตือนที่มาจากแหล่งที่มาเหล่านี้:

ในวงจรการใช้งานทั่วไป ฟังก์ชันที่ถูกทริกเกอร์โดยเหตุการณ์การแจ้งเตือนจะดำเนินการดังต่อไปนี้:

  1. ฟัง/รอการแจ้งเตือนประเภทใดประเภทหนึ่งที่จะส่งออกจาก Firebase
  2. ทริกเกอร์เมื่อมีการส่งการแจ้งเตือน และรับเพย์โหลดเหตุการณ์ซึ่งมีข้อมูลเฉพาะเกี่ยวกับเหตุการณ์
  3. เรียกใช้โค้ดฟังก์ชันของคุณเพื่อจัดการเพย์โหลดเหตุการณ์

เรียกใช้ฟังก์ชันเมื่อมีเหตุการณ์แจ้งเตือน

ใช้แพ็คเกจย่อย firebase-functions/v2/alerts เพื่อเขียนฟังก์ชันที่จัดการเหตุการณ์การแจ้งเตือน ตัวอย่างเฉพาะผลิตภัณฑ์ต่อไปนี้สาธิตขั้นตอนการทำงานที่ฟังก์ชันใช้เว็บฮุคเพื่อโพสต์ข้อความไปยังช่องทาง Discord เมื่อมีการส่งการแจ้งเตือนสำหรับผลิตภัณฑ์นั้นจาก Firebase

จัดการเหตุการณ์การแจ้งเตือน Crashlytics

สำหรับตัวอย่าง Crashlytics ต่อไปนี้ คุณจะใช้ Cloud Functions สำหรับ Firebase เพื่อจัดการเหตุการณ์การแจ้งเตือนเกี่ยวกับปัญหาข้อขัดข้องร้ายแรงครั้งใหม่ ฟังก์ชั่นนี้จะโพสต์ข้อมูลการแจ้งเตือนในข้อความไปยังช่อง Discord

ตัวอย่างการแจ้งเตือนข้อขัดข้องใน Discord

ตัวอย่างการแจ้งเตือนสำหรับปัญหาข้อขัดข้องร้ายแรงครั้งใหม่

ฟังก์ชั่นรับฟังเหตุการณ์ที่เกี่ยวข้องกับ Firebase ที่เผยแพร่ปัญหาร้ายแรงใหม่:

โหนด js

exports.postfatalissuetodiscord = onNewFatalIssuePublished(async (event) => {

หลาม

@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."""

จากนั้นฟังก์ชันจะแยกวิเคราะห์ออบเจ็กต์เหตุการณ์ที่ส่งคืน แยกวิเคราะห์ข้อมูลที่เป็นประโยชน์จากเพย์โหลดของเหตุการณ์ และสร้างข้อความเพื่อโพสต์ไปยังช่องทาง Discord:

โหนด 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}\`
`;

หลาม

    # 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()

ในที่สุด ฟังก์ชันจะส่งข้อความที่สร้างขึ้นไปยัง Discord ผ่านการร้องขอ HTTP:

โหนด 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);
}

หลาม

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()

หากต้องการเรียนรู้เกี่ยวกับเหตุการณ์การแจ้งเตือน Crashlytics ทั้งหมดที่คุณบันทึกได้ โปรดไปที่เอกสารอ้างอิงสำหรับ การแจ้งเตือน Crashlytics

จัดการเหตุการณ์การแจ้งเตือนการตรวจสอบประสิทธิภาพ

ตัวอย่างนี้ส่งออกฟังก์ชันที่รับฟังเหตุการณ์การแจ้งเตือนเกณฑ์ประสิทธิภาพ:

โหนด js

exports.postperformancealerttodiscord = onThresholdAlertPublished(
    async (event) => {

หลาม

@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."""

จากนั้นฟังก์ชันจะแยกวิเคราะห์ออบเจ็กต์เหตุการณ์ที่ส่งคืน แยกวิเคราะห์ข้อมูลที่เป็นประโยชน์จากเพย์โหลดของเหตุการณ์ และสร้างข้อความเพื่อโพสต์ไปยังช่องทาง Discord:

โหนด 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}
    `;

หลาม

    # 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()

ในที่สุด ฟังก์ชันจะส่งข้อความที่สร้างขึ้นไปยัง Discord ผ่านการร้องขอ HTTP:

โหนด 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);
}

หลาม

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()

หากต้องการเรียนรู้เกี่ยวกับเหตุการณ์การแจ้งเตือนประสิทธิภาพทั้งหมดที่คุณสามารถบันทึกได้ โปรดไปที่เอกสารอ้างอิงสำหรับ การแจ้งเตือนการตรวจสอบประสิทธิภาพ

จัดการเหตุการณ์การแจ้งเตือน App Distribution

ตัวอย่างในส่วนนี้จะแสดงวิธีเขียนฟังก์ชันสำหรับการแจ้งเตือนอุปกรณ์ iOS ของผู้ทดสอบรายใหม่

ในตัวอย่างนี้ ฟังก์ชันจะรับฟังเหตุการณ์ที่ถูกส่งทุกครั้งที่ผู้ทดสอบลงทะเบียนอุปกรณ์ iOS ใหม่ เมื่อมีการลงทะเบียนอุปกรณ์ iOS ใหม่ คุณจะต้องอัปเดตโปรไฟล์การจัดสรรของคุณด้วย UDID ของอุปกรณ์นั้น จากนั้นจึงแจกจ่ายแอปอีกครั้ง

โหนด js

exports.postnewduuidtodiscord = onNewTesterIosDevicePublished(async (event) => {

หลาม

@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."""

จากนั้นฟังก์ชันจะแยกวิเคราะห์วัตถุที่ส่งคืน แยกวิเคราะห์ข้อมูลที่เป็นประโยชน์จากเพย์โหลดของเหตุการณ์ และสร้างข้อความเพื่อโพสต์ไปยังช่องทาง Discord:

โหนด 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}
`;

หลาม

    # 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()

ในที่สุด ฟังก์ชันจะส่งข้อความที่สร้างขึ้นไปยัง Discord ผ่านการร้องขอ HTTP:

โหนด 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);
}

หลาม

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()

หากต้องการเรียนรู้เกี่ยวกับเหตุการณ์การแจ้งเตือน App Distribution ทั้งหมดที่คุณสามารถบันทึกได้ โปรดไปที่เอกสารอ้างอิงสำหรับ การแจ้งเตือน App Distribution

หากต้องการเรียนรู้วิธีใช้ฟังก์ชันที่กระตุ้นโดย การแจ้งเตือน Firebase แบบตอบรับในแอปจาก App Distribution โปรดดู ที่ส่งความคิดเห็นในแอปไปยัง Jira