-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase_message.go
More file actions
56 lines (46 loc) · 1.23 KB
/
firebase_message.go
File metadata and controls
56 lines (46 loc) · 1.23 KB
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
package main
import (
"context"
"fmt"
"log"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/messaging"
"google.golang.org/api/option"
)
var app *firebase.App
func initFirebase() error {
opt := option.WithCredentialsFile("languagebuddy-3a272-firebase-adminsdk-gtk54-4179a964ef.json")
var err error
app, err = firebase.NewApp(context.Background(), nil, opt)
if err != nil {
return fmt.Errorf("error initializing app: %v", err)
}
return nil
}
func sendNotificationToToken(token,title,body string) error {
// Obtain a messaging.Client from the App.
ctx := context.Background()
client, err := app.Messaging(ctx)
if err != nil {
log.Fatalf("error getting Messaging client: %v\n", err)
}
// This registration token comes from the client FCM SDKs.
// See documentation on defining a message payload.
message := &messaging.Message{
Notification: &messaging.Notification{
Title: title,
Body: body,
},
Token: token,
}
// Send a message to the device corresponding to the provided
// registration token.
response, err := client.Send(ctx, message)
if err != nil {
log.Println(err)
}else {
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)
}
return err
}