admin-push-notification-supplier #1
|
@ -24,37 +24,37 @@ func main() {
|
||||||
|
|
||||||
userID := "61a499ad8d5770f8872b03d8"
|
userID := "61a499ad8d5770f8872b03d8"
|
||||||
requestID, err := c.PushToUsers(notification.PushRequest{
|
requestID, err := c.PushToUsers(notification.PushRequest{
|
||||||
Title: "Notification 1",
|
Title: "Notification campaign 111",
|
||||||
Body: "nats stream view notification",
|
Body: "Notification campaign 11",
|
||||||
Data: "{}",
|
Data: "{}",
|
||||||
Users: []string{userID},
|
Users: []string{userID},
|
||||||
Label: "tracking-label",
|
Label: "tracking-campaign",
|
||||||
Category: "order",
|
Category: "campaign",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Push err: ", err)
|
fmt.Println("Push err: ", err)
|
||||||
}
|
}
|
||||||
fmt.Println("Request id: ", requestID)
|
fmt.Println("Request id: ", requestID)
|
||||||
|
|
||||||
total, err := c.CountUnread(notification.CountUnread{
|
unread, err := c.CountUnread(notification.CountUnread{
|
||||||
User: userID,
|
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{
|
res, err := c.Query(notification.Query{
|
||||||
User: userID,
|
User: userID,
|
||||||
Category: "order",
|
|
||||||
Page: 0,
|
Page: 0,
|
||||||
Limit: 20,
|
Limit: 20,
|
||||||
|
Category: "", // get all if not specify category
|
||||||
})
|
})
|
||||||
fmt.Println("Query : ", res, err)
|
fmt.Println("Query : ", res, err)
|
||||||
|
|
||||||
total, err = c.CountUnread(notification.CountUnread{
|
unread, err = c.CountUnread(notification.CountUnread{
|
||||||
User: userID,
|
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{
|
err = c.Subscribe("test", []string{
|
||||||
"eX1gEc7WokSHh-zJ3WR5Hn:APA91bFZDuzkjjFFL6TNpMg0ot93a0wsypWi4aCdm7M2x6AihgjS_QWsbKSFCT4hNhv_d8wKGy-DG6_3e8OlwPiWiJB4R33xLbbUekgxKcfCiiFooIC1E1OE3XWkvUtn4egn8aLG5jqv",
|
"eX1gEc7WokSHh-zJ3WR5Hn:APA91bFZDuzkjjFFL6TNpMg0ot93a0wsypWi4aCdm7M2x6AihgjS_QWsbKSFCT4hNhv_d8wKGy-DG6_3e8OlwPiWiJB4R33xLbbUekgxKcfCiiFooIC1E1OE3XWkvUtn4egn8aLG5jqv",
|
||||||
|
|
31
model.go
31
model.go
|
@ -32,18 +32,10 @@ type PushResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type pushRequest struct {
|
type pushRequest struct {
|
||||||
|
PushRequest
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
APIKey string `json:"apiKey"`
|
APIKey string `json:"apiKey"`
|
||||||
Title string `json:"title"`
|
|
||||||
Body string `json:"body"`
|
|
||||||
Data string `json:"data"`
|
|
||||||
SendBy string `json:"sendBy"`
|
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 ...
|
// Query ...
|
||||||
|
@ -56,10 +48,7 @@ type Query struct {
|
||||||
|
|
||||||
type query struct {
|
type query struct {
|
||||||
APIKey string `json:"apiKey"`
|
APIKey string `json:"apiKey"`
|
||||||
User string `json:"user"`
|
Query
|
||||||
Category string `json:"category,omitempty"`
|
|
||||||
Page int64 `json:"page,omitempty"`
|
|
||||||
Limit int64 `json:"limit,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notification ...
|
// Notification ...
|
||||||
|
@ -99,13 +88,17 @@ type CountUnread struct {
|
||||||
|
|
||||||
// CountUnreadResponse ...
|
// CountUnreadResponse ...
|
||||||
type CountUnreadResponse struct {
|
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 {
|
type countUnread struct {
|
||||||
|
CountUnread
|
||||||
APIKey string `json:"apiKey"`
|
APIKey string `json:"apiKey"`
|
||||||
User string `json:"user"`
|
|
||||||
Category string `json:"category"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommonError ...
|
// CommonError ...
|
||||||
|
@ -123,3 +116,9 @@ type subscribe struct {
|
||||||
Subscribe
|
Subscribe
|
||||||
APIKey string `json:"apiKey"`
|
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
|
// version specify current version of client
|
||||||
const version = "1.0.2"
|
const version = "1.0.3"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SendByTopic = "topic"
|
SendByTopic = "topic"
|
||||||
|
@ -20,6 +20,7 @@ const (
|
||||||
SubjectPushNotification = "push_notification"
|
SubjectPushNotification = "push_notification"
|
||||||
SubjectGetNotification = "get_notification"
|
SubjectGetNotification = "get_notification"
|
||||||
SubjectReadNotification = "read_notification"
|
SubjectReadNotification = "read_notification"
|
||||||
|
SubjectReadAllNotification = "read_all_notification"
|
||||||
SubjectCountUnreadNotification = "count_unread_notification"
|
SubjectCountUnreadNotification = "count_unread_notification"
|
||||||
SubjectSubscribeTopic = "subscribe_topic"
|
SubjectSubscribeTopic = "subscribe_topic"
|
||||||
SubjectUnsubscribeTopic = "unsubscribe_topic"
|
SubjectUnsubscribeTopic = "unsubscribe_topic"
|
||||||
|
@ -58,15 +59,8 @@ func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error)
|
||||||
p := pushRequest{
|
p := pushRequest{
|
||||||
Version: version,
|
Version: version,
|
||||||
APIKey: c.Config.APIKey,
|
APIKey: c.Config.APIKey,
|
||||||
Title: payload.Title,
|
|
||||||
Body: payload.Body,
|
|
||||||
Data: payload.Data,
|
|
||||||
SendBy: SendByUsers,
|
SendBy: SendByUsers,
|
||||||
Users: payload.Users,
|
PushRequest: payload,
|
||||||
Label: payload.Label,
|
|
||||||
Category: payload.Category,
|
|
||||||
Sound: payload.Sound,
|
|
||||||
Link: payload.Link,
|
|
||||||
}
|
}
|
||||||
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p))
|
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p))
|
||||||
if err != nil {
|
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) {
|
func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
||||||
p := query{
|
p := query{
|
||||||
APIKey: c.Config.APIKey,
|
APIKey: c.Config.APIKey,
|
||||||
User: q.User,
|
Query: q,
|
||||||
Category: q.Category,
|
|
||||||
Page: q.Page,
|
|
||||||
Limit: q.Limit,
|
|
||||||
}
|
}
|
||||||
msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p))
|
msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -112,27 +103,26 @@ func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountUnread count total unread notification
|
// CountUnread count total unread notification
|
||||||
func (c *Client) CountUnread(q CountUnread) (int64, error) {
|
func (c *Client) CountUnread(q CountUnread) ([]CategoryCount, error) {
|
||||||
p := countUnread{
|
p := countUnread{
|
||||||
APIKey: c.Config.APIKey,
|
APIKey: c.Config.APIKey,
|
||||||
User: q.User,
|
CountUnread: q,
|
||||||
Category: q.Category,
|
|
||||||
}
|
}
|
||||||
msg, err := c.natsServer.Request(SubjectCountUnreadNotification, toBytes(p))
|
msg, err := c.natsServer.Request(SubjectCountUnreadNotification, toBytes(p))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var res struct {
|
var res struct {
|
||||||
Data CountUnreadResponse `json:"data"`
|
Data CountUnreadResponse `json:"data"`
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
if err = json.Unmarshal(msg.Data, &res); err != nil {
|
||||||
return 0, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if res.Error != "" {
|
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
|
// Read mark notification as read
|
||||||
|
@ -155,6 +145,29 @@ func (c *Client) Read(notificationID string) error {
|
||||||
return err
|
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
|
// Subscribe tokens to topic
|
||||||
func (c *Client) Subscribe(topic string, tokens []string) error {
|
func (c *Client) Subscribe(topic string, tokens []string) error {
|
||||||
p := subscribe{
|
p := subscribe{
|
||||||
|
|
Loading…
Reference in New Issue