-
-
Notifications
You must be signed in to change notification settings - Fork 789
/
Copy pathmain.go
50 lines (47 loc) · 1.04 KB
/
main.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
package main
import (
"context"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/messaging"
"flag"
"fmt"
"google.golang.org/api/option"
"os"
"strings"
)
func main() {
conffile := flag.String("config", "/etc/fbsend/fbsend.json", "config file")
flag.Parse()
if flag.NArg() < 2 {
fail("Syntax: fbsend [-config FILE] topic key=value ...")
}
topic := flag.Arg(0)
data := make(map[string]string)
for i := 1; i < flag.NArg(); i++ {
kv := strings.SplitN(flag.Arg(i), "=", 2)
if len(kv) != 2 {
fail(fmt.Sprintf("Invalid argument: %s (%v)", flag.Arg(i), kv))
}
data[kv[0]] = kv[1]
}
fb, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsFile(*conffile))
if err != nil {
fail(err.Error())
}
msg, err := fb.Messaging(context.Background())
if err != nil {
fail(err.Error())
}
_, err = msg.Send(context.Background(), &messaging.Message{
Topic: topic,
Data: data,
})
if err != nil {
fail(err.Error())
}
fmt.Println("Sent successfully")
}
func fail(s string) {
fmt.Println(s)
os.Exit(1)
}