Skip to content

Commit 9f1c369

Browse files
author
Adam Jones
committed
Merge pull request #3 from sideshow/fix-payload
Fix Notification interface
2 parents 0988951 + 1529fba commit 9f1c369

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,14 @@ func (c *Client) Production() *Client {
7373
// indicating whether the notification was accepted or rejected by the APNs
7474
// gateway, or an error if something goes wrong.
7575
func (c *Client) Push(n *Notification) (*Response, error) {
76-
jsonBytes, err := json.Marshal(n.Payload)
76+
payload, err := json.Marshal(n)
77+
7778
if err != nil {
7879
return nil, err
7980
}
8081

8182
url := fmt.Sprintf("%v/3/device/%v", c.Host, n.DeviceToken)
82-
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes))
83+
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload))
8384
setHeaders(req, n)
8485
httpRes, err := c.HTTPClient.Do(req)
8586
if err != nil {

client_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package apns2_test
22

33
import (
44
"crypto/tls"
5-
"encoding/json"
65
"fmt"
76
"io/ioutil"
87
"net/http"
@@ -125,11 +124,10 @@ func TestHeaders(t *testing.T) {
125124

126125
func TestPayload(t *testing.T) {
127126
n := mockNotification()
128-
bytes, _ := json.Marshal(n.Payload)
129127
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
130128
body, err := ioutil.ReadAll(r.Body)
131129
assert.NoError(t, err)
132-
assert.Equal(t, bytes, body)
130+
assert.Equal(t, n.Payload, body)
133131
}))
134132
defer server.Close()
135133
_, err := mockClient(server.URL).Push(n)

notification.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package apns2
22

3-
import "time"
3+
import (
4+
"encoding/json"
5+
"time"
6+
)
47

58
const (
69
// PriorityLow will tell APNs to send the push message at a time that takes
@@ -58,3 +61,15 @@ type Notification struct {
5861
// Remote Notification Programming Guide for more info.
5962
Payload interface{}
6063
}
64+
65+
// PayloadBytes converts the notification payload to JSON.
66+
func (n *Notification) MarshalJSON() ([]byte, error) {
67+
switch n.Payload.(type) {
68+
case string:
69+
return []byte(n.Payload.(string)), nil
70+
case []byte:
71+
return n.Payload.([]byte), nil
72+
default:
73+
return json.Marshal(n.Payload)
74+
}
75+
}

notification_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package apns2_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/sideshow/apns2"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestMarshalJSON(t *testing.T) {
11+
scenarios := []struct {
12+
in interface{}
13+
out []byte
14+
err error
15+
}{
16+
{`{"a": "b"}`, []byte(`{"a": "b"}`), nil},
17+
{[]byte(`{"a": "b"}`), []byte(`{"a": "b"}`), nil},
18+
{struct {
19+
A string `json:"a"`
20+
}{"b"}, []byte(`{"a":"b"}`), nil},
21+
}
22+
23+
notification := &apns2.Notification{}
24+
25+
for _, scenario := range scenarios {
26+
notification.Payload = scenario.in
27+
payloadBytes, err := notification.MarshalJSON()
28+
29+
assert.Equal(t, scenario.out, payloadBytes)
30+
assert.Equal(t, scenario.err, err)
31+
}
32+
}

0 commit comments

Comments
 (0)