mirror of
https://github.com/balzack/databag.git
synced 2025-03-13 00:50:03 +00:00
adding payload to notification endpoint
This commit is contained in:
parent
f2cc1d79c0
commit
bf1fed34b6
@ -13,6 +13,12 @@ import (
|
||||
"firebase.google.com/go/v4/messaging"
|
||||
)
|
||||
|
||||
func ParseRequest(r *http.Request, w http.ResponseWriter, obj interface{}) error {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
||||
dec := json.NewDecoder(r.Body)
|
||||
return dec.Decode(&obj)
|
||||
}
|
||||
|
||||
func WriteResponse(w http.ResponseWriter, v interface{}) {
|
||||
body, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
@ -39,15 +45,17 @@ func Notify(w http.ResponseWriter, r *http.Request) {
|
||||
log.Fatalf("error getting Messaging client: %v\n", err)
|
||||
}
|
||||
|
||||
// This registration token comes from the client FCM SDKs.
|
||||
registrationToken := "dKYLg8VpRGiYciBtuh_Wrs:APA91bEvpRVQscGZuVK8ynYRT_3ZJLuZJKVm705deeVd7EMe6ISok3hqXLNuVKbSR0Ck0EyxYyOoAKgOQY--MU7AacWtvU3qbnTEZ6Df-ZoO61NVGziZ5TBacLjiy9YoLcqCrxvYy2yp"
|
||||
var msg PushMessage
|
||||
if err := ParseRequest(r, w, &msg); err != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
// See documentation on defining a message payload.
|
||||
notification := &messaging.Notification{ Title: "TEST TITLE", Body: "TEST BODY" }
|
||||
notification := &messaging.Notification{ Title: msg.Title, Body: msg.Body }
|
||||
message := &messaging.Message{
|
||||
Topic: "news",
|
||||
Notification : notification,
|
||||
Token: registrationToken,
|
||||
Token: msg.Token,
|
||||
}
|
||||
|
||||
// Send a message to the device corresponding to the provided
|
||||
@ -59,5 +67,6 @@ func Notify(w http.ResponseWriter, r *http.Request) {
|
||||
// Response is a message ID string.
|
||||
fmt.Println("Successfully sent message:", response)
|
||||
|
||||
WriteResponse(w, nil)
|
||||
res := &PushResponse{ Message: response }
|
||||
WriteResponse(w, res);
|
||||
}
|
||||
|
14
net/repeater/internal/models.go
Normal file
14
net/repeater/internal/models.go
Normal file
@ -0,0 +1,14 @@
|
||||
package repeater
|
||||
|
||||
//PushMessage notification
|
||||
type PushMessage struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
//PushResponse notification response
|
||||
type PushResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user