6 changed files with 181 additions and 6 deletions
-
5apps/account/serializers/user.py
-
101apps/account/tasks.py
-
4apps/account/urls.py
-
56apps/account/views/notification.py
-
19apps/account/views/user.py
-
2requirements.txt
@ -1,9 +1,108 @@ |
|||
import time |
|||
from config.settings import base as settings |
|||
|
|||
from celery import shared_task |
|||
import requests |
|||
import json |
|||
import logging |
|||
import firebase_admin |
|||
from firebase_admin import credentials, messaging |
|||
import firebase_admin |
|||
from firebase_admin import credentials, messaging |
|||
from google.oauth2 import service_account |
|||
import google.auth.transport.requests |
|||
|
|||
|
|||
|
|||
logging.basicConfig( |
|||
level=logging.INFO, |
|||
format='%(asctime)s - %(levelname)s - %(message)s' |
|||
) |
|||
logger = logging.getLogger(__name__) |
|||
|
|||
# تنظیمات Firebase |
|||
data = { |
|||
"type": "service_account", |
|||
"project_id": "imamjavad-25c31", |
|||
"private_key_id": "1edc90fb80a335809c4b04a713403355ff4e8bd0", |
|||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEugIBADANBgkqhkiG9w0BAQEFAASCBKQwggSgAgEAAoIBAQCM57lia6vNNzJL\nYBwFcx49sFPXtrYKkrRhtDN13EOnF2j+y8vlwtqYR6P7HB1l10GyHx3mlN8XpYXN\n24yrTsK6WugqPdWGl/z/BgqHximH+v4NCPQTBr3lemHbTXlEkhNtmaf9zM8IzsXP\nEzD4z5u9hy3AgfZdHKO0isTvxRTuTlpTKU3PQDwiIjfb6Bk8IjfjrDQqWbHLC4am\nidwM5F+L8ecAyhVe/G7IXAflqyi4zVM/hM5FiAknA5FmyfGd9HCxaLkw3Dqtnof8\nqflJmZp0vptT3Lte7ObeUEMoRoT0bZt6DbMBI+w19OIBu0ne0OjLu/1z4CFYoR+r\nAeWGvWCHAgMBAAECgf9HnQx/FY90oO5gtiLdE/pnJxqSMtjEhufRazaDd4vOYKXD\nhLQ5EkFcsij66PnPHiZiC+BfbUpnSIAqrmsliXBSYv4OCELTJU/FovcMfHG7qtU7\nIBjsrw64ISXT+ow1+EE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ej9JCc7WJYnnN75M\n5TGjxvmxYqR231/L/p0=\n-----END PRIVATE KEY-----\n", |
|||
"client_email": "[email protected]", |
|||
"client_id": "103207313184637638669", |
|||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", |
|||
"token_uri": "https://oauth2.googleapis.com/token", |
|||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", |
|||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-fbsvc%40imamjavad-25c31.iam.gserviceaccount.com", |
|||
"universe_domain": "googleapis.com" |
|||
} |
|||
|
|||
PROJECT_ID = 'imamjavad-25c31' |
|||
BASE_URL = 'https://fcm.googleapis.com' |
|||
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging'] |
|||
FCM_ENDPOINT = f'v1/projects/{PROJECT_ID}/messages:send' |
|||
FCM_URL = f'{BASE_URL}/{FCM_ENDPOINT}' |
|||
|
|||
def _get_access_token(): |
|||
"""Retrieve a valid access token that can be used to authorize requests. |
|||
|
|||
:return: Access token. |
|||
""" |
|||
credentials = service_account.Credentials.from_service_account_info( |
|||
data, scopes=SCOPES) |
|||
request = google.auth.transport.requests.Request() |
|||
credentials.refresh(request) |
|||
return credentials.token |
|||
|
|||
|
|||
# @shared_task |
|||
async def send_notification(ids: list, title: str = None, body: str = None, data=None, |
|||
extra_notification_kwargs: dict = None) -> list: |
|||
if not ids: |
|||
return [] |
|||
|
|||
chunked_ids = [ids[i:i + 500] for i in range(0, len(ids), 500)] |
|||
|
|||
responses = [] |
|||
for chunk in chunked_ids: |
|||
|
|||
|
|||
access_token = _get_access_token() |
|||
headers = { |
|||
'Authorization': f'Bearer {access_token}', |
|||
'Content-Type': 'application/json', |
|||
} |
|||
payload = { |
|||
'message': { |
|||
'token': chunk[0], |
|||
'notification': { |
|||
'title': title, |
|||
'body': body, |
|||
}, |
|||
'data': {k: str(v) for k, v in (data or {}).items()}, |
|||
'android': { |
|||
'priority': 'high', |
|||
'notification': { |
|||
'title': title, |
|||
'body': body, |
|||
# 'sound': 'incoming_call_sound', |
|||
'color': '#06EEBD', |
|||
# 'channel_id': 'incoming_call_channel', |
|||
'visibility': 'public', |
|||
}, |
|||
}, |
|||
} |
|||
} |
|||
# Send the POST request to FCM API |
|||
print(f'=========(send-notif)===******') |
|||
response = requests.post(FCM_URL, headers=headers, json=payload) |
|||
if response.status_code == 200: |
|||
logger.warning('Successfully sent message:', response.json()) |
|||
responses.append(response.json()) |
|||
else: |
|||
responses.append({'status': 'error', 'message': ""}) |
|||
logger.error(f'Failed to send message notif') |
|||
|
|||
return responses |
|||
|
|||
|
|||
|
|||
@shared_task |
|||
def send_otp_code(phone_number, code): |
|||
|
|||
@ -117,6 +117,8 @@ webencodings==0.5.1 |
|||
whitenoise==6.9.0 |
|||
wrapt==1.16.0 |
|||
wsproto==1.2.0 |
|||
firebase-admin==6.2.0 |
|||
google-auth==2.6.0 |
|||
|
|||
https://yaghoubi:[email protected]/NewHorizon/django-limitless-dashboard.git/archive/master.zip |
|||
https://yaghoubi:[email protected]/NewHorizon/ajax-datatable.git/archive/master.zip |
|||
|
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue