admin-push-notification-supplier #1
|
@ -0,0 +1,7 @@
|
|||
package notification
|
||||
|
||||
const (
|
||||
SortOrderDesc = "-order"
|
||||
SortOrderAsc = "order"
|
||||
SortDefault = "-lastPushAt"
|
||||
)
|
|
@ -24,37 +24,37 @@ func main() {
|
|||
|
||||
userID := "61a499ad8d5770f8872b03d8"
|
||||
requestID, err := c.PushToUsers(notification.PushRequest{
|
||||
Title: "Notification 1",
|
||||
Body: "nats stream view notification",
|
||||
Title: "Notification campaign 111",
|
||||
Body: "Notification campaign 11",
|
||||
Data: "{}",
|
||||
Users: []string{userID},
|
||||
Label: "tracking-label",
|
||||
Category: "order",
|
||||
Label: "tracking-campaign",
|
||||
Category: "campaign",
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("Push err: ", err)
|
||||
}
|
||||
fmt.Println("Request id: ", requestID)
|
||||
|
||||
total, err := c.CountUnread(notification.CountUnread{
|
||||
unread, err := c.CountUnread(notification.CountUnread{
|
||||
User: userID,
|
||||
Category: "order",
|
||||
Category: "", // count all if not specify category
|
||||
})
|
||||
fmt.Println("Count: ", total, err)
|
||||
fmt.Println("Count: ", unread, err)
|
||||
|
||||
res, err := c.Query(notification.Query{
|
||||
User: userID,
|
||||
Category: "order",
|
||||
Page: 0,
|
||||
Limit: 20,
|
||||
Category: "", // get all if not specify category
|
||||
})
|
||||
fmt.Println("Query : ", res, err)
|
||||
|
||||
total, err = c.CountUnread(notification.CountUnread{
|
||||
unread, err = c.CountUnread(notification.CountUnread{
|
||||
User: userID,
|
||||
Category: "order",
|
||||
Category: "",
|
||||
})
|
||||
fmt.Println("Count: 2", total, err)
|
||||
fmt.Printf("Count: 2 %+v, %v\n", unread, err)
|
||||
|
||||
err = c.Subscribe("test", []string{
|
||||
"eX1gEc7WokSHh-zJ3WR5Hn:APA91bFZDuzkjjFFL6TNpMg0ot93a0wsypWi4aCdm7M2x6AihgjS_QWsbKSFCT4hNhv_d8wKGy-DG6_3e8OlwPiWiJB4R33xLbbUekgxKcfCiiFooIC1E1OE3XWkvUtn4egn8aLG5jqv",
|
||||
|
|
35
model.go
35
model.go
|
@ -18,6 +18,8 @@ type PushRequest struct {
|
|||
Category string `json:"category,omitempty"`
|
||||
Sound *Sound `json:"sound,omitempty"`
|
||||
Link string `json:"link"` // for webpush
|
||||
Order int `json:"order"` // for sort
|
||||
DisplayUntil int64 `json:"displayUntil"` // time.Unix
|
||||
}
|
||||
|
||||
// Sound ...
|
||||
|
@ -32,34 +34,25 @@ type PushResponse struct {
|
|||
}
|
||||
|
||||
type pushRequest struct {
|
||||
PushRequest
|
||||
Version string `json:"version"`
|
||||
APIKey string `json:"apiKey"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Data string `json:"data"`
|
||||
SendBy string `json:"sendBy"`
|
||||
Users []string `json:"users"`
|
||||
Topic string `json:"topic"`
|
||||
Label string `json:"label"`
|
||||
Category string `json:"category"`
|
||||
Sound *Sound `json:"sound,omitempty"`
|
||||
Link string `json:"link"`
|
||||
}
|
||||
|
||||
// Query ...
|
||||
type Query struct {
|
||||
User string `json:"user"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Page int64 `json:"page,omitempty"`
|
||||
Limit int64 `json:"limit,omitempty"`
|
||||
Sort string `json:"sort,omitempty"` // SortDefault, SortOrderDesc, SortOrderAsc
|
||||
}
|
||||
|
||||
type query struct {
|
||||
APIKey string `json:"apiKey"`
|
||||
User string `json:"user"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Page int64 `json:"page,omitempty"`
|
||||
Limit int64 `json:"limit,omitempty"`
|
||||
Query
|
||||
}
|
||||
|
||||
// Notification ...
|
||||
|
@ -99,13 +92,17 @@ type CountUnread struct {
|
|||
|
||||
// CountUnreadResponse ...
|
||||
type CountUnreadResponse struct {
|
||||
Total int64 `json:"total"`
|
||||
List []CategoryCount `json:"list"`
|
||||
}
|
||||
|
||||
type CategoryCount struct {
|
||||
Category string `json:"category"`
|
||||
TotalUnread int64 `json:"totalUnread"`
|
||||
}
|
||||
|
||||
type countUnread struct {
|
||||
CountUnread
|
||||
APIKey string `json:"apiKey"`
|
||||
User string `json:"user"`
|
||||
Category string `json:"category"`
|
||||
}
|
||||
|
||||
// CommonError ...
|
||||
|
@ -123,3 +120,9 @@ type subscribe struct {
|
|||
Subscribe
|
||||
APIKey string `json:"apiKey"`
|
||||
}
|
||||
|
||||
// ReadAll ...
|
||||
type ReadAll struct {
|
||||
User string `json:"user"`
|
||||
Category string `json:"category,omitempty"`
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
)
|
||||
|
||||
// version specify current version of client
|
||||
const version = "1.0.2"
|
||||
const version = "1.0.3"
|
||||
|
||||
const (
|
||||
SendByTopic = "topic"
|
||||
|
@ -20,6 +20,7 @@ const (
|
|||
SubjectPushNotification = "push_notification"
|
||||
SubjectGetNotification = "get_notification"
|
||||
SubjectReadNotification = "read_notification"
|
||||
SubjectReadAllNotification = "read_all_notification"
|
||||
SubjectCountUnreadNotification = "count_unread_notification"
|
||||
SubjectSubscribeTopic = "subscribe_topic"
|
||||
SubjectUnsubscribeTopic = "unsubscribe_topic"
|
||||
|
@ -58,15 +59,8 @@ func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error)
|
|||
p := pushRequest{
|
||||
Version: version,
|
||||
APIKey: c.Config.APIKey,
|
||||
Title: payload.Title,
|
||||
Body: payload.Body,
|
||||
Data: payload.Data,
|
||||
SendBy: SendByUsers,
|
||||
Users: payload.Users,
|
||||
Label: payload.Label,
|
||||
Category: payload.Category,
|
||||
Sound: payload.Sound,
|
||||
Link: payload.Link,
|
||||
PushRequest: payload,
|
||||
}
|
||||
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p))
|
||||
if err != nil {
|
||||
|
@ -89,10 +83,7 @@ func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error)
|
|||
func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
||||
p := query{
|
||||
APIKey: c.Config.APIKey,
|
||||
User: q.User,
|
||||
Category: q.Category,
|
||||
Page: q.Page,
|
||||
Limit: q.Limit,
|
||||
Query: q,
|
||||
}
|
||||
msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p))
|
||||
if err != nil {
|
||||
|
@ -112,27 +103,26 @@ func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
|||
}
|
||||
|
||||
// CountUnread count total unread notification
|
||||
func (c *Client) CountUnread(q CountUnread) (int64, error) {
|
||||
func (c *Client) CountUnread(q CountUnread) ([]CategoryCount, error) {
|
||||
p := countUnread{
|
||||
APIKey: c.Config.APIKey,
|
||||
User: q.User,
|
||||
Category: q.Category,
|
||||
CountUnread: q,
|
||||
}
|
||||
msg, err := c.natsServer.Request(SubjectCountUnreadNotification, toBytes(p))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return nil, err
|
||||
}
|
||||
var res struct {
|
||||
Data CountUnreadResponse `json:"data"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
||||
return 0, err
|
||||
if err = json.Unmarshal(msg.Data, &res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.Error != "" {
|
||||
return 0, errors.New(res.Error)
|
||||
return nil, errors.New(res.Error)
|
||||
}
|
||||
return res.Data.Total, nil
|
||||
return res.Data.List, nil
|
||||
}
|
||||
|
||||
// Read mark notification as read
|
||||
|
@ -155,6 +145,29 @@ func (c *Client) Read(notificationID string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// ReadAll mark notification as read
|
||||
func (c *Client) ReadAll(r ReadAll) error {
|
||||
p := struct {
|
||||
ReadAll
|
||||
APIKey string `json:"apiKey"`
|
||||
}{
|
||||
ReadAll: r,
|
||||
APIKey: c.Config.APIKey,
|
||||
}
|
||||
msg, err := c.natsServer.Request(SubjectReadAllNotification, toBytes(p))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var res CommonError
|
||||
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
||||
return err
|
||||
}
|
||||
if res.Error != "" {
|
||||
err = errors.New(res.Error)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Subscribe tokens to topic
|
||||
func (c *Client) Subscribe(topic string, tokens []string) error {
|
||||
p := subscribe{
|
||||
|
|
Loading…
Reference in New Issue