Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Safari Push and Mobile Device Management #9

Merged
merged 5 commits into from
Feb 13, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,26 @@ type NotificationResult struct {

type Alert struct {
Body string `json:"body,omitempty"`
Title string `json:"title,omitempty"`
Action string `json:"action,omitempty"`
LocKey string `json:"loc-key,omitempty"`
LocArgs []string `json:"loc-args,omitempty"`
ActionLocKey string `json:"action-loc-key,omitempty"`
LaunchImage string `json:"launch-image,omitempty"`
}

type APS struct {
Alert Alert `json:"alert,omitempty"`
Badge *int `json:"badge,omitempty"`
Sound string `json:"sound,omitempty"`
ContentAvailable int `json:"content-available,omitempty"`
Alert Alert `json:"alert,omitempty"`
Badge *int `json:"badge,omitempty"`
Sound string `json:"sound,omitempty"`
ContentAvailable int `json:"content-available,omitempty"`
URLArgs []string `json:"url-args,omitempty"`
}

type Payload struct {
APS APS
APS APS
// MDM for mobile device management
MDM string
customValues map[string]interface{}
}

Expand Down Expand Up @@ -85,7 +90,11 @@ func (p *Payload) SetCustomValue(key string, value interface{}) error {
}

func (p *Payload) MarshalJSON() ([]byte, error) {
p.customValues["aps"] = p.APS
if len(p.MDM) != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry on the delay on this.

This feels a little awkward – there is some implicit behavior that isn't clear from the API. It feels like it should only be the if and no else. Whether or not "aps" is included shouldn't really depend on whether "mdm" exists.

My suggestion is:

if len(p.MDM) != 0 {
    p.customValues["mdm"] = p.MDM
}

p.customValues["aps"] = p.APS

And then including the approach for APS in this PR https://github.com/nathany/apns/pull/1 – it'll make the code much cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdotdub The thing is the json shouldn't include an empty "aps" node when using MDM.

Perhaps it makes more sense to check if aps is empty? Of course APS isn't just a string, so that's a deep check to ensure the entirety of APS is zero values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdotdub So I looked into your suggestion because I agree it's awkward as is.

But as far as I know, for Passbook notifications, the JSON should be {"aps":{}} even though there is nothing in the APS struct (aps.isZero() returns true).

For Mobile Device Management, there should be no aps key, as in {"mdm":"00000000-1111-3333-4444-555555555555"}.

So unfortunately there is a dependency there, at least as far as I can tell based on the documentation and other implementations. :-(

p.customValues["mdm"] = p.MDM
} else {
p.customValues["aps"] = p.APS
}

return json.Marshal(p.customValues)
}
Expand Down
33 changes: 33 additions & 0 deletions notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ var _ = Describe("Notifications", func() {
})
})

Describe("Safari", func() {
Describe("#MarshalJSON", func() {
Context("with complete payload", func() {
It("should marshal APS", func() {
p := apns.NewPayload()

p.APS.Alert.Title = "Hello World!"
p.APS.Alert.Body = "This is a body"
p.APS.Alert.Action = "Launch"
p.APS.URLArgs = []string{"hello", "world"}

b, err := json.Marshal(p)

Expect(err).To(BeNil())
Expect(b).To(Equal([]byte(`{"aps":{"alert":{"body":"This is a body","title":"Hello World!","action":"Launch"},"url-args":["hello","world"]}}`)))
})
})
})
})

Describe("Payload", func() {
Describe("#MarshalJSON", func() {
Context("with only APS", func() {
Expand Down Expand Up @@ -110,6 +130,19 @@ var _ = Describe("Notifications", func() {
Expect(b).To(Equal([]byte(`{"aps":{"alert":{"body":"testing"}},"email":"come@me.bro"}`)))
})
})

Context("with only MDM", func() {
It("should marshal MDM", func() {
p := apns.NewPayload()

p.MDM = "00000000-1111-3333-4444-555555555555"

b, err := json.Marshal(p)

Expect(err).To(BeNil())
Expect(b).To(Equal([]byte(`{"mdm":"00000000-1111-3333-4444-555555555555"}`)))
})
})
})
})

Expand Down