-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
62 lines (58 loc) · 1.45 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package controller
import (
"context"
"firebase.google.com/go/messaging"
"github.com/shoriwe/message-api/models"
"github.com/shoriwe/message-api/session"
)
func (c *Controller) SendNotificationWithFirebase(recipients []string, msg *models.Message) error {
bResponse, sErr := c.Client.SendMulticast(
context.Background(),
&messaging.MulticastMessage{
Tokens: recipients,
Notification: &messaging.Notification{
Title: msg.Title,
Body: msg.Body,
},
})
if sErr != nil {
return sErr
}
results := make([]models.MessageResponse, 0, len(bResponse.Responses))
for _, response := range bResponse.Responses {
var errS string
if response.Error != nil {
errS = response.Error.Error()
}
results = append(results, models.MessageResponse{
MessageUUID: msg.UUID,
Success: response.Success,
FirebaseId: response.MessageID,
Error: errS,
})
}
return c.DB.Create(results).Error
}
func (c *Controller) SendMessage(s *session.Session, msg *models.Message) error {
m := &models.Message{
SenderUUID: s.UserUUID,
RecipientUUID: msg.RecipientUUID,
Title: msg.Title,
Body: msg.Body,
}
cErr := c.DB.Create(m).Error
if cErr != nil {
return cErr
}
var recipients []string
fErr := c.DB.
Model(&models.Device{}).
Select("firebase_token").
Where("user_uuid = ?", msg.RecipientUUID).
Find(&recipients).
Error
if fErr != nil {
return fErr
}
return c.SendNotificationWithFirebase(recipients, m)
}