diff --git a/net/repeater/internal/api_notify.go b/net/repeater/internal/api_notify.go index a37797ff..ff1f0f67 100644 --- a/net/repeater/internal/api_notify.go +++ b/net/repeater/internal/api_notify.go @@ -3,13 +3,11 @@ package repeater import ( "encoding/json" "log" - "context" "net/http" "os" "runtime" "strings" "fmt" - firebase "firebase.google.com/go/v4" "firebase.google.com/go/v4/messaging" ) @@ -34,16 +32,6 @@ func WriteResponse(w http.ResponseWriter, v interface{}) { //Notify proxies push notification to device func Notify(w http.ResponseWriter, r *http.Request) { - app, err := firebase.NewApp(context.Background(), nil) - if err != nil { - log.Fatalf("error initializing app: %v\n", err) - } - - ctx := context.Background() - client, err := app.Messaging(ctx) - if err != nil { - log.Fatalf("error getting Messaging client: %v\n", err) - } var msg PushMessage if err := ParseRequest(r, w, &msg); err != nil { @@ -60,9 +48,10 @@ func Notify(w http.ResponseWriter, r *http.Request) { // Send a message to the device corresponding to the provided // registration token. - response, err := client.Send(ctx, message) + response, err := FCMClient.Send(FCMContext, message) if err != nil { - log.Fatalln(err) + ErrResponse(w, http.StatusBadRequest, err) + return } // Response is a message ID string. fmt.Println("Successfully sent message:", response) diff --git a/net/repeater/internal/routers.go b/net/repeater/internal/routers.go index 5b8ebbab..50b422f4 100644 --- a/net/repeater/internal/routers.go +++ b/net/repeater/internal/routers.go @@ -1,9 +1,13 @@ package repeater import ( + "firebase.google.com/go/v4/messaging" + firebase "firebase.google.com/go/v4" "github.com/gorilla/mux" "net/http" "strings" + "context" + "log" ) type route struct { @@ -15,9 +19,25 @@ type route struct { type routes []route +var FCMClient *messaging.Client +var FCMContext context.Context + //NewRouter allocate router for databag API func NewRouter() *mux.Router { + app, err := firebase.NewApp(context.Background(), nil) + if err != nil { + log.Fatalf("error initializing app: %v\n", err) + } + + ctx := context.Background() + client, err := app.Messaging(ctx) + if err != nil { + log.Fatalf("error getting Messaging client: %v\n", err) + } + FCMClient = client + FCMContext = ctx + router := mux.NewRouter().StrictSlash(true) for _, route := range endpoints { var handler http.Handler