> ## Documentation Index
> Fetch the complete documentation index at: https://docs.auxia.io/llms.txt
> Use this file to discover all available pages before exploring further.

# プッシュ通知サンプルスクリプト

> Firebaseプッシュ通知を実装するためのサンプルスクリプト

> [!NOTE]
>
> 日本語版はAIによる翻訳です。正確な情報については[英語版](https://docs.auxia.io/deploying-auxia/delivery-integrations/firebase-push-notifications-integration-manual/sample-push-notifications-script.md)をご参照ください。

## 前提条件

1. FCMトークン：[Flutterサンプルセットアップ](https://docs.auxia.io/ja/deploying-auxia/delivery-integrations/firebase-push-notifications-integration-manual/sample-flutter-set-up.md)セクションで、開発者のデバイスのFCMトークンをログに出力する実装を紹介しています。
2. APIへのアクセス：[サービスアカウント](https://docs.auxia.io/ja/deploying-auxia/delivery-integrations/firebase-push-notifications-integration-manual/enable-auxia-to-send-notification.md#service-account)セクションの手順で、APIへのアクセス権を持つサービスアカウントのJSONキーをダウンロード済みであること。

[完全な実装はこちらをご参照ください](#complete-implementation)。

> [!TIP]
>
> 通知の受信とイベントログのデバッグには、[ローカルデバッガー](https://firebase.google.com/docs/analytics/debugview)を使用できます。
>
> ![Firebase Analytics Debug View](https://mintcdn.com/auxia/XsfpA_ObWj7oESRX/img/firebase-push-notifications/debug-view.png?fit=max\&auto=format\&n=XsfpA_ObWj7oESRX\&q=85\&s=5b2b02924d09ce637611ac55e0b54a29)

## Pythonによるプッシュ通知送信スクリプトの例

次のPythonスクリプトは、Firebase Cloud Messagingを使用してプッシュ通知を送信する例です。

```python theme={null}
# sender.py
# Refer to the Service Account section for the JSON key file creation.
# Refer to the Sample Flutter Setup section for FCM token creation.

import time
import firebase_admin
from firebase_admin import messaging, credentials

# Firebase service account JSON file (Replace with the correct path)
SERVICE_ACCOUNT_PATH = "path/to/file"

# Firebase Cloud Messaging (FCM) token of the target device (Replace with actual token)
FCM_TOKEN = "Get this token from the client device set up."

# Initialize Firebase Admin SDK if not already initialized
if not firebase_admin._apps:
    cred = credentials.Certificate(SERVICE_ACCOUNT_PATH)
    firebase_admin.initialize_app(cred)

# Create a notification message
message = messaging.Message(
    notification=messaging.Notification(
        title="Sample Title",
        body="Sample Body."
    ),
    data={"timestamp": str(time.time())},
    token=FCM_TOKEN,  # Target device token
)

# Send the notification
try:
    response = messaging.send(message)
    print("Successfully sent message:", response)
except Exception as e:
    print("Failed to send message:", str(e))
```

## 完全な実装

Flutterの完全な実装については、[Flutterサンプルセットアップ](https://docs.auxia.io/ja/deploying-auxia/delivery-integrations/firebase-push-notifications-integration-manual/sample-flutter-set-up.md)をご参照ください。モバイルアプリケーションでプッシュ通知を受信・処理するために必要なコードがすべて含まれています。

## デバッグ

通知の受信とイベントログのデバッグには、ローカルデバッガーを使用できます。FirebaseコンソールのDebugViewでは、イベントの発生をリアルタイムで確認できます。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.