Skip to content

Notifications #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ __pycache__/
.env
.envrc
.DS_Store
ca-certificate.crt
ca-certificate.crt
firebase_serviceAccountKey.json
3 changes: 2 additions & 1 deletion src/mutations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .create_game import CreateGame
from .create_team import CreateTeam
from .create_youtube_video import CreateYoutubeVideo
from .create_youtube_video import CreateYoutubeVideo
from .create_notifications import CreateNotification
23 changes: 23 additions & 0 deletions src/mutations/create_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import graphene
from graphene import Mutation
from src.services.notification_service import NotificationService

class CreateNotification(Mutation):
class Arguments:
tokens = graphene.List(graphene.String, required=True)
title = graphene.String(required=True)
body = graphene.String(required=True)

success = graphene.Boolean()
message = graphene.String()

@staticmethod
def mutate(root, info, tokens, title, body):
try:
response = NotificationService.send_notification(tokens, title, body)
return CreateNotification(
success=True,
message=f"Notification sent to {response.success_count} device(s)."
)
except Exception as e:
return CreateNotification(success=False, message=f"Error: {str(e)}")
3 changes: 2 additions & 1 deletion src/schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from graphene import ObjectType, Schema, Mutation
from src.mutations import CreateGame, CreateTeam, CreateYoutubeVideo
from src.mutations import CreateGame, CreateTeam, CreateYoutubeVideo, CreateNotification
from src.queries import GameQuery, TeamQuery, YoutubeVideoQuery


Expand All @@ -11,6 +11,7 @@ class Mutation(ObjectType):
create_game = CreateGame.Field(description="Creates a new game.")
create_team = CreateTeam.Field(description="Creates a new team.")
create_youtube_video = CreateYoutubeVideo.Field(description="Creates a new youtube video.")
create_notification = CreateNotification.Field(description="Sends a notification.")


schema = Schema(query=Query, mutation=Mutation)
3 changes: 2 additions & 1 deletion src/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .game_service import GameService
from .team_service import TeamService
from .youtube_video_service import YoutubeVideoService
from .youtube_video_service import YoutubeVideoService
from .notification_service import NotificationService
34 changes: 34 additions & 0 deletions src/services/notification_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import firebase_admin
from firebase_admin import credentials, messaging

class NotificationService:

base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..',))
service_account_path = os.path.join(base_dir, 'firebase_serviceAccountKey.json')

if not os.path.exists(service_account_path):
raise FileNotFoundError(
f"Firebase service account key not found at {service_account_path}. "
"Please ensure it exists in the project root folder and is excluded from version control."
)

if not firebase_admin._apps:
cred = credentials.Certificate(service_account_path)
firebase_admin.initialize_app(cred)

def send_notification (tokens: list, title: str, body: str):
"""
Sends a notification to multiple device tokens.
Returns a response object with success_count and failure_count.
"""
message = messaging.MulticastMessage(
notification=messaging.Notification(
title=title,
body=body
),
tokens=tokens,
)
return messaging.send_each_for_multicast(message)