Skip to content

Commit 25f2d53

Browse files
authored
Add remainder of EPushType types (#153)
* Add remainder of EPushType types
1 parent cd7205e commit 25f2d53

File tree

2 files changed

+134
-30
lines changed

2 files changed

+134
-30
lines changed

client_test.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import (
1616

1717
"golang.org/x/net/http2"
1818

19+
apns "github.com/sideshow/apns2"
1920
"github.com/sideshow/apns2/certificate"
2021
"github.com/sideshow/apns2/token"
2122
"github.com/stretchr/testify/assert"
22-
apns "github.com/sideshow/apns2"
2323
)
2424

2525
// Mocks
@@ -180,20 +180,84 @@ func TestHeaders(t *testing.T) {
180180
n.Topic = "com.testapp"
181181
n.Priority = 10
182182
n.Expiration = time.Now()
183-
n.PushType = apns.PushTypeBackground
184183
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
185184
assert.Equal(t, n.ApnsID, r.Header.Get("apns-id"))
186185
assert.Equal(t, n.CollapseID, r.Header.Get("apns-collapse-id"))
187186
assert.Equal(t, "10", r.Header.Get("apns-priority"))
188187
assert.Equal(t, n.Topic, r.Header.Get("apns-topic"))
189188
assert.Equal(t, fmt.Sprintf("%v", n.Expiration.Unix()), r.Header.Get("apns-expiration"))
189+
}))
190+
defer server.Close()
191+
_, err := mockClient(server.URL).Push(n)
192+
assert.NoError(t, err)
193+
}
194+
195+
func TestPushTypeAlertHeader(t *testing.T) {
196+
n := mockNotification()
197+
n.PushType = apns.PushTypeAlert
198+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
199+
assert.Equal(t, "alert", r.Header.Get("apns-push-type"))
200+
}))
201+
defer server.Close()
202+
_, err := mockClient(server.URL).Push(n)
203+
assert.NoError(t, err)
204+
}
205+
206+
func TestPushTypeBackgroundHeader(t *testing.T) {
207+
n := mockNotification()
208+
n.PushType = apns.PushTypeBackground
209+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
190210
assert.Equal(t, "background", r.Header.Get("apns-push-type"))
191211
}))
192212
defer server.Close()
193213
_, err := mockClient(server.URL).Push(n)
194214
assert.NoError(t, err)
195215
}
196216

217+
func TestPushTypeVOIPHeader(t *testing.T) {
218+
n := mockNotification()
219+
n.PushType = apns.PushTypeVOIP
220+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
221+
assert.Equal(t, "voip", r.Header.Get("apns-push-type"))
222+
}))
223+
defer server.Close()
224+
_, err := mockClient(server.URL).Push(n)
225+
assert.NoError(t, err)
226+
}
227+
228+
func TestPushTypeComplicationHeader(t *testing.T) {
229+
n := mockNotification()
230+
n.PushType = apns.PushTypeComplication
231+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
232+
assert.Equal(t, "complication", r.Header.Get("apns-push-type"))
233+
}))
234+
defer server.Close()
235+
_, err := mockClient(server.URL).Push(n)
236+
assert.NoError(t, err)
237+
}
238+
239+
func TestPushTypeFileProviderHeader(t *testing.T) {
240+
n := mockNotification()
241+
n.PushType = apns.PushTypeFileProvider
242+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
243+
assert.Equal(t, "fileprovider", r.Header.Get("apns-push-type"))
244+
}))
245+
defer server.Close()
246+
_, err := mockClient(server.URL).Push(n)
247+
assert.NoError(t, err)
248+
}
249+
250+
func TestPushTypeMDMHeader(t *testing.T) {
251+
n := mockNotification()
252+
n.PushType = apns.PushTypeMDM
253+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
254+
assert.Equal(t, "mdm", r.Header.Get("apns-push-type"))
255+
}))
256+
defer server.Close()
257+
_, err := mockClient(server.URL).Push(n)
258+
assert.NoError(t, err)
259+
}
260+
197261
func TestAuthorizationHeader(t *testing.T) {
198262
n := mockNotification()
199263
token := mockToken()

notification.go

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,107 @@ import (
55
"time"
66
)
77

8-
//EPushType defines the value for the apns-push-type header
8+
// EPushType defines the value for the apns-push-type header
99
type EPushType string
1010

1111
const (
12-
//PushTypeAlert will set the apns-push-type header to 'alert'
12+
// PushTypeAlert is used for notifications that trigger a user interaction —
13+
// for example, an alert, badge, or sound. If you set this push type, the
14+
// apns-topic header field must use your app’s bundle ID as the topic. The
15+
// alert push type is required on watchOS 6 and later. It is recommended on
16+
// macOS, iOS, tvOS, and iPadOS.
1317
PushTypeAlert EPushType = "alert"
14-
//PushTypeBackground will set the apns-push-type header to 'background'
18+
19+
// PushTypeBackground is used for notifications that deliver content in the
20+
// background, and don’t trigger any user interactions. If you set this push
21+
// type, the apns-topic header field must use your app’s bundle ID as the
22+
// topic. The background push type is required on watchOS 6 and later. It is
23+
// recommended on macOS, iOS, tvOS, and iPadOS.
1524
PushTypeBackground EPushType = "background"
16-
//PushTypeVoIP will set the apns-push-type header to 'voip' which is required
17-
// for PushKit applications.
18-
PushTypeVoIP EPushType = "voip"
25+
26+
// PushTypeVOIP is used for notifications that provide information about an
27+
// incoming Voice-over-IP (VoIP) call. If you set this push type, the
28+
// apns-topic header field must use your app’s bundle ID with .voip appended
29+
// to the end. If you’re using certificate-based authentication, you must
30+
// also register the certificate for VoIP services. The voip push type is
31+
// not available on watchOS. It is recommended on macOS, iOS, tvOS, and
32+
// iPadOS.
33+
PushTypeVOIP EPushType = "voip"
34+
35+
// PushTypeComplication is used for notifications that contain update
36+
// information for a watchOS app’s complications. If you set this push type,
37+
// the apns-topic header field must use your app’s bundle ID with
38+
// .complication appended to the end. If you’re using certificate-based
39+
// authentication, you must also register the certificate for WatchKit
40+
// services. The complication push type is recommended for watchOS and iOS.
41+
// It is not available on macOS, tvOS, and iPadOS.
42+
PushTypeComplication EPushType = "complication"
43+
44+
// PushTypeFileProvider is used to signal changes to a File Provider
45+
// extension. If you set this push type, the apns-topic header field must
46+
// use your app’s bundle ID with .pushkit.fileprovider appended to the end.
47+
// The fileprovider push type is not available on watchOS. It is recommended
48+
// on macOS, iOS, tvOS, and iPadOS.
49+
PushTypeFileProvider EPushType = "fileprovider"
50+
51+
// PushTypeMDM is used for notifications that tell managed devices to
52+
// contact the MDM server. If you set this push type, you must use the topic
53+
// from the UID attribute in the subject of your MDM push certificate.
54+
PushTypeMDM EPushType = "mdm"
1955
)
2056

2157
const (
2258
// PriorityLow will tell APNs to send the push message at a time that takes
2359
// into account power considerations for the device. Notifications with this
24-
// priority might be grouped and delivered in bursts. They are throttled, and
25-
// in some cases are not delivered.
60+
// priority might be grouped and delivered in bursts. They are throttled,
61+
// and in some cases are not delivered.
2662
PriorityLow = 5
2763

2864
// PriorityHigh will tell APNs to send the push message immediately.
29-
// Notifications with this priority must trigger an alert, sound, or badge on
30-
// the target device. It is an error to use this priority for a push
65+
// Notifications with this priority must trigger an alert, sound, or badge
66+
// on the target device. It is an error to use this priority for a push
3167
// notification that contains only the content-available key.
3268
PriorityHigh = 10
3369
)
3470

3571
// Notification represents the the data and metadata for a APNs Remote Notification.
3672
type Notification struct {
3773

38-
// An optional canonical UUID that identifies the notification. The canonical
39-
// form is 32 lowercase hexadecimal digits, displayed in five groups separated
40-
// by hyphens in the form 8-4-4-4-12. An example UUID is as follows:
74+
// An optional canonical UUID that identifies the notification. The
75+
// canonical form is 32 lowercase hexadecimal digits, displayed in five
76+
// groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as
77+
// follows:
4178
//
42-
// 123e4567-e89b-12d3-a456-42665544000
79+
// 123e4567-e89b-12d3-a456-42665544000
4380
//
4481
// If you don't set this, a new UUID is created by APNs and returned in the
4582
// response.
4683
ApnsID string
4784

48-
// A string which allows multiple notifications with the same collapse identifier
49-
// to be displayed to the user as a single notification. The value should not
50-
// exceed 64 bytes.
85+
// A string which allows multiple notifications with the same collapse
86+
// identifier to be displayed to the user as a single notification. The
87+
// value should not exceed 64 bytes.
5188
CollapseID string
5289

53-
// A string containing hexadecimal bytes of the device token for the target device.
90+
// A string containing hexadecimal bytes of the device token for the target
91+
// device.
5492
DeviceToken string
5593

56-
// The topic of the remote notification, which is typically the bundle ID for
57-
// your app. The certificate you create in the Apple Developer Member Center
58-
// must include the capability for this topic. If your certificate includes
59-
// multiple topics, you must specify a value for this header. If you omit this
60-
// header and your APNs certificate does not specify multiple topics, the APNs
61-
// server uses the certificate’s Subject as the default topic.
94+
// The topic of the remote notification, which is typically the bundle ID
95+
// for your app. The certificate you create in the Apple Developer Member
96+
// Center must include the capability for this topic. If your certificate
97+
// includes multiple topics, you must specify a value for this header. If
98+
// you omit this header and your APNs certificate does not specify multiple
99+
// topics, the APNs server uses the certificate’s Subject as the default
100+
// topic.
62101
Topic string
63102

64103
// An optional time at which the notification is no longer valid and can be
65104
// discarded by APNs. If this value is in the past, APNs treats the
66105
// notification as if it expires immediately and does not store the
67106
// notification or attempt to redeliver it. If this value is left as the
68-
// default (ie, Expiration.IsZero()) an expiration header will not added to the
69-
// http request.
107+
// default (ie, Expiration.IsZero()) an expiration header will not added to
108+
// the http request.
70109
Expiration time.Time
71110

72111
// The priority of the notification. Specify ether apns.PriorityHigh (10) or
@@ -79,8 +118,9 @@ type Notification struct {
79118
// Remote Notification Programming Guide for more info.
80119
Payload interface{}
81120

82-
// The pushtype of the push notification. If this values is left as the default
83-
// an apns-push-type header with value 'alert' will be added to the http request.
121+
// The pushtype of the push notification. If this values is left as the
122+
// default an apns-push-type header with value 'alert' will be added to the
123+
// http request.
84124
PushType EPushType
85125
}
86126

0 commit comments

Comments
 (0)