/
google_msg.go
49 lines (42 loc) · 1.13 KB
/
google_msg.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
package SQLConn
import (
"context"
"fmt"
"log"
firebase "firebase.google.com/go"
"firebase.google.com/go/messaging"
"google.golang.org/api/option"
)
type SendGoogleMessage struct {
cli *messaging.Client
}
func (sgm *SendGoogleMessage) Init(filename string) error {
opt := option.WithCredentialsFile(filename)
config := &firebase.Config{ProjectID: "easypapertracker"}
app, err := firebase.NewApp(context.Background(), config, opt)
if err != nil {
return err
}
log.Print("google cloud message is ok ")
sgm.cli, err = app.Messaging(context.Background())
if err != nil {
return err
}
return nil
}
func (sgm *SendGoogleMessage) sendMessageToTopic(topicID string, topicStr string) (string, error) {
ret, err := sgm.cli.Send(context.Background(), &messaging.Message{
Topic: topicID,
Data: map[string]string{
"type": "update",
},
Notification: &messaging.Notification{
Title: fmt.Sprintf("%s has new content", topicStr),
Body: fmt.Sprintf("The topic you subscribe to - %s has new content and will be automatically updated with one click.", topicStr),
},
})
if err != nil {
return "", err
}
return ret, nil
}