-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathregistration.go
219 lines (199 loc) · 6.67 KB
/
registration.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package notificationhubs
import (
"bytes"
"context"
"encoding/xml"
"errors"
"path"
"strings"
"time"
)
// newRegistration initializes and returns a Notification pointer
func newRegistration(deviceID string, expirationTime *time.Time, notificationFormat NotificationFormat,
registrationID string, tags string) *Registration {
return &Registration{
deviceID,
expirationTime,
notificationFormat,
registrationID,
tags,
}
}
// newTemplateRegistration initializes and returns a TemplateNotification pointer
func newTemplateRegistration(deviceID string, expirationTime *time.Time, registrationID string, tags string,
platform TargetPlatform, template string) *TemplateRegistration {
return &TemplateRegistration{
deviceID,
expirationTime,
registrationID,
tags,
platform,
template,
}
}
// Normalize normalizes all devices in the feed
func (r *Registrations) normalize() {
for _, entry := range r.Entries {
if entry.RegistrationContent != nil {
entry.RegistrationContent.normalize()
}
}
}
// Normalize the registration result
func (r RegistrationResult) normalize() {
if r.RegistrationContent != nil {
r.RegistrationContent.normalize()
}
}
// Normalize normalizes the different devices
func (r *RegistrationContent) normalize() {
if r.AppleRegistrationDescription != nil || r.AppleTemplateRegistrationDescription != nil {
if r.AppleTemplateRegistrationDescription != nil {
r.Format = Template
r.Target = AppleTemplatePlatform
r.RegisteredDevice = r.AppleTemplateRegistrationDescription
} else {
r.Format = AppleFormat
r.Target = ApplePlatform
r.RegisteredDevice = r.AppleRegistrationDescription
}
r.RegisteredDevice.DeviceID = *r.RegisteredDevice.DeviceToken
r.RegisteredDevice.DeviceToken = nil
r.AppleRegistrationDescription = nil
r.AppleTemplateRegistrationDescription = nil
} else if r.GcmRegistrationDescription != nil || r.GcmTemplateRegistrationDescription != nil {
if r.GcmTemplateRegistrationDescription != nil {
r.Format = Template
r.Target = GcmTemplatePlatform
r.RegisteredDevice = r.GcmTemplateRegistrationDescription
} else {
r.Format = GcmFormat
r.Target = GcmPlatform
r.RegisteredDevice = r.GcmRegistrationDescription
}
r.RegisteredDevice.DeviceID = *r.RegisteredDevice.GcmRegistrationID
r.RegisteredDevice.GcmRegistrationID = nil
r.GcmRegistrationDescription = nil
r.GcmTemplateRegistrationDescription = nil
}
if r.RegisteredDevice != nil {
expirationTime, err := time.Parse("2006-01-02T15:04:05.000Z", *r.RegisteredDevice.ExpirationTimeString)
if err != nil { // The API just forwards the date string used by Apple, Google etc unfortunately. So format varies.
expirationTime, _ = time.Parse("2006-01-02T15:04:05.000", *r.RegisteredDevice.ExpirationTimeString)
}
r.RegisteredDevice.ExpirationTime = &expirationTime
r.RegisteredDevice.ExpirationTimeString = nil
if r.RegisteredDevice.TagsString != nil {
r.RegisteredDevice.Tags = strings.Split(*r.RegisteredDevice.TagsString, ",")
}
r.RegisteredDevice.TagsString = nil
}
}
// Registration reads one specific registration
func (h *NotificationHub) Registration(ctx context.Context, registrationID string) (raw []byte, registrationResult *RegistrationResult, err error) {
var (
regURL = h.generateAPIURL(path.Join("registrations", registrationID))
)
raw, _, err = h.exec(ctx, getMethod, regURL, Headers{}, nil)
if err != nil {
return
}
if err = xml.Unmarshal(raw, ®istrationResult); err != nil {
return
}
registrationResult.RegistrationContent.normalize()
return
}
// Registrations reads all registrations
func (h *NotificationHub) Registrations(ctx context.Context) (raw []byte, registrations *Registrations, err error) {
raw, _, err = h.exec(ctx, getMethod, h.generateAPIURL("registrations"), Headers{}, nil)
if err != nil {
return
}
if err = xml.Unmarshal(raw, ®istrations); err != nil {
return
}
registrations.normalize()
return
}
// Register sends a device registration to the Azure hub
func (h *NotificationHub) Register(ctx context.Context, r Registration) (raw []byte, registrationResult *RegistrationResult, err error) {
var (
regURL = h.generateAPIURL("registrations")
method = postMethod
payload = ""
headers = map[string]string{
"Content-Type": "application/atom+xml;type=entry;charset=utf-8",
}
)
switch r.NotificationFormat {
case AppleFormat:
payload = strings.Replace(appleRegXMLString, "{{DeviceID}}", r.DeviceID, 1)
case GcmFormat:
payload = strings.Replace(gcmRegXMLString, "{{DeviceID}}", r.DeviceID, 1)
default:
return nil, nil, errors.New("Notification format not implemented")
}
payload = strings.Replace(payload, "{{Tags}}", r.Tags, 1)
if r.RegistrationID != "" {
method = putMethod
regURL.Path = path.Join(regURL.Path, r.RegistrationID)
}
raw, _, err = h.exec(ctx, method, regURL, headers, bytes.NewBufferString(payload))
if err == nil {
if err = xml.Unmarshal(raw, ®istrationResult); err != nil {
return
}
}
if registrationResult != nil {
registrationResult.normalize()
}
return
}
// RegisterWithTemplate sends a device registration with template to the Azure hub
func (h *NotificationHub) RegisterWithTemplate(ctx context.Context, r TemplateRegistration) (raw []byte, registrationResult *RegistrationResult, err error) {
var (
regURL = h.generateAPIURL("registrations")
method = postMethod
payload = ""
headers = map[string]string{
"Content-Type": "application/atom+xml;type=entry;charset=utf-8",
}
)
switch r.Platform {
case ApplePlatform:
payload = strings.Replace(appleTemplateRegXMLString, "{{DeviceID}}", r.DeviceID, 1)
case GcmPlatform:
payload = strings.Replace(gcmTemplateRegXMLString, "{{DeviceID}}", r.DeviceID, 1)
default:
return nil, nil, errors.New("Notification format not implemented")
}
payload = strings.Replace(payload, "{{Tags}}", r.Tags, 1)
payload = strings.Replace(payload, "{{Template}}", r.Template, 1)
if r.RegistrationID != "" {
method = putMethod
regURL.Path = path.Join(regURL.Path, r.RegistrationID)
}
raw, _, err = h.exec(ctx, method, regURL, headers, bytes.NewBufferString(payload))
if err == nil {
if err = xml.Unmarshal(raw, ®istrationResult); err != nil {
return
}
}
if registrationResult != nil {
registrationResult.normalize()
}
return
}
// Unregister sends a device registration delete to the Azure hub
func (h *NotificationHub) Unregister(ctx context.Context, registration RegisteredDevice) (err error) {
var (
regURL = h.generateAPIURL(path.Join("registrations", registration.RegistrationID))
headers = map[string]string{
"Content-Type": "application/atom+xml;type=entry;charset=utf-8",
"If-Match": registration.ETag,
}
)
_, _, err = h.exec(ctx, deleteMethod, regURL, headers, nil)
return
}