-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c8b2ad
commit e86c614
Showing
4 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package fcm | ||
|
||
import "encoding/base64" | ||
|
||
func DecodeFirebaseAuthKey(encoded string) ([]byte, error) { | ||
decodedKey, err := base64.StdEncoding.DecodeString(encoded) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return decodedKey, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package fcm | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
firebase "firebase.google.com/go" | ||
"firebase.google.com/go/messaging" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
type FCM struct { | ||
App *firebase.App | ||
Client *messaging.Client | ||
} | ||
|
||
func NewFCM(firebaseAuthKey string) (*FCM, error) { | ||
decodedKey, err := DecodeFirebaseAuthKey(firebaseAuthKey) | ||
if err != nil { | ||
return &FCM{}, err | ||
} | ||
|
||
opts := []option.ClientOption{option.WithCredentialsJSON(decodedKey)} | ||
app, err := firebase.NewApp(context.Background(), nil, opts...) | ||
if err != nil { | ||
return &FCM{}, err | ||
} | ||
|
||
fcmClient, err := app.Messaging(context.Background()) | ||
if err != nil { | ||
return &FCM{}, err | ||
} | ||
|
||
return &FCM{ | ||
App: app, | ||
Client: fcmClient, | ||
}, nil | ||
} | ||
|
||
func (f *FCM) SendTopicCtx(ctx context.Context, topic string, message *messaging.Message) error { | ||
if topic == "" { | ||
return errors.New("topic is required") | ||
} | ||
|
||
message.Topic = topic | ||
_, err := f.Client.Send(ctx, message) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f *FCM) SendTopic(topic string, message *messaging.Message) error { | ||
if topic == "" { | ||
return errors.New("topic is required") | ||
} | ||
|
||
message.Topic = topic | ||
_, err := f.Client.Send(context.Background(), message) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,44 @@ | ||
module github.com/awang-jakpat/golds-dev-tools | ||
|
||
go 1.21.5 | ||
|
||
require ( | ||
cloud.google.com/go v0.110.10 // indirect | ||
cloud.google.com/go/compute v1.23.3 // indirect | ||
cloud.google.com/go/compute/metadata v0.2.3 // indirect | ||
cloud.google.com/go/firestore v1.14.0 // indirect | ||
cloud.google.com/go/iam v1.1.5 // indirect | ||
cloud.google.com/go/longrunning v0.5.4 // indirect | ||
cloud.google.com/go/storage v1.36.0 // indirect | ||
firebase.google.com/go v3.13.0+incompatible // indirect | ||
github.com/felixge/httpsnoop v1.0.4 // indirect | ||
github.com/go-logr/logr v1.3.0 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/google/s2a-go v0.1.7 // indirect | ||
github.com/google/uuid v1.4.0 // indirect | ||
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect | ||
github.com/googleapis/gax-go/v2 v2.12.0 // indirect | ||
go.opencensus.io v0.24.0 // indirect | ||
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect | ||
go.opentelemetry.io/otel v1.21.0 // indirect | ||
go.opentelemetry.io/otel/metric v1.21.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.21.0 // indirect | ||
golang.org/x/crypto v0.16.0 // indirect | ||
golang.org/x/net v0.19.0 // indirect | ||
golang.org/x/oauth2 v0.15.0 // indirect | ||
golang.org/x/sync v0.5.0 // indirect | ||
golang.org/x/sys v0.15.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
golang.org/x/time v0.5.0 // indirect | ||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect | ||
google.golang.org/api v0.154.0 // indirect | ||
google.golang.org/appengine v1.6.8 // indirect | ||
google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f // indirect | ||
google.golang.org/genproto/googleapis/api v0.0.0-20231120223509-83a465c0220f // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect | ||
google.golang.org/grpc v1.59.0 // indirect | ||
google.golang.org/protobuf v1.31.0 // indirect | ||
) |
Oops, something went wrong.