Cloud Functions で可能な処理
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Cloud Functions によって、デベロッパーは Firebase および Google Cloud イベントにアクセスでき、それらのイベントに対応してコードを実行するスケーラブルな処理能力を得ることができます。Firebase アプリでは、独自の要件を満たすために独自の方法で Cloud Functions を使用することが想定されますが、典型的な使用例は次の領域に分類されます。
興味のあるカテゴリの使用例とサンプルを確認してから、はじめにチュートリアル、または認証イベントやアナリティクス イベントなどの入門ガイドに進みます。
関心事が発生したときにユーザーに通知する
デベロッパーは Cloud Functions を使用して、ユーザーが常にアプリに関する情報に関心を持ち、最新情報に接するようにすることができます。たとえば、ユーザーがアプリ内でのお互いの活動をフォローできるアプリを考えてみましょう。ユーザーが自分を別のユーザーのフォロワーとして追加するたびに、Realtime Database に書き込みが行われます。この書き込みイベントによって関数がトリガーされ、適切なユーザーに新しいフォロワーの獲得を知らせる Firebase Cloud Messaging(FCM)通知が作成されます。

- 関数は、フォロワーが保存される Realtime Database パスへの書き込みでトリガーされます。
- 関数によって、FCM を介して送信するメッセージが作成されます。
- FCM によって、通知メッセージがユーザーのデバイスに送信されます。
実際のコードを確認するには、GitHub のサンプルコードをご覧ください。
関心事通知のその他の使用例
- ニュースレターを登録または登録解除したユーザーに確認メールを送信します。
- ユーザーがサインアップを完了したときに登録完了の通知メールを送信します。
- ユーザーが新しいアカウントを作成したときに SMS 確認を送信します。
データベースのサニタイズとメンテナンスを実行する
Cloud Functions のデータベース イベント処理を使用すると、ユーザーの動作に応じて Realtime Database または Cloud Firestore を変更し、システムを目的の状態に保つことができます。たとえば、書き込みイベントをモニタリングし、ユーザーのメッセージ内の特定の文字列の形式を変更(大文字に変更するなど)できます。
その仕組みは次のとおりです。

- 関数のデータベース イベント ハンドラによって、特定のパス上の書き込みイベントがリッスンされ、メッセージのテキストを含むイベントデータが取得されます。
- 関数によってテキストが処理され、文字列が大文字に変換されます。
- 更新されたテキストが関数によってデータベースに書き戻されます。
実際のコードを確認するには、GitHub のサンプルコードをご覧ください。
データベースのサニタイズとメンテナンスのその他の使用例
- 削除されたユーザーのコンテンツを Realtime Database から削除します。
- Firebase データベース内の子ノードの数を制限します。
- Realtime Database リスト内の要素数を追跡します。
- Realtime Database から Google Cloud BigQuery にデータをコピーします。
- テキストを絵文字に変換します。
- データベース レコードの計算されたメタデータを管理します。
アプリではなくクラウドで集中的なタスクを実行する
Cloud Functions を利用して、リソースを大量に必要とする作業(負荷が大きい CPU やネットワーキング)をユーザーのデバイスで実行するのではなく、Google Cloud にオフロードすることで、アプリの応答性を向上させることができます。たとえば、Cloud Storage への画像アップロードをリッスンする関数を記述し、関数を実行するインスタンスに画像をダウンロードし、変更し、Cloud Storage にアップロードして戻すことができます。変更には、sharp や Pillow などのツールによる画像のサイズ変更、切り抜き、変換が含まれます。

- 画像ファイルが Cloud Storage にアップロードされると、関数がトリガーされます。
- ファンクションによって画像がダウンロードされ、画像のサムネイル バージョンが作成されます。
- ファンクションによってそのサムネイルの場所がデータベースに書き込まれるため、クライアント アプリはそれを見つけて使用できます。
- 関数によってサムネイルは Cloud Storage の新しい場所にアップロードされます。
- アプリはサムネイル リンクをダウンロードします。
画像処理の具体例のチュートリアルについては、Cloud Storage イベントの処理のガイドをご覧ください。
Firebase Cloud でのバッチジョブのその他の例
- 未使用の Firebase アカウントを定期的に削除します(Node.js | Python)。
- アップロードされた画像を自動的にバックアップします(Node.js | Python)。
- 一括メールをユーザーに送信します。
- データを定期的に集計して整理します。
- 保留中の作業のキューを処理します。
サードパーティのサービスおよび API との統合
Cloud Functions は、ウェブ API の呼び出しや公開によってアプリと他のサービスとの連携を強化するのに役立ちます。たとえば、開発時の共同作業に使用されるアプリは、GitHub の commit をワークグループのチャットルームに投稿できます。

- ユーザーが GitHub リポジトリに commit を push します。
- HTTPS 関数が GitHub webhook API を介してトリガーされます。
- 関数によって commit の通知がチームの Slack チャンネルに送信されます。
サードパーティのサービスおよび API と統合するためのその他の方法
- Google Cloud Vision API を使用して、アップロードした画像を分析およびタグ付けします。
- Google 翻訳を使用してメッセージを翻訳します。
- カスタム認証を使用してユーザーをログインします。
- Realtime Database 書き込みで Webhook にリクエストを送信します。
- Realtime Database 要素に対する全文検索を有効にします。
- ユーザーからの支払いを処理します。
- 電話や SMS メッセージに対する自動応答を作成します。
- Google アシスタントを使用して chatbot を作成します。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-17 UTC。
[null,null,["最終更新日 2025-08-17 UTC。"],[],[],null,["\u003cbr /\u003e\n\nCloud Functions gives developers access to Firebase and Google Cloud\nevents, along with\nscalable computing power to run code in response to those events. While it's\nexpected that Firebase apps will use Cloud Functions in unique ways to meet\ntheir unique requirements, typical use cases might fall into these areas:\n\n- [Notify users when something interesting happens](#notify_users_when_something_interesting_happens).\n- [Perform database sanitization and maintenance](#perform_database_sanitization_and_maintenance).\n- [Execute intensive tasks in the cloud instead of in your app](#execute_intensive_tasks_in_the_cloud_instead_of_in_your_app).\n- [Integrate with third-party services and APIs](#integrate_with_third-party_services_and_apis).\n\nReview the use cases and examples for each category that interests\nyou, and then proceed to our [Get Started](/docs/functions/get-started) tutorial\nor to specific how-to guides\nfor [authentication events](/docs/functions/auth-events),\n[analytics events](/docs/functions/analytics-events), and more.\n\nNotify users when something interesting happens\n\nDevelopers can use Cloud Functions to keep users engaged and up to date\nwith relevant information about an app. Consider, for example, an app that\nallows users to follow one another's activities in the app. Each time a user\nadds themselves as a follower of another user, a write occurs in the\nRealtime Database. Then this write event could trigger a function\nto create Firebase Cloud Messaging (FCM) notifications to let the appropriate\nusers know that they have gained new followers.\n\n1. The function triggers on writes to the Realtime Database path where followers are stored.\n2. The function composes a message to send via .\n3. sends the notification message to the user's device.\n\nTo review working code, see the sample code in GitHub:\n\n- Node.js: [fcm-notifications](https://github.com/firebase/functions-samples/tree/main/Node/fcm-notifications)\n- Python: [fcm-notifications](https://github.com/firebase/functions-samples/tree/main/Python/fcm-notifications)\n\nOther interesting notification use cases\n\n- Send confirmation emails to users subscribing to a newsletter.\n- Send a welcome email when a user completes signup.\n- Send an SMS confirmation when a user creates a new account.\n\nPerform database sanitization and maintenance\n\nWith Cloud Functions database event handling, you can modify Realtime Database or\nCloud Firestore in response to user behavior, keeping the system in your desired\nstate. For example, you could monitor write events and change the format\n(for example, change to all uppercase) of certain strings in users' messages.\nHere's how that could work:\n\n1. The function's database event handler listens for write events on a specific path, and retrieves event data containing the text of a messages.\n2. The function processes the text to change strings to uppercase.\n3. The function writes the updated text back to the database.\n\nTo review working code, see the sample code in GitHub:\n\n- Node.js: [uppercase-rtdb](https://github.com/firebase/functions-samples/tree/main/Node/quickstarts/uppercase-rtdb)\n- Python: [uppercase-rtdb](https://github.com/firebase/functions-samples/tree/main/Python/quickstarts/uppercase-rtdb)\n\nOther database sanitization and maintenance use cases\n\n- Purge a deleted user's content from Realtime Database.\n- Limit the number of child nodes in a Firebase database.\n- Track the number of elements in a Realtime Database list.\n- Copy data from Realtime Database to Google Cloud BigQuery.\n- Convert text to emoji.\n- Manage computed metadata for database records.\n\nExecute intensive tasks in the cloud instead of in your app\n\nYou can take advantage of Cloud Functions to offload to the Google\ncloud resource-intensive work (heavy CPU or networking) instead of running it\non a user's device, improving the responsiveness of your app.\nFor instance, you could write a function\nto listen for image uploads to Cloud Storage, download the image to the instance\nrunning the function, modify it, and upload it back to Cloud Storage. Your\nmodifications could include resizing, cropping, or converting images with tools\nlike [sharp](https://www.npmjs.com/package/sharp) or\n[Pillow](https://pillow.readthedocs.io/en/stable/).\n\n1. A function triggers when an image file is uploaded to Cloud Storage.\n2. The function downloads the image and creates a thumbnail version of it.\n3. The function writes that thumbnail location to the database, so a client app can find and use it.\n4. The function uploads the thumbnail back to Cloud Storage in a new location.\n5. The app downloads the thumbnail link.\n\nFor a walkthrough of an image processing example, see the guide to\n[handling Cloud Storage events](https://firebase.google.com/docs/functions/gcp-storage-events?gen=2nd).\n\nOther examples of batch jobs in the Firebase cloud\n\n- Periodically delete unused Firebase accounts [Node.js](https://github.com/firebase/functions-samples/tree/main/Node/delete-unused-accounts-cron) \\| [Python](https://github.com/firebase/functions-samples/tree/main/Python/delete-unused-accounts-cron).\n- Automatically back up uploaded images [Node.js](https://github.com/firebase/functions-samples/tree/main/Node/taskqueues-backup-images) \\| [Python](https://github.com/firebase/functions-samples/tree/main/Python/taskqueues-backup-images).\n- Send bulk email to users.\n- Aggregate and summarize data periodically.\n- Process a queue of pending work.\n\nIntegrate with third-party services and APIs\n\nCloud Functions can help your app work better with other services by\ncalling and exposing web APIs. For instance, an app used for collaboration on\ndevelopment could post GitHub commits to a workgroup chat room.\n\n1. A user pushes commits to a GitHub repo.\n2. An HTTPS function triggers via the [GitHub webhook API](https://developer.github.com/webhooks/).\n3. The function sends a notification of the commit to a team Slack channel.\n\nOther ways to integrate with third-party services and APIs\n\n- Use Google [Cloud Vision API](https://cloud.google.com/vision/) to analyze and tag uploaded images.\n- Translate messages using Google Translate.\n- Use [custom auth](https://firebase.google.com/docs/auth/where-to-start#custom-auth) to sign in users.\n- Send a request to a webhook on Realtime Database writes.\n- Enable full-text search on Realtime Database elements.\n- Process payments from users.\n- Create auto-responses to phone calls and SMS messages.\n- Create a chatbot using Google Assistant."]]