From f5fd97abeb8c2666c69e6518713af38fbdffd68a Mon Sep 17 00:00:00 2001 From: Carlo Bortolan <106114526+carlobortolan@users.noreply.github.com> Date: Tue, 16 Jan 2024 23:45:02 +0100 Subject: [PATCH] Update worker_grpc.go --- api/worker_grpc.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/api/worker_grpc.go b/api/worker_grpc.go index 0487aeae0..7b90f03a4 100644 --- a/api/worker_grpc.go +++ b/api/worker_grpc.go @@ -645,6 +645,44 @@ func (s server) NotifyStreamStarted(ctx context.Context, request *pb.StreamStart NotifyLiveUpdateCourseWentLive(stream.Model.ID) }() + // Send push notifications to users enrolled in stream's course and subscribed to push notifications + go func() { + if s.FcmClient != nil { + deviceTokens, err := s.CoursesDao.GetSubscribedDevices(stream.ID) + if err != nil { + logger.Error("Get subscribed devices:", "err", err) + } else { + logger.Info(fmt.Sprintf("Start sending push notifications to devices: %d", len(deviceTokens))) + + // Create a new notification payload + notification := &fcm.NotificationPayload{ + Title: fmt.Sprintf("%s is currently live!", course.Slug), + Body: fmt.Sprintf("%s %s", stream.Name, stream.Description), + } + + // Create a new data payload + data := map[string]interface{}{ + // Add any key-value pairs that you want to send along with the notification + } + + // Create a new FCM message + msg := s.FcmClient.NewFcmRegIdsMsg(deviceTokens, data) + + // Set the notification payload of the message + msg.SetNotificationPayload(notification) + + // Send the message + status, err := s.FcmClient.Send() + if err != nil { + logger.Error("Error sending push notification", "err", err) + return + } + + logger.Info("Sent push notifications to devices: ", "status", fmt.Sprintf("%v", status)) + } + } + }() + return &pb.Status{Ok: true}, nil }