-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications.go
111 lines (94 loc) · 2.78 KB
/
notifications.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package onesignal
import (
"encoding/json"
"net/url"
)
// NotificationServices handles communication with onesignal api.
type NotificationServices struct {
client *Client
}
// Browse function
//
// Browse notifications, you can see all the notification you have
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/view-notifications
func (c *NotificationServices) Browse(limit, offset string) (Success, error) {
u, err := url.Parse(c.client.BaseURL.String() + "notifications?app_id=" + c.client.AppID + "&limit=" + limit + "&offset=" + offset)
if err != nil {
return Success{}, err
}
resp, err := GET(u.String(), c.client)
if err != nil {
return Success{}, err
}
defer resp.Body.Close()
var data Success
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return Success{}, err
}
return data, nil
}
// Get function
//
// View the details of a single notification. This function returns a JSON object not a struct like others
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/view-notification
func (c *NotificationServices) Get(id string) (interface{}, error) {
u, err := url.Parse(c.client.BaseURL.String() + "notifications/" + id + "?app_id=" + c.client.AppID)
if err != nil {
return Success{}, err
}
resp, err := GET(u.String(), c.client)
if err != nil {
return Success{}, err
}
defer resp.Body.Close()
var data interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return Success{}, err
}
return data, nil
}
// Create function
//
// Create a notification
//
// Sends notifications to your users. This function relates to https://onesignal.com/api/v1/notifications
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/create-notification
func (c *NotificationServices) Create(opt *NotificationOpt) (Success, error) {
u, err := url.Parse(c.client.BaseURL.String() + "notifications")
if err != nil {
return Success{}, err
}
opt.AppID = c.client.AppID
b, err := EncodeBody(opt)
if err != nil {
return Success{}, err
}
resp, err := POST(u.String(), b, c.client)
if err != nil {
return Success{}, err
}
return resp, nil
}
// Cancel function
//
// Cancel a notification
//
// Stop a scheduled or currently outgoing notification
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/cancel-notification
func (c *NotificationServices) Cancel(id string) (Success, error) {
u, err := url.Parse(c.client.BaseURL.String() + "notifications/" + id + "?app_id=" + c.client.AppID)
if err != nil {
return Success{}, err
}
resp, err := DELETE(u.String(), c.client)
if err != nil {
return Success{}, err
}
return resp, nil
}