diff --git a/README.md b/README.md index 3bf8ef3..b0c865d 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,8 @@ c, _ := apns.NewClient(apns.ProductionGateway, apnsCert, apnsKey) p := apns.NewPayload() p.APS.Alert.Body = "I am a push notification!" -p.APS.Badge = 5 +badge := 5 +p.APS.Badge = &badge p.APS.Sound = "turn_down_for_what.aiff" m := apns.NewNotification() @@ -55,7 +56,8 @@ go func() { p := apns.NewPayload() p.APS.Alert.Body = "I am a push notification!" -p.APS.Badge = 5 +badge := 5 +p.APS.Badge = &badge p.APS.Sound = "turn_down_for_what.aiff" p.APS.ContentAvailable = 1 diff --git a/notification.go b/notification.go index 43fd208..a82557e 100644 --- a/notification.go +++ b/notification.go @@ -40,6 +40,8 @@ type NotificationResult struct { type Alert struct { // Do not add fields without updating the implementation of isZero. 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"` @@ -55,6 +57,7 @@ type APS struct { Badge *int // 0 to clear notifications, nil to leave as is. Sound string ContentAvailable int + URLArgs []string Category string // requires iOS 8+ } @@ -76,17 +79,26 @@ func (aps APS) MarshalJSON() ([]byte, error) { if aps.Category != "" { data["category"] = aps.Category } + if aps.URLArgs != nil && len(aps.URLArgs) != 0 { + data["url-args"] = aps.URLArgs + } return json.Marshal(data) } type Payload struct { - APS APS + APS APS + // MDM for mobile device management + MDM string customValues map[string]interface{} } func (p *Payload) MarshalJSON() ([]byte, error) { - p.customValues["aps"] = p.APS + if len(p.MDM) != 0 { + p.customValues["mdm"] = p.MDM + } else { + p.customValues["aps"] = p.APS + } return json.Marshal(p.customValues) } diff --git a/notification_test.go b/notification_test.go index 0d8dfd2..cea990a 100644 --- a/notification_test.go +++ b/notification_test.go @@ -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("no alert (as with Passbook)", func() { @@ -129,6 +149,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"}`))) + }) + }) }) })