feat: upgrade apns2 to v0.9 (#222)
This commit is contained in:
parent
b21b961d09
commit
957db775b3
|
@ -19,16 +19,16 @@ APNS/2 is a go package designed for simple, flexible and fast Apple Push Notific
|
||||||
- Make sure you have [Go](https://golang.org/doc/install) installed and have set your [GOPATH](https://golang.org/doc/code.html#GOPATH).
|
- Make sure you have [Go](https://golang.org/doc/install) installed and have set your [GOPATH](https://golang.org/doc/code.html#GOPATH).
|
||||||
- Download and install the dependencies:
|
- Download and install the dependencies:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go get -u golang.org/x/net/http2
|
go get -u golang.org/x/net/http2
|
||||||
go get -u golang.org/x/crypto/pkcs12
|
go get -u golang.org/x/crypto/pkcs12
|
||||||
```
|
```
|
||||||
|
|
||||||
- Install apns2:
|
- Install apns2:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go get -u github.com/sideshow/apns2
|
go get -u github.com/sideshow/apns2
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ func main() {
|
||||||
At a minimum, a _Notification_ needs a _DeviceToken_, a _Topic_ and a _Payload_.
|
At a minimum, a _Notification_ needs a _DeviceToken_, a _Topic_ and a _Payload_.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
notification := &Notification{
|
notification := &apns2.Notification{
|
||||||
DeviceToken: "11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7",
|
DeviceToken: "11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7",
|
||||||
Topic: "com.sideshow.Apns2",
|
Topic: "com.sideshow.Apns2",
|
||||||
Payload: []byte(`{"aps":{"alert":"Hello!"}}`),
|
Payload: []byte(`{"aps":{"alert":"Hello!"}}`),
|
||||||
|
@ -83,7 +83,7 @@ You can also set an optional _ApnsID_, _Expiration_ or _Priority_.
|
||||||
```go
|
```go
|
||||||
notification.ApnsID = "40636A2C-C093-493E-936A-2A4333C06DEA"
|
notification.ApnsID = "40636A2C-C093-493E-936A-2A4333C06DEA"
|
||||||
notification.Expiration = time.Now()
|
notification.Expiration = time.Now()
|
||||||
notification.Priority = apns.PriorityLow
|
notification.Priority = apns2.PriorityLow
|
||||||
```
|
```
|
||||||
|
|
||||||
## Payload
|
## Payload
|
||||||
|
@ -93,7 +93,7 @@ You can use raw bytes for the `notification.Payload` as above, or you can use th
|
||||||
```go
|
```go
|
||||||
// {"aps":{"alert":"hello","badge":1},"key":"val"}
|
// {"aps":{"alert":"hello","badge":1},"key":"val"}
|
||||||
|
|
||||||
payload := NewPayload().Alert("hello").Badge(1).Custom("key", "val")
|
payload := apns2.NewPayload().Alert("hello").Badge(1).Custom("key", "val")
|
||||||
|
|
||||||
notification.Payload = payload
|
notification.Payload = payload
|
||||||
client.Push(notification)
|
client.Push(notification)
|
||||||
|
@ -124,6 +124,20 @@ if res.Sent() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Context & Timeouts
|
||||||
|
|
||||||
|
For better control over request cancelations and timeouts APNS/2 supports
|
||||||
|
contexts. Using a context can be helpful if you want to cancel all pushes when
|
||||||
|
the parent process is cancelled, or need finer grained control over individual
|
||||||
|
push timeouts. See the [Google post](https://blog.golang.org/context) for more
|
||||||
|
information on contexts.
|
||||||
|
|
||||||
|
```go
|
||||||
|
ctx, cancel = context.WithTimeout(context.Background(), 10 * time.Second)
|
||||||
|
res, err := client.PushWithContext(ctx, notification)
|
||||||
|
defer cancel()
|
||||||
|
```
|
||||||
|
|
||||||
## Command line tool
|
## Command line tool
|
||||||
|
|
||||||
APNS/2 has a command line tool that can be installed with `go get github.com/sideshow/apns2/apns2`. Usage:
|
APNS/2 has a command line tool that can be installed with `go get github.com/sideshow/apns2/apns2`. Usage:
|
||||||
|
|
|
@ -32,7 +32,7 @@ var (
|
||||||
// HTTPClientTimeout specifies a time limit for requests made by the
|
// HTTPClientTimeout specifies a time limit for requests made by the
|
||||||
// HTTPClient. The timeout includes connection time, any redirects,
|
// HTTPClient. The timeout includes connection time, any redirects,
|
||||||
// and reading the response body.
|
// and reading the response body.
|
||||||
HTTPClientTimeout = 30 * time.Second
|
HTTPClientTimeout = 60 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client represents a connection with the APNs
|
// Client represents a connection with the APNs
|
||||||
|
@ -93,9 +93,23 @@ func (c *Client) Production() *Client {
|
||||||
// transparently before sending the notification. It will return a Response
|
// transparently before sending the notification. It will return a Response
|
||||||
// indicating whether the notification was accepted or rejected by the APNs
|
// indicating whether the notification was accepted or rejected by the APNs
|
||||||
// gateway, or an error if something goes wrong.
|
// gateway, or an error if something goes wrong.
|
||||||
|
//
|
||||||
|
// Use PushWithContext if you need better cancelation and timeout control.
|
||||||
func (c *Client) Push(n *Notification) (*Response, error) {
|
func (c *Client) Push(n *Notification) (*Response, error) {
|
||||||
payload, err := json.Marshal(n)
|
return c.PushWithContext(nil, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushWithContext sends a Notification to the APNs gateway. Context carries a
|
||||||
|
// deadline and a cancelation signal and allows you to close long running
|
||||||
|
// requests when the context timeout is exceeded. Context can be nil, for
|
||||||
|
// backwards compatibility.
|
||||||
|
//
|
||||||
|
// If the underlying http.Client is not currently connected, this method will
|
||||||
|
// attempt to reconnect transparently before sending the notification. It will
|
||||||
|
// return a Response indicating whether the notification was accepted or
|
||||||
|
// rejected by the APNs gateway, or an error if something goes wrong.
|
||||||
|
func (c *Client) PushWithContext(ctx Context, n *Notification) (*Response, error) {
|
||||||
|
payload, err := json.Marshal(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -103,7 +117,8 @@ func (c *Client) Push(n *Notification) (*Response, error) {
|
||||||
url := fmt.Sprintf("%v/3/device/%v", c.Host, n.DeviceToken)
|
url := fmt.Sprintf("%v/3/device/%v", c.Host, n.DeviceToken)
|
||||||
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload))
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload))
|
||||||
setHeaders(req, n)
|
setHeaders(req, n)
|
||||||
httpRes, err := c.HTTPClient.Do(req)
|
|
||||||
|
httpRes, err := c.requestWithContext(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
// +build go1.6,!go1.7
|
||||||
|
|
||||||
|
package apns2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"golang.org/x/net/context/ctxhttp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A Context carries a deadline, a cancelation signal, and other values across
|
||||||
|
// API boundaries.
|
||||||
|
//
|
||||||
|
// Context's methods may be called by multiple goroutines simultaneously.
|
||||||
|
type Context interface {
|
||||||
|
context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) requestWithContext(ctx Context, req *http.Request) (*http.Response, error) {
|
||||||
|
if ctx != nil {
|
||||||
|
return ctxhttp.Do(ctx, c.HTTPClient, req)
|
||||||
|
}
|
||||||
|
return c.HTTPClient.Do(req)
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
// +build go1.7
|
||||||
|
|
||||||
|
package apns2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A Context carries a deadline, a cancelation signal, and other values across
|
||||||
|
// API boundaries.
|
||||||
|
//
|
||||||
|
// Context's methods may be called by multiple goroutines simultaneously.
|
||||||
|
type Context interface {
|
||||||
|
context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) requestWithContext(ctx Context, req *http.Request) (*http.Response, error) {
|
||||||
|
if ctx != nil {
|
||||||
|
req = req.WithContext(ctx)
|
||||||
|
}
|
||||||
|
return c.HTTPClient.Do(req)
|
||||||
|
}
|
|
@ -255,10 +255,12 @@
|
||||||
"revisionTime": "2017-02-16T22:32:56Z"
|
"revisionTime": "2017-02-16T22:32:56Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "980httoMt2EXTX/XQJCLkCJZkhQ=",
|
"checksumSHA1": "iICAK15yh57U3UpWEOTdAQcvT5o=",
|
||||||
"path": "github.com/sideshow/apns2",
|
"path": "github.com/sideshow/apns2",
|
||||||
"revision": "c23f4b07d9e47aa490ff76001001decc688243f8",
|
"revision": "6ffbf017e660feff7df81e68de6482e263fae288",
|
||||||
"revisionTime": "2017-01-05T14:10:18Z"
|
"revisionTime": "2017-04-24T10:10:45Z",
|
||||||
|
"version": "v0.9",
|
||||||
|
"versionExact": "v0.9"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "c3x81Xq+gIn5JBgs1eCTVeRwDDo=",
|
"checksumSHA1": "c3x81Xq+gIn5JBgs1eCTVeRwDDo=",
|
||||||
|
|
Loading…
Reference in New Issue