Skip to content

Commit

Permalink
Allow overriding credentials file via setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdinand Hoffmann committed Feb 26, 2024
1 parent 480b964 commit 44ca252
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ service account JSON file:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
```

Alternatively you can use the django setting `FCM_CREDENTIALS_FILE` to provide the path.

#### To generate a private key file for your service account:

1. In the Firebase console, open **Settings** > [Service Accounts](https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk).
Expand Down
11 changes: 10 additions & 1 deletion firebase_push/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import firebase_admin
from celery import shared_task
from django.conf import settings
from firebase_admin import credentials
from requests import HTTPError, Timeout

from firebase_push.models import FCMHistoryBase
Expand All @@ -11,7 +13,14 @@
FCMDevice = get_device_model()

FCM_RETRY_EXCEPTIONS = (HTTPError, Timeout)
firebase = firebase_admin.initialize_app()


if credentials_file := getattr(settings, "FCM_CREDENTIALS_FILE", None):
credential = credentials.Certificate(credentials_file)
else:
credential = None

firebase = firebase_admin.initialize_app(credential=credential)


@shared_task(autoretry_for=FCM_RETRY_EXCEPTIONS, retry_backoff=True)
Expand Down

0 comments on commit 44ca252

Please sign in to comment.