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

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

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

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

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

ทำงานอย่างไร

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

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

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

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

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

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

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

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

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

ฟังก์ชันจะเฝ้าติดตามเหตุการณ์ที่เกี่ยวข้องกับ Firebase ที่เผยแพร่ปัญหาใหม่ที่ร้ายแรง นั่นคือ

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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