-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathfcm.go
36 lines (30 loc) · 917 Bytes
/
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
package fcm
import (
"context"
firebase "firebase.google.com/go"
"firebase.google.com/go/messaging"
log "github.com/pion/ion-log"
"google.golang.org/api/option"
)
func FCMPush(fcmCert string, token string, payload map[string]string) (string, error) {
opt := option.WithCredentialsFile(fcmCert)
app, err := firebase.NewApp(context.Background(), nil, opt)
// Obtain a messaging.Client from the App.
ctx := context.Background()
client, err := app.Messaging(ctx)
// See documentation on defining a message payload.
message := &messaging.Message{
Data: payload,
Token: token,
}
// Send a message to the device corresponding to the provided
// registration token.
response, err := client.Send(ctx, message)
if err != nil {
log.Errorf("FCMPush: err %v", err)
return "", err
}
// Response is a message ID string.
log.Infof("Successfully sent message: %v", response)
return response, err
}