عند إنشاء إضافة، يمكنك كتابة منطقها باستخدام Cloud Functions،
بالطريقة نفسها التي تكتب بها دالة لن يتم استخدامها إلا في
مشروعك الخاص. يمكنك الإعلان عن دوالّك في ملف extension.yaml، وعندما يثبّت المستخدمون إضافتك، يتم نشر هذه الدوالّ في مشروعهم.
يمكنك الاطّلاع على مستندات Cloud Functions للحصول على معلومات عامة حول استخدام Cloud Functions.
الجيلان الأول والثاني من Cloud Functions
يتيح Firebase استخدام كلّ من الجيل الأول والجيل الثانيCloud Functions. ومع ذلك، تفرض إضافات Firebase حاليًا بعض القيود على جيل دوال السحابة الإلكترونية الذي يمكنك استخدامه مع أنواع معيّنة من المشغّلات. لهذا السبب، تتضمّن العديد من الإضافات مزيجًا من دوال الجيلَين الأول والثاني.
يتم توضيح إمكانية إنشاء الدوال لكل نوع من أنواع المشغّلات أدناه.
اعتبارات خاصة
تتطلّب بعض تعريفات الدوال تحديد معلومات يتم تحديدها أيضًا في ملف
extension.yaml. على سبيل المثال، يحتوي Cloud Firestore على طريقةdocument()التي تحدّد نمط المستند المطلوب مراقبته، ويحتوي البيان المقابل لها فيextension.yamlعلى حقلresourceيحدّد النمط نفسه.في هذه الحالات، يتم استخدام الإعدادات المحدّدة في ملف
extension.yamlويتم تجاهل الإعدادات المحدّدة في تعريف الدالة.من الممارسات الشائعة تحديد القيمة التي تم ضبطها في تعريف الدالة بغض النظر عن ذلك، من أجل التوثيق. تتبع الأمثلة الموجودة في هذه الصفحة هذا النمط.
تتضمّن حزمة تطوير البرامج (SDK) للجيل الأول من Cloud Functions طريقة
functions.config()وأمر واجهة سطر الأوامر (CLI)functions:config:setاللذين يمكنك استخدامهما للتعامل مع قيم المَعلمات في دوال الجيل الأول. تم إيقاف هذه التقنية نهائيًا في Cloud Functions ولن تعمل على الإطلاق في أي إضافة. بدلاً من ذلك، استخدِم وحدةfunctions.params(يُنصح بها) أوprocess.env.
استخدام TypeScript
توضّح معظم المستندات الخاصة بتطوير الإضافات مهام سير العمل باستخدام JavaScript لـ Cloud Functions for Firebase. ومع ذلك، يمكنك بدلاً من ذلك كتابة الدوال باستخدام TypeScript.
في الواقع، تمت كتابة جميع إضافات Firebase الرسمية بلغة TypeScript. يمكنك مراجعة هذه الإضافات للاطّلاع على بعض أفضل الممارسات لاستخدام TypeScript في إضافتك.
إذا كنت تكتب دوال الإضافة بلغة TypeScript، عليك تنفيذ ما يلي قبل تثبيت الإضافة:
جمِّع الرمز المصدر لدوال الإضافة في JavaScript.
يتيح لك الأمر
firebase ext:dev:initاختيار TypeScript لكتابة الدوال. يوفّر لك الأمر إضافة كاملة قابلة للتثبيت بالإضافة إلى نص برمجي للإصدار يمكنك تشغيله باستخدامnpm run build.في ملف
package.json، تأكَّد من توجيه الحقلmainإلى ملف JavaScript الذي تم إنشاؤه.إذا كنت تثبّت الإضافة أو تحمّلها من مصدر محلي، عليك تجميع ملفات 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() في حزمة تطوير البرامج (SDK) للمشرف.
يمكنك الاطّلاع على التعامل مع أحداث يتم في مراحل النشاط للإضافة للحصول على معلومات حول كتابة الدوال التي تتعامل مع أحداث يتم في مراحل النشاط.
يمكنك الاطّلاع على إضافة الدوال إلى قائمة الانتظار باستخدام Cloud Tasks في مستندات Cloud Functions للحصول على معلومات حول كتابة دوال قائمة انتظار المهام.
تعريف الدالة (الجيل الأول فقط)
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
يتم تشغيل دالة يتم تشغيلها من خلال "إحصاءات Google" عند تسجيل حدث محدّد في "إحصاءات Google".
يمكنك الاطّلاع على مشغّلات "إحصاءات Google" في مستندات Cloud Functions للحصول على معلومات عن كتابة الدوال التي يتم تشغيلها من خلال "إحصاءات Google".
تعريف الدالة (الجيل الأول فقط)
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
Authentication
يتم تشغيل دالة يتم تشغيلها من خلال المصادقة عند إنشاء مستخدم أو حذفه.
يمكنك الاطّلاع على مشغّلات مصادقة 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: ...
يوضّح الجدول التالي كيفية تحديد كل نوع من أنواع أحداث Authentication المتوافقة:
| مشغِّل حدث واحد (Cloud Functions) | eventType |
الوصف |
|---|---|---|
onCreate() |
providers/firebase.auth/eventTypes/user.create |
تم إنشاء مستخدم جديد |
onDelete() |
providers/firebase.auth/eventTypes/user.delete |
تم حذف المستخدم |
Cloud Firestore
يتم تشغيل دالة يتم تشغيلها بواسطة 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:
| مشغِّل حدث واحد (Cloud Functions) | 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
يتم تشغيل دالة يتم تشغيلها من خلال Pub/Sub عند نشر رسالة في موضوع معيّن.
يمكنك الاطّلاع على مشغّلات 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:
| مشغِّل حدث واحد (Cloud Functions) | 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
Remote Config
يتم تشغيل دالة يتم تشغيلها من خلال ميزة "الإعداد عن بُعد" عند تعديل نموذج مَعلمات المشروع.
يُرجى الاطّلاع على مشغّلات "الإعداد عن بُعد" في مستندات 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
يتم تشغيل دالة Cloud Storage عند إنشاء عنصر أو أرشفته أو حذفه أو عند تغيير بياناته الوصفية.
يمكنك الاطّلاع على مشغّلات Cloud Storage في مستندات Cloud Functions للحصول على معلومات حول كتابة الدوال التي يتم تشغيلها من خلال 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:
| مشغِّل حدث واحد (Cloud Functions) | 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
Test Lab
يتم تشغيل دالة يتم تشغيلها بواسطة Test Lab عند انتهاء مصفوفة تجريبية من اختباراتها.
يمكنك الاطّلاع على مشغّلات مختبر Firebase الافتراضي في مستندات Cloud Functions للحصول على معلومات حول كتابة الوظائف التي يتم تشغيلها من خلال مركز الاختبار الافتراضي.
تعريف الدالة (الجيل الأول فقط)
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 في مستندات 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)
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.newFatalIssuecrashlytics.newNonfatalIssuecrashlytics.regressioncrashlytics.stabilityDigestcrashlytics.velocitycrashlytics.newAnrIssue
Performance Monitoring عوامل تشغيل التنبيهات
يتم تشغيل دالة يتم تشغيلها بواسطة Performance Monitoring عندما ينشر Performance Monitoring تنبيهًا.
يمكنك الاطّلاع على عوامل تشغيل تنبيهات Firebase في مستندات Cloud Functions للحصول على معلومات حول كتابة الدوال التي يتم تشغيلها من خلال التنبيهات.
تعريف الدالة (الجيل الثاني فقط)
import { onThresholdAlertPublished } from "firebase-functions/v2/alerts/performance";
export const yourFunctionName = onThresholdAlertPublished((event) => {
// ...
});
تعريف الموارد (extension.yaml)
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 عوامل تشغيل التنبيهات
يتم تشغيل دالة يتم تشغيلها بواسطة App Distribution عندما ينشر App Distribution تنبيهًا.
يمكنك الاطّلاع على عوامل تشغيل تنبيهات Firebase في مستندات Cloud Functions للحصول على معلومات حول كتابة الدوال التي يتم تشغيلها من خلال التنبيهات.
تعريف الدالة (الجيل الثاني فقط)
import {
onNewTesterIosDevicePublished,
onInAppFeedbackPublished
} from "firebase-functions/v2/alerts/appDistribution";
export const yourFunctionName = onNewTesterIosDevicePublished((event) => {
// ...
});
export const yourFunctionName2 = onInAppFeedbackPublished((event) => {
// ...
});
تعريف الموارد (extension.yaml)
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.newTesterIosDeviceappDistribution.inAppFeedback
عوامل تشغيل الأحداث المخصّصة (Eventarc)
يتم تشغيل دالة Eventarc عندما يتم نشر نوع حدث معيّن على قناة معيّنة.
يمكنك الاطّلاع على مقالة إنشاء عوامل تشغيل الأحداث المخصّصة والتعامل معها في مستندات Cloud Functions للحصول على معلومات حول كتابة الدوال التي يتم تشغيلها من خلال Eventarc.
يمكنك أيضًا نشر أحداث من إضافاتك لمنح المستخدمين طريقة لإدراج منطق مخصّص في إضافتك. يُرجى الاطّلاع على مقالة استخدام المنطق المخصّص الذي يوفّره المطوّر في إضافة.
تعريف الدالة (الجيل الثاني فقط)
import { onCustomEventPublished } from "firebase-functions/v2/eventarc";
export const yourFunctionName = onCustomEventPublished((event) => {
// ...
});
تعريف الموارد (extension.yaml)
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}