مشغلات Firebase Alerts

يوفر Firebase تنبيهًا لمجموعة واسعة من أحداث إدارة المشاريع والتطبيقات. فيما يلي بعض الأمثلة على الأحداث التي يمكن لـ Firebase أن يرسل إليك فيها هذا النوع من التنبيه:

  • بالنسبة إلى Crashlytics، يمكننا تنبيهك إذا كان تطبيقك يعاني من زيادة كبيرة في الأعطال.
  • بالنسبة لمراقبة الأداء، يمكننا تنبيهك إذا تجاوز وقت بدء تشغيل التطبيق الخاص بك الحد الذي قمت بتكوينه.
  • بالنسبة لتوزيع التطبيقات، يمكننا تنبيهك إذا قام أحد المختبرين لديك بتسجيل جهاز iOS جديد.

اعتمادًا على التنبيه والتفضيلات التي يحددها عضو المشروع ، يعرض Firebase هذه الأنواع من التنبيهات في وحدة تحكم Firebase أو يرسلها عبر البريد الإلكتروني.

توضح هذه الصفحة كيفية كتابة الوظائف في Cloud Functions for Firebase (الجيل الثاني) التي تتعامل مع أحداث التنبيه.

كيف يعمل؟

يمكنك تشغيل وظائف استجابةً لأحداث التنبيه المنبعثة من هذه المصادر:

في دورة الحياة النموذجية، تقوم الوظيفة التي يتم تشغيلها بواسطة حدث تنبيه بما يلي:

  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) => {

بايثون

@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}\`
`;

بايثون

    # 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);
}

بايثون

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) => {

بايثون

@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}
    `;

بايثون

    # 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);
}

بايثون

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

للتعرف على كافة أحداث تنبيهات الأداء التي يمكنك التقاطها، انتقل إلى الوثائق المرجعية الخاصة بتنبيهات مراقبة الأداء .

التعامل مع حدث تنبيه توزيع التطبيق

يوضح لك المثال الموجود في هذا القسم كيفية كتابة وظيفة لتنبيهات جهاز iOS المختبر الجديد.

في هذا المثال، تستمع الوظيفة إلى الأحداث التي يتم إرسالها في كل مرة يقوم فيها أحد المختبرين بتسجيل جهاز iOS جديد. عندما يتم تسجيل جهاز iOS جديد، فإنك تحتاج إلى تحديث ملف تعريف التوفير الخاص بك باستخدام UDID الخاص بهذا الجهاز ثم إعادة توزيع التطبيق.

Node.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:

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}
`;

بايثون

    # 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);
}

بايثون

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

للتعرف على جميع أحداث تنبيه توزيع التطبيق التي يمكنك التقاطها، انتقل إلى الوثائق المرجعية لتنبيهات توزيع التطبيق .

لمعرفة كيفية استخدام وظيفة يتم تشغيلها بواسطة تنبيه Firebase للتعليقات داخل التطبيق من App Distribution ، راجع إرسال تعليقات داخل التطبيق إلى Jira .