اكتب وظائف السحابة للامتداد

عندما تقوم بإنشاء ملحق، فإنك تكتب منطقه باستخدام Cloud Functions، بنفس الطريقة التي تكتب بها وظيفة لن يتم استخدامها إلا في مشروعك الخاص. أنت تعلن عن وظائفك في ملف extension.yaml ، وعندما يقوم المستخدمون بتثبيت الامتداد الخاص بك، يتم نشر هذه الوظائف في مشروعهم.

راجع وثائق Cloud Functions للحصول على معلومات عامة حول استخدام Cloud Functions.

وظائف السحابة من الجيل الأول والثاني

يدعم Firebase كلاً من وظائف السحابة من الجيل الأول والثاني . ومع ذلك، تحتوي ملحقات Firebase حاليًا على بعض القيود على جيل الوظائف السحابية الذي يمكنك استخدامه مع أنواع معينة من المشغلات. لهذا السبب، تشتمل العديد من الملحقات على مزيج من وظائف الجيل الأول والثاني.

تمت الإشارة إلى دعم إنشاء الوظائف لكل نوع مشغل أدناه.

إعتبارات خاصة

  • تتطلب بعض تعريفات الوظائف تحديد المعلومات المحددة أيضًا في ملف extension.yaml . على سبيل المثال، لدى Cloud Firestore طريقة document() تحدد نمط المستند المطلوب مشاهدته، والإعلان المقابل له في extension.yaml يحتوي على حقل resource يحدد نفس الشيء.

    في هذه الحالات، يتم استخدام التكوين المحدد في ملف extension.yaml ويتم تجاهل التكوين المحدد في تعريف الوظيفة.

    من الممارسات الشائعة تحديد القيمة التي تم تكوينها في تعريف الوظيفة بغض النظر، من أجل التوثيق. الأمثلة الموجودة في هذه الصفحة تتبع هذا النمط.

  • يحتوي Cloud Functions 1st gen SDK على طريقة functions.config() وأمر functions:config:set CLI الذي يمكنك استخدامه للعمل مع القيم ذات المعلمات في وظائف الجيل الأول. تم إهمال هذه التقنية في Cloud Functions ولن تعمل على الإطلاق في الامتداد. بدلاً من ذلك، استخدم الوحدة functions.params (الموصى بها) أو process.env .

باستخدام تايب سكريبت

تصف معظم الوثائق الخاصة بتطوير الامتداد الخاص بك سير العمل باستخدام JavaScript for Cloud Functions for Firebase. ومع ذلك، يمكنك بدلاً من ذلك كتابة وظائفك باستخدام TypeScript.

في الواقع، جميع ملحقات Firebase الرسمية مكتوبة بلغة TypeScript. يمكنك مراجعة هذه الامتدادات للتعرف على بعض أفضل الممارسات لاستخدام TypeScript لامتدادك.

إذا قمت بكتابة وظائف ملحقك في TypeScript، فيجب عليك القيام بما يلي قبل تثبيت ملحقك:

  1. قم بتجميع الكود المصدري لوظائف ملحقك إلى JavaScript.

    يسمح لك أمر firebase ext:dev:init باختيار TypeScript لكتابة وظائفك. يوفر لك الأمر امتدادًا كاملاً وقابلاً للتثبيت بالإضافة إلى برنامج نصي للإنشاء يمكنك تشغيله باستخدام npm run build .

  2. في ملف package.json الخاص بك، تأكد من توجيه الحقل main إلى JavaScript الذي تم إنشاؤه.

  3. إذا كنت تقوم بتثبيت ملحقك أو تحميله من مصدر محلي، فقم بتجميع ملفات TypeScript أولاً.

مشغلات الوظائف المدعومة

مشغلات HTTP

يتم نشر وظيفة يتم تشغيلها بواسطة HTTP إلى نقطة نهاية https عامة ويتم تشغيلها عند الوصول إلى نقطة النهاية.

راجع وظائف الاتصال عبر طلبات HTTP في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة HTTP.

تعريف الوظيفة (الجيل الأول فقط)

import { https } from "firebase-functions/v1";

export const yourFunctionName = https.onRequest(async (req, resp) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      runtime: nodejs16
      httpsTrigger: {}
  - name: anotherFunction
    type: ...

وظائف قابلة للاستدعاء

تشبه الوظائف القابلة للاستدعاء الوظائف التي يتم تشغيلها بواسطة HTTP، ولكنها تنفذ بروتوكولًا يجعلها ملائمة للاتصال من التعليمات البرمجية من جانب العميل.

راجع وظائف الاتصال من تطبيقك في وثائق Cloud Functions للحصول على معلومات حول استخدام الوظائف القابلة للاستدعاء.

تعريف الوظيفة (الجيل الأول فقط)

import { https } from "firebase-functions/v1";

export const yourFunctionName = https.onCall(async (data, context) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      runtime: nodejs16
      httpsTrigger: {}
  - name: anotherFunction
    type: ...

مشغلات الوظيفة المجدولة

تعمل الوظيفة المجدولة بشكل متكرر بناءً على جدول زمني قابل للتخصيص.

راجع جدولة الوظائف في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف المجدولة.

تعريف الوظيفة (الجيل الأول فقط)

import { pubsub } from "firebase-functions/v1";

export const yourFunctionName = pubsub.schedule("every 6 hours").onRun((context) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      scheduleTrigger:
        schedule: 'every 5 minutes'
  - name: anotherFunction
    type: ...

فيما يلي الحقول الفرعية المتاحة للجدول scheduleTrigger :

مجال وصف
schedule
(مطلوب)

التردد الذي تريد تشغيل الوظيفة به.

يمكن لهذا الحقل قبول السلاسل التي تستخدم أيًا من تركيبتي الجملة (يلزم استخدام علامتي اقتباس مفردتين ):

timeZone
(خياري)

المنطقة الزمنية التي سيتم تشغيل الجدول فيها.

إذا كنت تريد أن يتمكن المستخدمون من تكوين الجدول الزمني عند تثبيت ملحقك، فأضف معلمة جديدة إلى ملف extension.yaml الخاص بك وقم بالإشارة إلى المعلمة في إعلان resource وظيفتك:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      scheduleTrigger:
        schedule: ${SCHEDULE_FREQUENCY}
  - name: anotherFunction
    type: ...

params:
  - param: SCHEDULE_FREQUENCY
    label: Schedule
    description: How often do you want to run yourFunctionName()?
    type: string
    default: 'every 5 minutes'  # Specifying a default is optional.
    required: true

مشغلات قائمة انتظار المهام

يتم تشغيل وظيفة قائمة انتظار المهام إما في أحداث دورة حياة ملحقك أو عند إضافتها يدويًا إلى قائمة انتظار مهام ملحقك باستخدام طريقة TaskQueue.enqueue() الخاصة بـ Admin SDK.

راجع التعامل مع أحداث دورة حياة ملحقك للحصول على معلومات حول وظائف الكتابة التي تتعامل مع أحداث دورة الحياة.

راجع وظائف قائمة الانتظار مع المهام السحابية في وثائق وظائف السحابة للحصول على معلومات حول كتابة وظائف قائمة انتظار المهام.

تعريف الوظيفة (الجيل الأول فقط)

import { tasks } from "firebase-functions/v1";

export const yourFunctionName = tasks.taskQueue().onDispatch(async (data, context) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: myTaskFunction
    type: firebaseextensions.v1beta.function
    description: >-
      Perform a task when triggered by a lifecycle event
    properties:
      taskQueueTrigger: {}

قم بتعيين خاصية taskQueueTrigger على {} أو خريطة للخيارات التي تضبط حدود المعدل وتعيد محاولة سلوك قائمة انتظار المهام (راجع ضبط قائمة انتظار المهام ).

إذا كنت تريد تشغيل وظيفتك في أحداث دورة حياة ملحقك، فأضف سجلات lifecycleEvents مع اسم الوظيفة ورسالة معالجة اختيارية، والتي سيتم عرضها في وحدة تحكم Firebase عند بدء المعالجة.

lifecycleEvents:
  onInstall:
    function: myTaskFunction
    processingMessage: Resizing your existing images
  onUpdate:
    function: myOtherTaskFunction
    processingMessage: Setting up your extension
  onConfigure:
    function: myOtherTaskFunction
    processingMessage: Setting up your extension

التحليلات

يتم تشغيل إحدى الوظائف التي يتم تشغيلها بواسطة Analytics عند تسجيل حدث Analytics محدد.

راجع مشغلات Google Analytics في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة Analytics.

تعريف الوظيفة (الجيل الأول فقط)

import { analytics } from "firebase-functions/v1";

export const yourFunctionName = analytics.event("event_name").onLog((event, context) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.analytics/eventTypes/event.log
        resource: projects/${PROJECT_ID}/events/ga_event
  - name: anotherFunction
    type: ...

إذا كنت تريد أن يتمكن المستخدمون من تكوين حدث Analytics للاستماع إليه عند تثبيت ملحقك، فأضف معلمة جديدة إلى ملف extension.yaml الخاص بك وقم بالإشارة إلى المعلمة في إعلان resource وظيفتك:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.analytics/eventTypes/event.log
        resource: projects/${PROJECT_ID}/events/${EVENT_NAME}
  - name: anotherFunction
    type: ...

params:
  - param: EVENT_NAME
    label: Analytics event
    description: What event do you want to respond to?
    type: string
    default: ga_event  # Specifying a default is optional.
    required: true

المصادقة

يتم تشغيل وظيفة المصادقة عند إنشاء مستخدم أو حذفه.

راجع مشغلات مصادقة Firebase في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بالمصادقة.

تعريف الوظيفة (الجيل الأول فقط)

import { auth } from "firebase-functions/v1";

export const yourFunctionName = auth.user().onCreate((user, context) => {
  // ...
});

export const yourFunctionName2 = auth.user().onDelete((user, context) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/firebase.auth/eventTypes/user.create
        resource: projects/${PROJECT_ID}
  - name: anotherFunction
    type: ...

يوضح الجدول التالي كيفية تحديد كل نوع من أنواع أحداث المصادقة المدعومة:

مشغل حدث وظائف السحابة eventType وصف
onCreate() providers/firebase.auth/eventTypes/user.create تم إنشاء مستخدم جديد
onDelete() providers/firebase.auth/eventTypes/user.delete تم حذف المستخدم

سحابة فايرستور

يتم تشغيل وظيفة Cloud Firestore عند إنشاء مستند أو تحديثه أو حذفه.

راجع مشغلات Cloud Firestore في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة Firestore.

تعريف الوظيفة (الجيل الأول فقط)

import { firestore } from "firebase-functions/v1";

export const yourFunctionName = firestore.document("collection/{doc_id}")
  .onCreate((snapshot, context) => {
    // ...
  });

export const yourFunctionName2 = firestore.document("collection/{doc_id}")
  .onUpdate((change, context) => {
    // ...
  });

export const yourFunctionName3 = firestore.document("collection/{doc_id}")
  .onDelete((snapshot, context) => {
    // ...
  });

export const yourFunctionName4 = firestore.document("collection/{doc_id}")
  .onWrite((change, context) => {
    // onWrite triggers on creation, update, and deletion.
    // ...
  });

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/cloud.firestore/eventTypes/document.write
        resource: projects/${PROJECT_ID}/databases/(default)/documents/collection/{documentID}
  - name: anotherFunction
    type: ...

يوضح الجدول التالي كيفية تحديد كل نوع من أنواع أحداث Cloud Firestore المدعومة:

مشغل حدث وظائف السحابة eventType وصف
onCreate() providers/cloud.firestore/eventTypes/document.create تم إنشاء مستند جديد
onDelete() providers/cloud.firestore/eventTypes/document.delete تم حذف المستند
onUpdate() providers/cloud.firestore/eventTypes/document.update تم تحديث الوثيقة
onWrite() providers/cloud.firestore/eventTypes/document.write تم إنشاء المستند أو حذفه أو تحديثه

إذا كنت تريد أن يتمكن المستخدمون من تكوين مسار المستند عند تثبيت ملحقك، فأضف معلمة جديدة إلى ملف extension.yaml الخاص بك وقم بالإشارة إلى المعلمة في إعلان resource وظيفتك:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/cloud.firestore/eventTypes/document.write
        resource: projects/${PROJECT_ID}/databases/(default)/documents/${YOUR_DOCUMENT_PATH}
  - name: anotherFunction
    type: ...

params:
  - param: YOUR_DOCUMENT_PATH
    label: Cloud Firestore path
    description: Where do you want to watch for changes?
    type: string
    default: path/to/{documentID}  # Specifying a default is optional.
    required: true

حانة/فرعية

يتم تشغيل وظيفة النشر/التشغيل الفرعي عند نشر رسالة في موضوع محدد.

راجع مشغلات Pub/Sub في وثائق Cloud Functions للحصول على معلومات حول كتابة وظائف Pub/Sub المشغلة.

تعريف الوظيفة (الجيل الأول فقط)

import { pubsub } from "firebase-functions/v1";

export const yourFunctionName = pubsub.topic("topic_name").onPublish((message, context) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.pubsub.topic.publish
        resource: projects/${PROJECT_ID}/topics/topic-name
  - name: anotherFunction
    type: ...

إذا كنت تريد أن يتمكن المستخدمون من تكوين موضوع Pub/Sub عند تثبيت ملحقك، فأضف معلمة جديدة إلى ملف extension.yaml الخاص بك وقم بالإشارة إلى المعلمة في إعلان resource وظيفتك:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.pubsub.topic.publish
        resource: projects/${PROJECT_ID}/topics/${PUBSUB_TOPIC}
  - name: anotherFunction
    type: ...

params:
  - param: PUBSUB_TOPIC
    label: Pub/Sub topic
    description: Which Pub/Sub topic do you want to watch for messages?
    type: string
    default: topic-name  # Specifying a default is optional.
    required: true

قاعدة بيانات الوقت الحقيقي

يتم تشغيل الوظيفة التي يتم تشغيلها بواسطة قاعدة بيانات الوقت الفعلي عند إنشاء مسار يطابق نمطًا محددًا أو تحديثه أو حذفه.

راجع مشغلات Realtime Database في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة RTDB.

تعريف الوظيفة (الجيل الأول فقط)

import { database } from "firebase-functions/v1";

export const yourFunctionName = database.ref("path/to/{item}")
  .onCreate((snapshot, context) => {
    // ...
  });

export const yourFunctionName2 = database.ref("path/to/{item}")
  .onUpdate((change, context) => {
    // ...
  });

export const yourFunctionName3 = database.ref("path/to/{item}")
  .onDelete((snapshot, context) => {
    // ...
  });

export const yourFunctionName4 = database.ref("path/to/{item}")
  .onWrite((change, context) => {
    // onWrite triggers on creation, update, and deletion.
    // ...
  });

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.database/eventTypes/ref.create
        # DATABASE_INSTANCE (project's default instance) is an auto-populated
        # parameter value. You can also specify an instance.
        resource: projects/_/instances/${DATABASE_INSTANCE}/refs/path/to/{itemId}
  - name: anotherFunction
    type: ...

يوضح الجدول التالي كيفية تحديد كل نوع من أنواع أحداث Cloud Firestore المدعومة:

مشغل حدث وظائف السحابة eventType وصف
onCreate() providers/google.firebase.database/eventTypes/ref.create تم إنشاء البيانات
onDelete() providers/google.firebase.database/eventTypes/ref.delete تم حذف البيانات
onUpdate() providers/google.firebase.database/eventTypes/ref.update تم تحديث البيانات
onWrite() providers/google.firebase.database/eventTypes/ref.write البيانات التي تم إنشاؤها أو حذفها أو تحديثها

إذا كنت تريد أن يتمكن المستخدمون من تكوين المسار لمشاهدته عند تثبيت ملحقك، فأضف معلمة جديدة إلى ملف extension.yaml الخاص بك وقم بالإشارة إلى المعلمة في إعلان resource وظيفتك:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.database/eventTypes/ref.create
        # DATABASE_INSTANCE (project's default instance) is an auto-populated
        # parameter value. You can also specify an instance.
        resource: projects/_/instances/${DATABASE_INSTANCE}/refs/${DB_PATH}
  - name: anotherFunction
    type: ...

params:
  - param: DB_PATH
    label: Realtime Database path
    description: Where do you want to watch for changes?
    type: string
    default: path/to/{itemId}  # Specifying a default is optional.
    required: true

التكوين عن بعد

يتم تشغيل وظيفة يتم تشغيلها عن طريق التكوين عن بعد عند تحديث قالب معلمة المشروع.

راجع مشغلات التكوين عن بعد في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة التكوين عن بعد.

تعريف الوظيفة (الجيل الأول فقط)

import { remoteConfig } from "firebase-functions/v1";

export const yourFunctionName = remoteConfig.onUpdate((version, context) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.firebase.remoteconfig.update
        resource: projects/${PROJECT_ID}
  - name: anotherFunction
    type: ...

سحابة التخزين

يتم تشغيل وظيفة Cloud Storage عند إنشاء كائن أو أرشفته أو حذفه، أو عند تغيير بيانات التعريف الخاصة به.

راجع مشغلات التخزين السحابي في وثائق وظائف السحابة للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة التخزين.

تعريف الوظيفة (الجيل الأول فقط)

import { storage } from "firebase-functions/v1";

export const yourFunctionName = storage.object().onFinalize((object, context) => {
  // ...
});

export const yourFunctionName2 = storage.object().onMetadataUpdate((object, context) => {
  // ...
});

export const yourFunctionName3 = storage.object().onArchive((object, context) => {
  // ...
});

export const yourFunctionName4 = storage.object().onDelete((object, context) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.storage.object.finalize
        # STORAGE_BUCKET (project's default bucket) is an auto-populated
        # parameter. You can also specify a bucket.
        resource: projects/_/buckets/${STORAGE_BUCKET}
  - name: anotherFunction
    type: ...

يوضح الجدول التالي كيفية تحديد كل نوع من أنواع أحداث Cloud Storage المدعومة:

مشغل حدث وظائف السحابة eventType وصف
onFinalize() google.storage.object.finalize تم إنشاء الكائن
onMetadataUpdate() google.storage.object.metadataUpdate تم تحديث بيانات تعريف الكائن
onArchive() google.storage.object.archive تمت أرشفة الكائن
onDelete() google.storage.object.delete تم حذف الكائن

إذا كنت تريد أن يتمكن المستخدمون من تكوين مجموعة التخزين عند تثبيت ملحقك، فأضف معلمة جديدة إلى ملف extension.yaml الخاص بك وقم بالإشارة إلى المعلمة في إعلان resource وظيفتك:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.storage.object.finalize
        resource: projects/_/buckets/${YOUR_BUCKET}
  - name: anotherFunction
    type: ...

params:
  - param: YOUR_BUCKET
    label: Cloud Storage bucket
    description: Which bucket do you want to watch for changes?
    type: selectResource
    resourceType: storage.googleapis.com/Bucket
    default: ${STORAGE_BUCKET}  # Specifying a default is optional.
    required: true

معمل الاختبار

يتم تشغيل وظيفة الاختبار المعملي عندما تنتهي مصفوفة الاختبار من اختباراتها.

راجع مشغلات Firebase Test Lab في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة Test Lab.

تعريف الوظيفة (الجيل الأول فقط)

import { testLab } from "firebase-functions/v1";

export const yourFunctionName = testLab.testMatrix().onComplete((matrix, context) => {
  // ...
});

إعلان الموارد (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.testing.testMatrix.complete
        resource: projects/${PROJECT_ID}/testMatrices/{matrixId}
  - name: anotherFunction
    type: ...

مشغلات تنبيه Crashlytics

يتم تشغيل الوظيفة التي يتم تشغيلها بواسطة Crashlytics عندما ينشر Crashlytics تنبيهًا.

راجع مشغلات Firebase Alerts في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة التنبيه.

تعريف الوظيفة (الجيل الثاني فقط)

import {
  onNewFatalIssuePublished,
  onNewNonfatalIssuePublished,
  onNewAnrIssuePublished,
  onRegressionAlertPublished,
  onVelocityAlertPublished,
  onStabilityDigestPublished,
} from "firebase-functions/v2/alerts/crashlytics";

export const yourFunctionName = onNewFatalIssuePublished((event) => {
  // ...
});

export const yourFunctionName2 = onNewNonfatalIssuePublished((event) => {
  // ...
});

export const yourFunctionName3 = onNewAnrIssuePublished((event) => {
  // ...
});

export const yourFunctionName4 = onRegressionAlertPublished((event) => {
  // ...
});

export const yourFunctionName5 = onVelocityAlertPublished((event) => {
  // ...
});

export const yourFunctionName6 = onStabilityDigestPublished((event) => {
  // ...
});

إعلان الموارد (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: crashlytics.newFatalIssue
  - name: anotherFunction
    type: ...

يمكنك استخدام القيم التالية alerttype

  • crashlytics.newFatalIssue
  • crashlytics.newNonfatalIssue
  • crashlytics.regression
  • crashlytics.stabilityDigest
  • crashlytics.velocity
  • crashlytics.newAnrIssue

مشغلات تنبيه مراقبة الأداء

يتم تشغيل وظيفة يتم تشغيلها بواسطة مراقبة الأداء عندما تقوم مراقبة الأداء بنشر تنبيه.

راجع مشغلات Firebase Alerts في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة التنبيه.

تعريف الوظيفة (الجيل الثاني فقط)

import { onThresholdAlertPublished } from "firebase-functions/v2/alerts/performance";

export const yourFunctionName = onThresholdAlertPublished((event) => {
  // ...
});

إعلان الموارد (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: performance.threshold
  - name: anotherFunction
    type: ...

مشغلات تنبيه توزيع التطبيق

يتم تشغيل الوظيفة التي يتم تشغيلها بواسطة "توزيع التطبيقات" عندما ينشر "App Distribution" تنبيهًا.

راجع مشغلات Firebase Alerts في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة التنبيه.

تعريف الوظيفة (الجيل الثاني فقط)

import {
  onNewTesterIosDevicePublished,
  onInAppFeedbackPublished
} from "firebase-functions/v2/alerts/appDistribution";

export const yourFunctionName = onNewTesterIosDevicePublished((event) => {
  // ...
});

export const yourFunctionName2 = onInAppFeedbackPublished((event) => {
  // ...
});

إعلان الموارد (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: appDistribution.inAppFeedback
  - name: anotherFunction
    type: ...

يمكنك استخدام القيم التالية alerttype

  • appDistribution.newTesterIosDevice
  • appDistribution.inAppFeedback

مشغلات الأحداث المخصصة (Eventarc)

يتم تشغيل الوظيفة التي يتم تشغيلها بواسطة Eventarc عند نشر نوع حدث معين إلى قناة معينة.

راجع إنشاء مشغلات الأحداث المخصصة والتعامل معها في وثائق Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها بواسطة Eventarc.

يمكنك أيضًا نشر الأحداث من ملحقاتك لمنح المستخدمين طريقة لإدراج منطق مخصص في ملحقك. راجع استخدام المنطق المخصص الذي يوفره المطور في ملحق .

تعريف الوظيفة (الجيل الثاني فقط)

import { onCustomEventPublished } from "firebase-functions/v2/eventarc";

export const yourFunctionName = onCustomEventPublished((event) => {
  // ...
});

إعلان الموارد (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      # LOCATION is a user-configured parameter value specified by the user
      # during installation.
      location: ${param:LOCATION}
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
        timeoutSeconds: 60
      eventTrigger:
        eventType: firebase.extensions.storage-resize-images.v1.complete
        channel: projects/${param:PROJECT_ID}/locations/us-central1/channels/firebase
  - name: anotherFunction
    type: ...

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

المثال أعلاه سينشئ مشغل حدث مخصص لقناة Firebase "الافتراضية" في منطقة us-central1 . يمكنك جعل اسم القناة والمنطقة قابلين للتخصيص باستخدام المعلمات. على سبيل المثال:


params:
  - param: EVENTARC_CHANNEL_NAME
    label: Eventarc channel name
    description: What is the name of the Eventarc channel.
    default: firebase
    type: string
    required: true

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      location: ${param:LOCATION}
      eventTrigger:
        eventType: firebase.extensions.storage-resize-images.v1.complete
        channel: projects/${param:PROJECT_ID}/locations/${param:LOCATION}/channels/${param:EVENTARC_CHANNEL_NAME}