-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.go
78 lines (68 loc) · 1.98 KB
/
chat.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package push_notification
import (
"context"
"errors"
"firebase.google.com/go/messaging"
"log"
"strings"
)
const (
MESSAGE_CHAT = "MESSAGE_CHAT"
IMAGE = "IMAGE"
IMAGE_OFFLINE = "IMAGE_OFFLINE"
STICKER = "STICKER"
STICKER_OFFLINE = "STICKER_OFFLINE"
)
func MessageReciver(ctx context.Context, e FirestoreEvent) error {
if e.Value.Fields.IDTo.StringValue == "" {
return errors.New("SE REQUIERE UN ID MINIMO PARA ENVIAR NOTIFICACION")
}
type_ := e.Value.Fields.Type_.StringValue
switch type_ {
case IMAGE, IMAGE_OFFLINE:
if e.Value.Fields.Content.StringValue == "" {
e.Value.Fields.Content.StringValue = "📷 Te envió una imagen."
}
case STICKER, STICKER_OFFLINE:
e.Value.Fields.Content.StringValue = "🌚 Te envió un sticker."
}
if len(e.Value.Fields.Content.StringValue) >= 50 {
e.Value.Fields.Content.StringValue = e.Value.Fields.Content.StringValue[0:50]
}
fullPath := strings.Split(e.Value.Name, "/documents/")[1]
pathParts := strings.Split(fullPath, "/")
data := DataNotification{
UsersID: []string{
e.Value.Fields.IDTo.StringValue,
},
Message: &messaging.MulticastMessage{
Tokens: nil,
Data: map[string]string{
"type": MESSAGE_CHAT,
"sub_type": e.Value.Fields.Type_.StringValue,
"uid_contact": e.Value.Fields.IDFrom.StringValue,
"avatar": e.Value.Fields.AvatarFrom.StringValue,
"id_message": pathParts[3],
"body": e.Value.Fields.Content.StringValue,
"title": e.Value.Fields.NameFrom.StringValue,
"imageURL": e.Value.Fields.Image.StringValue,
},
//Notification: &messaging.Notification{
//Title: e.Value.Fields.NameFrom.StringValue,
//Body: e.Value.Fields.Content.StringValue,
//ImageURL: e.Value.Fields.Image.StringValue,
//},
},
}
err := SendPushNotificaction(ctx, &data)
if err != nil {
log.Println(err)
return err
}
//err = HmsSendPushNotificaction(ctx, &data)
//if err != nil {
// log.Println(err)
// return err
//}
return nil
}