-
Notifications
You must be signed in to change notification settings - Fork 3
/
fcm.go
73 lines (56 loc) · 1.81 KB
/
fcm.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
package main
import (
"fmt"
"log"
fcm "github.com/NaySoftware/go-fcm"
)
// GetInAlert is a function
func GetInAlert(routeID string, stationID string) {
tokens, err := getGetInUserTokens(routeID, stationID)
if err != nil {
log.Printf("%v\n", err)
}
title := "승차알림"
message := fmt.Sprintf("[%v] %v번 버스가 곧 도착합니다.", GetStationNameFromStationID(routeID, stationID), GetRouteNameFromRouteID(routeID))
fcmAlert(routeID, stationID, title, message, tokens)
}
func GetOutAlert(routeID string, stationID string, plateNo string) {
tokens, err := getGetOutUserTokens(routeID, stationID, plateNo)
if err != nil {
log.Printf("%v\n", err)
}
title := "하차알림"
message := fmt.Sprintf("%v번 버스가 곧 %v에 도착합니다.", GetRouteNameFromRouteID(routeID), GetStationNameFromStationID(routeID, stationID))
fcmAlert(routeID, stationID, title, message, tokens)
}
func GetInAlertUsingUUID(reserv Reserv) {
title := "승차알림"
message := fmt.Sprintf("[%v] %v번 버스가 곧 도착합니다.", GetStationNameFromStationID(reserv.RouteID, reserv.StationID), GetRouteNameFromRouteID(reserv.RouteID))
token, err := getTokenFromUUID(reserv.UUID)
if err != nil {
log.Printf("%v\n", err)
}
fcmAlert(reserv.RouteID, reserv.StationID, title, message, []string{token})
}
func fcmAlert(routeID string, stationID string, title string, message string, tokens []string) {
data := map[string]string{
"routeID": routeID,
"stationID": stationID,
"Title": "",
"Message": "",
}
ids := []string{}
for _, token := range tokens {
ids = append(ids, token)
}
xds := []string{}
data["Title"] = title
data["Message"] = message
c := fcm.NewFcmClient(config.ServerKey)
c.NewFcmRegIdsMsg(ids, data)
c.AppendDevices(xds)
_, err := c.Send()
if err != nil {
log.Printf("%v\n", err)
}
}