مشغِّلات تنبيهات Firebase

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

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

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

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

كيف تعمل هذه الميزة؟

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

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

  1. ينتظر/ينتظر وصول نوع تنبيه معيّن من Firebase.
  2. يتم تشغيله عند إطلاق التنبيه وتلقّي حمولة الحدث التي يحتوي على معلومات محددة عن الحدث.
  3. لاستدعاء رمز الدالة للتعامل مع حمولة الحدث.

تشغيل دالة عند أحداث التنبيه

استخدام الحزمة الفرعية firebase-functions/v2/alerts لكتابة دالة وتتعامل مع أحداث التنبيهات. توضح الأمثلة التالية الخاصة بالمنتج سير عمل معيّن عندما تستخدم دالة ردًّا تلقائيًا على الويب لنشر رسالة على قناة Discord عند انطلاق تنبيه لهذا المنتج من Firebase.

التعامل مع حدث تنبيه Crashlytics

بالنسبة إلى مثال Crashlytics التالي، يمكنك استخدام دوال Cloud Functions في 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()

للتعرّف على جميع أحداث تنبيه الأداء التي يمكنك تسجيلها، انتقِل إلى وثائق مرجعية تنبيهات مراقبة الأداء:

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

يوضّح المثال في هذا القسم كيفية كتابة دالة لمختبِر جديد على نظام التشغيل iOS. بتنبيهات الأجهزة.

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

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

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

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