Skip to content

Commit 80d1440

Browse files
committed
Merge pull request #9 from nathany/safari
Support Safari Push and Mobile Device Management
2 parents e4ea1d8 + 63c006b commit 80d1440

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ c, _ := apns.NewClient(apns.ProductionGateway, apnsCert, apnsKey)
2828

2929
p := apns.NewPayload()
3030
p.APS.Alert.Body = "I am a push notification!"
31-
p.APS.Badge = 5
31+
badge := 5
32+
p.APS.Badge = &badge
3233
p.APS.Sound = "turn_down_for_what.aiff"
3334

3435
m := apns.NewNotification()
@@ -55,7 +56,8 @@ go func() {
5556

5657
p := apns.NewPayload()
5758
p.APS.Alert.Body = "I am a push notification!"
58-
p.APS.Badge = 5
59+
badge := 5
60+
p.APS.Badge = &badge
5961
p.APS.Sound = "turn_down_for_what.aiff"
6062
p.APS.ContentAvailable = 1
6163

notification.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type NotificationResult struct {
4040
type Alert struct {
4141
// Do not add fields without updating the implementation of isZero.
4242
Body string `json:"body,omitempty"`
43+
Title string `json:"title,omitempty"`
44+
Action string `json:"action,omitempty"`
4345
LocKey string `json:"loc-key,omitempty"`
4446
LocArgs []string `json:"loc-args,omitempty"`
4547
ActionLocKey string `json:"action-loc-key,omitempty"`
@@ -55,6 +57,7 @@ type APS struct {
5557
Badge *int // 0 to clear notifications, nil to leave as is.
5658
Sound string
5759
ContentAvailable int
60+
URLArgs []string
5861
Category string // requires iOS 8+
5962
}
6063

@@ -76,17 +79,26 @@ func (aps APS) MarshalJSON() ([]byte, error) {
7679
if aps.Category != "" {
7780
data["category"] = aps.Category
7881
}
82+
if aps.URLArgs != nil && len(aps.URLArgs) != 0 {
83+
data["url-args"] = aps.URLArgs
84+
}
7985

8086
return json.Marshal(data)
8187
}
8288

8389
type Payload struct {
84-
APS APS
90+
APS APS
91+
// MDM for mobile device management
92+
MDM string
8593
customValues map[string]interface{}
8694
}
8795

8896
func (p *Payload) MarshalJSON() ([]byte, error) {
89-
p.customValues["aps"] = p.APS
97+
if len(p.MDM) != 0 {
98+
p.customValues["mdm"] = p.MDM
99+
} else {
100+
p.customValues["aps"] = p.APS
101+
}
90102

91103
return json.Marshal(p.customValues)
92104
}

notification_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ var _ = Describe("Notifications", func() {
8282
})
8383
})
8484

85+
Describe("Safari", func() {
86+
Describe("#MarshalJSON", func() {
87+
Context("with complete payload", func() {
88+
It("should marshal APS", func() {
89+
p := apns.NewPayload()
90+
91+
p.APS.Alert.Title = "Hello World!"
92+
p.APS.Alert.Body = "This is a body"
93+
p.APS.Alert.Action = "Launch"
94+
p.APS.URLArgs = []string{"hello", "world"}
95+
96+
b, err := json.Marshal(p)
97+
98+
Expect(err).To(BeNil())
99+
Expect(b).To(Equal([]byte(`{"aps":{"alert":{"body":"This is a body","title":"Hello World!","action":"Launch"},"url-args":["hello","world"]}}`)))
100+
})
101+
})
102+
})
103+
})
104+
85105
Describe("Payload", func() {
86106
Describe("#MarshalJSON", func() {
87107
Context("no alert (as with Passbook)", func() {
@@ -129,6 +149,19 @@ var _ = Describe("Notifications", func() {
129149
Expect(b).To(Equal([]byte(`{"aps":{"alert":{"body":"testing"}},"email":"[email protected]"}`)))
130150
})
131151
})
152+
153+
Context("with only MDM", func() {
154+
It("should marshal MDM", func() {
155+
p := apns.NewPayload()
156+
157+
p.MDM = "00000000-1111-3333-4444-555555555555"
158+
159+
b, err := json.Marshal(p)
160+
161+
Expect(err).To(BeNil())
162+
Expect(b).To(Equal([]byte(`{"mdm":"00000000-1111-3333-4444-555555555555"}`)))
163+
})
164+
})
132165
})
133166
})
134167

0 commit comments

Comments
 (0)