update notification payload and add demo

This commit is contained in:
Sinh 2021-12-03 11:58:55 +07:00
parent 20b0bccbc2
commit 282a94a2ef
3 changed files with 81 additions and 20 deletions

58
example/main.go Normal file
View File

@ -0,0 +1,58 @@
package main
import (
"fmt"
"github.com/Selly-Modules/natsio"
"github.com/Selly-Modules/notification"
)
func main() {
c, err := notification.NewClient(notification.Config{
APIKey: "UWvieSs2erFfhwvl1g8CavEY2V7ouc3",
Nats: natsio.Config{
URL: "127.0.0.1:4222",
User: "",
Password: "",
TLS: nil,
},
})
if err != nil {
panic(err)
}
userID := "61a499ad8d5770f8872b03d8"
requestID, err := c.PushToUsers(notification.PushRequest{
Title: "Notification 1",
Body: "nats stream view notification",
Data: "{}",
Users: []string{userID},
Label: "tracking-label",
Category: "order",
})
if err != nil {
fmt.Println("Push err: ", err)
}
fmt.Println("Request id: ", requestID)
total, err := c.CountUnread(notification.CountUnread{
User: userID,
Category: "order",
})
fmt.Println("Count: ", total, err)
res, err := c.Query(notification.Query{
User: userID,
Category: "order",
Page: 0,
Limit: 20,
})
fmt.Println("Query : ", res, err)
total, err = c.CountUnread(notification.CountUnread{
User: userID,
Category: "order",
})
fmt.Println("Count: 2", total, err)
}

View File

@ -10,11 +10,12 @@ type Config struct {
// PushRequest ... // PushRequest ...
type PushRequest struct { type PushRequest struct {
Title string `json:"title"` Title string `json:"title"`
Body string `json:"body"` Body string `json:"body"`
Data string `json:"data"` Data string `json:"data"`
Users []string `json:"users"` Users []string `json:"users"`
Label string `json:"label"` Label string `json:"label"`
Category string `json:"category"`
} }
// PushResponse ... // PushResponse ...
@ -24,14 +25,15 @@ type PushResponse struct {
} }
type pushRequest struct { type pushRequest struct {
APIKey string `json:"apiKey"` APIKey string `json:"apiKey"`
Title string `json:"title"` Title string `json:"title"`
Body string `json:"body"` Body string `json:"body"`
Data string `json:"data"` Data string `json:"data"`
SendBy string `json:"sendBy"` SendBy string `json:"sendBy"`
Users []string `json:"users"` Users []string `json:"users"`
Topic string `json:"topic"` Topic string `json:"topic"`
Label string `json:"label"` Label string `json:"label"`
Category string `json:"category"`
} }
// Query ... // Query ...

View File

@ -51,13 +51,14 @@ func NewClient(cfg Config) (*Client, error) {
// PushToUsers push notification to list user id // PushToUsers push notification to list user id
func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) { func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) {
p := pushRequest{ p := pushRequest{
APIKey: c.Config.APIKey, APIKey: c.Config.APIKey,
Title: payload.Title, Title: payload.Title,
Body: payload.Body, Body: payload.Body,
Data: payload.Data, Data: payload.Data,
SendBy: SendByUsers, SendBy: SendByUsers,
Users: payload.Users, Users: payload.Users,
Label: payload.Label, Label: payload.Label,
Category: payload.Category,
} }
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p)) msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p))
if err != nil { if err != nil {