notification: add firebase notification to web
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
from core.apps.notifications.models import Notification
|
||||
from core.apps.notifications.utils.send_notification import send_notification
|
||||
from core.apps.notifications.utils.send_notification import send_notification, send_web_notification
|
||||
|
||||
|
||||
def notify_user(user, title, body, data):
|
||||
tokens = Notification.objects.filter(user=user)
|
||||
for token in tokens:
|
||||
send_notification(token.token, title, body, data)
|
||||
if token.type == 'mobile':
|
||||
send_notification(token.token, title, body, data)
|
||||
if token.type == 'web':
|
||||
send_web_notification(token.token, title, body, data)
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import requests
|
||||
from firebase_admin import messaging
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def send_notification(token, title, body, data=None):
|
||||
message = {
|
||||
@@ -11,6 +15,19 @@ def send_notification(token, title, body, data=None):
|
||||
response = requests.post(
|
||||
"https://exp.host/--/api/v2/push/send",
|
||||
json=message,
|
||||
headers={"Content-Type": "application/json"}
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
return response.json()
|
||||
return response.json()
|
||||
|
||||
|
||||
def send_web_notification(token, title, body, data=None):
|
||||
message = messaging.MulticastMessage(
|
||||
notification=messaging.Notification(
|
||||
title=title,
|
||||
body=body
|
||||
),
|
||||
data=data or {},
|
||||
tokens=token,
|
||||
)
|
||||
|
||||
response = messaging.send_multicast(message)
|
||||
Reference in New Issue
Block a user