diff --git a/.gitignore b/.gitignore index 7e14a5b..d25aaf9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ __pycache__/ .env .envrc .DS_Store -ca-certificate.crt \ No newline at end of file +ca-certificate.crt +firebase_serviceAccountKey.json \ No newline at end of file diff --git a/src/mutations/__init__.py b/src/mutations/__init__.py index 3fd3a8a..4342038 100644 --- a/src/mutations/__init__.py +++ b/src/mutations/__init__.py @@ -1,3 +1,4 @@ from .create_game import CreateGame from .create_team import CreateTeam -from .create_youtube_video import CreateYoutubeVideo \ No newline at end of file +from .create_youtube_video import CreateYoutubeVideo +from .create_notifications import CreateNotification \ No newline at end of file diff --git a/src/mutations/create_notifications.py b/src/mutations/create_notifications.py new file mode 100644 index 0000000..733cea6 --- /dev/null +++ b/src/mutations/create_notifications.py @@ -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)}") \ No newline at end of file diff --git a/src/schema.py b/src/schema.py index 2cbbe69..084b18d 100644 --- a/src/schema.py +++ b/src/schema.py @@ -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 @@ -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) diff --git a/src/services/__init__.py b/src/services/__init__.py index 2ed3e7a..df2a6e3 100644 --- a/src/services/__init__.py +++ b/src/services/__init__.py @@ -1,3 +1,4 @@ from .game_service import GameService from .team_service import TeamService -from .youtube_video_service import YoutubeVideoService \ No newline at end of file +from .youtube_video_service import YoutubeVideoService +from .notification_service import NotificationService \ No newline at end of file diff --git a/src/services/notification_service.py b/src/services/notification_service.py new file mode 100644 index 0000000..c3e379a --- /dev/null +++ b/src/services/notification_service.py @@ -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) + + \ No newline at end of file